diff --git a/server/app/db/__init__.py b/server/app/db/__init__.py index 18f55f06..e7157d74 100644 --- a/server/app/db/__init__.py +++ b/server/app/db/__init__.py @@ -1,44 +1,202 @@ -""" -This module creates and initiliazes a MongoDB instance. It also contains the -`convert_one()` and `conver_many()` methods for converting MongoDB cursors to Python dicts. -""" - -import pymongo -import json -from bson import json_util - - -class Mongo: +class AlbumMethods: """ - The base class for all mongodb classes. + Lists all the methods that can be found in the Albums class. """ - def __init__(self, database): - mongo_uri = pymongo.MongoClient() - self.db = mongo_uri[database] + def insert_album(): + """ + Inserts a new album object into the database. + """ + pass + + def get_all_albums(): + """ + Returns all the albums in the database. + """ + pass + + def get_album_by_id(): + """ + Returns a single album matching the passed id. + """ + pass + + def get_album_by_name(): + """ + Returns a single album matching the passed name. + """ + pass + + def get_album_by_artist(): + """ + Returns a single album matching the passed artist name. + """ + pass -def convert_one(song): +class ArtistMethods: """ - Converts a single mongodb cursor to a json object. - """ - json_song = json.dumps(song, default=json_util.default) - loaded_song = json.loads(json_song) - - return loaded_song - - -def convert_many(array): - """ - Converts a list of mongodb cursors to a list of json objects. + Lists all the methods that can be found in the Artists class. """ - songs = [] + def insert_artist(): + """ + Inserts a new artist object into the database. + """ + pass - for song in array: - json_song = json.dumps(song, default=json_util.default) - loaded_song = json.loads(json_song) + def get_all_artists(): + """ + Returns all the artists in the database. + """ + pass - songs.append(loaded_song) + def get_artist_by_id(): + """ + Returns an artist matching the mongo Id. + """ + pass - return songs + def get_artists_by_name(): + """ + Returns all the artists matching the query. + """ + pass + + +class PlaylistMethods: + """ + Lists all the methods that can be found in the Playlists class. + """ + + def insert_playlist(): + """ + Inserts a new playlist object into the database. + """ + pass + + def get_all_playlists(): + """ + Returns all the playlists in the database. + """ + pass + + def get_playlist_by_id(): + """ + Returns a single playlist matching the id in the query params. + """ + pass + + def add_track_to_playlist(): + """ + Adds a track to a playlist. + """ + pass + + def get_playlist_by_name(): + """ + Returns a single playlist matching the name in the query params. + """ + pass + + def update_playlist(): + """ + Updates a playlist. + """ + pass + + +class TrackMethods: + """ + Lists all the methods that can be found in the Tracks class. + """ + + def insert_track(): + """ + Inserts a new track object into the database. + """ + pass + + def drop_db(): + """ + Drops the entire database. + """ + pass + + def get_all_tracks(): + """ + Returns all the tracks in the database. + """ + pass + + def get_track_by_id(): + """ + Returns a single track matching the id in the query params. + """ + pass + + def get_track_by_album(): + """ + Returns a single track matching the album in the query params. + """ + pass + + def search_tracks_by_album(): + """ + Returns all the tracks matching the albums in the query params (using regex). + """ + pass + + def search_tracks_by_artist(): + """ + Returns all the tracks matching the artists in the query params. + """ + pass + + def find_track_by_title(): + """ + Finds all the tracks matching the title in the query params. + """ + pass + + def find_tracks_by_album(): + """ + Finds all the tracks matching the album in the query params. + """ + pass + + def find_tracks_by_folder(): + """ + Finds all the tracks matching the folder in the query params. + """ + pass + + def find_tracks_by_artist(): + """ + Finds all the tracks matching the artist in the query params. + """ + pass + + def find_tracks_by_albumartist(): + """ + Finds all the tracks matching the album artist in the query params. + """ + pass + + def get_track_by_path(): + """ + Returns a single track matching the path in the query params. + """ + pass + + def remove_track_by_path(): + """ + Removes a track from the database. Returns a boolean indicating success or failure of the operation. + """ + pass + + def remove_track_by_id(): + """ + Removes a track from the database. Returns a boolean indicating success or failure of the operation. + """ + pass diff --git a/server/app/db/mongodb/__init__.py b/server/app/db/mongodb/__init__.py new file mode 100644 index 00000000..e64c3209 --- /dev/null +++ b/server/app/db/mongodb/__init__.py @@ -0,0 +1,80 @@ +""" +This module creates and initiliazes a MongoDB instance. It also contains the +`convert_one()` and `conver_many()` methods for converting MongoDB cursors to Python dicts. +""" +import json + +import pymongo +from app.db import AlbumMethods +from app.db import ArtistMethods +from app.db import PlaylistMethods +from app.db import TrackMethods +from bson import json_util + + +class Mongo: + """ + The base class for all mongodb classes. + """ + + def __init__(self, database): + mongo_uri = pymongo.MongoClient() + self.db = mongo_uri[database] + + +class MongoAlbums(Mongo, AlbumMethods): + + def __init__(self): + super(MongoAlbums, self).__init__("ALICE_ALBUMS") + self.collection = self.db["ALL_ALBUMS"] + + +class MongoArtists(Mongo, ArtistMethods): + + def __init__(self): + super(MongoArtists, self).__init__("ALICE_ARTISTS") + self.collection = self.db["ALL_ARTISTS"] + + +class MongoPlaylists(Mongo, PlaylistMethods): + + def __init__(self): + super(MongoPlaylists, self).__init__("ALICE_PLAYLISTS") + self.collection = self.db["ALL_PLAYLISTS"] + + +class MongoTracks(Mongo, TrackMethods): + + def __init__(self): + super(MongoTracks, self).__init__("ALICE_MUSIC_TRACKS") + self.collection = self.db["ALL_TRACKS"] + + +# ====================================================================== # +# cursor convertion methods + + +def convert_one(song): + """ + Converts a single mongodb cursor to a json object. + """ + json_song = json.dumps(song, default=json_util.default) + loaded_song = json.loads(json_song) + + return loaded_song + + +def convert_many(array): + """ + Converts a list of mongodb cursors to a list of json objects. + """ + + songs = [] + + for song in array: + json_song = json.dumps(song, default=json_util.default) + loaded_song = json.loads(json_song) + + songs.append(loaded_song) + + return songs diff --git a/server/app/db/albums.py b/server/app/db/mongodb/albums.py similarity index 88% rename from server/app/db/albums.py rename to server/app/db/mongodb/albums.py index 319da9f5..27b7feb5 100644 --- a/server/app/db/albums.py +++ b/server/app/db/mongodb/albums.py @@ -3,22 +3,18 @@ This file contains the Album class for interacting with album documents in MongoDB. """ from app import db +from app.db.mongodb import convert_many +from app.db.mongodb import convert_one +from app.db.mongodb import MongoAlbums from app.models import Album from bson import ObjectId -convert_many = db.convert_many -convert_one = db.convert_one - -class Albums(db.Mongo): +class Albums(MongoAlbums): """ The class for all album-related database operations. """ - def __init__(self): - super(Albums, self).__init__("ALICE_ALBUMS") - self.collection = self.db["ALL_ALBUMS"] - def insert_album(self, album: Album) -> None: """ Inserts a new album object into the database. diff --git a/server/app/db/artists.py b/server/app/db/mongodb/artists.py similarity index 85% rename from server/app/db/artists.py rename to server/app/db/mongodb/artists.py index 4f82428d..db449399 100644 --- a/server/app/db/artists.py +++ b/server/app/db/mongodb/artists.py @@ -1,19 +1,15 @@ """ This file contains the Artists class for interacting with artist documents in MongoDB. """ -from app import db +from app.db.mongodb import MongoArtists from bson import ObjectId -class Artists(db.Mongo): +class Artists(MongoArtists): """ The artist class for all artist related database operations. """ - def __init__(self): - super(Artists, self).__init__("ALICE_ARTISTS") - self.collection = self.db["ALL_ARTISTS"] - def insert_artist(self, artist_obj: dict) -> None: """ Inserts an artist into the database. diff --git a/server/app/db/playlists.py b/server/app/db/mongodb/playlists.py similarity index 78% rename from server/app/db/playlists.py rename to server/app/db/mongodb/playlists.py index 7447b46c..d6df804b 100644 --- a/server/app/db/playlists.py +++ b/server/app/db/mongodb/playlists.py @@ -1,32 +1,29 @@ """ This file contains the Playlists class for interacting with the playlist documents in MongoDB. """ -from app import db -from app import models +from app.db.mongodb import convert_many +from app.db.mongodb import convert_one +from app.db.mongodb import MongoPlaylists +from app.helpers import create_new_date from bson import ObjectId -from app.helpers import create_new_date -convert_many = db.convert_many -convert_one = db.convert_one - - -class Playlists(db.Mongo): +class Playlists(MongoPlaylists): """ The class for all playlist-related database operations. """ - def __init__(self): - super(Playlists, self).__init__("ALICE_PLAYLISTS") - self.collection = self.db["ALL_PLAYLISTS"] - def insert_playlist(self, playlist: dict) -> None: """ Inserts a new playlist object into the database. """ return self.collection.update_one( - {"name": playlist["name"]}, - {"$set": playlist}, + { + "name": playlist["name"] + }, + { + "$set": playlist + }, upsert=True, ).upserted_id @@ -54,7 +51,14 @@ class Playlists(db.Mongo): { "_id": ObjectId(playlistid), }, - {"$push": {"pre_tracks": track}, "$set": {"lastUpdated": date}}, + { + "$push": { + "pre_tracks": track + }, + "$set": { + "lastUpdated": date + } + }, ) def get_playlist_by_name(self, name: str) -> dict: diff --git a/server/app/db/mongodb/trackcolors.py b/server/app/db/mongodb/trackcolors.py new file mode 100644 index 00000000..09f7ef2b --- /dev/null +++ b/server/app/db/mongodb/trackcolors.py @@ -0,0 +1,30 @@ +# """ +# This file contains the TrackColors class for interacting with Track colors documents in MongoDB. +# """ +# from app import db +# class TrackColors(db.Mongo): +# """ +# The class for all track-related database operations. +# """ +# def __init__(self): +# super(TrackColors, self).__init__("ALICE_TRACK_COLORS") +# self.collection = self.db["TRACK_COLORS"] +# def insert_track_color(self, track_color: dict) -> None: +# """ +# Inserts a new track object into the database. +# """ +# return self.collection.update_one( +# { +# "filepath": track_color["filepath"] +# }, +# { +# "$set": track_color +# }, +# upsert=True, +# ).upserted_id +# def get_track_color_by_track(self, filepath: str) -> dict: +# """ +# Returns a track color object by its filepath. +# """ +# track_color = self.collection.find_one({"filepath": filepath}) +# return db.convert_one(track_color) diff --git a/server/app/db/tracks.py b/server/app/db/mongodb/tracks.py similarity index 95% rename from server/app/db/tracks.py rename to server/app/db/mongodb/tracks.py index b4e5ca1d..c8ccb358 100644 --- a/server/app/db/tracks.py +++ b/server/app/db/mongodb/tracks.py @@ -1,22 +1,17 @@ """ This file contains the AllSongs class for interacting with track documents in MongoDB. """ -from app import db +from app.db.mongodb import convert_many +from app.db.mongodb import convert_one +from app.db.mongodb import MongoTracks from bson import ObjectId -convert_many = db.convert_many -convert_one = db.convert_one - -class AllSongs(db.Mongo): +class Tracks(MongoTracks): """ The class for all track-related database operations. """ - def __init__(self): - super(AllSongs, self).__init__("ALICE_MUSIC_TRACKS") - self.collection = self.db["ALL_TRACKS"] - # def drop_db(self): # self.collection.drop() diff --git a/server/app/db/trackcolors.py b/server/app/db/trackcolors.py deleted file mode 100644 index fc52c4ca..00000000 --- a/server/app/db/trackcolors.py +++ /dev/null @@ -1,35 +0,0 @@ -""" -This file contains the TrackColors class for interacting with Track colors documents in MongoDB. -""" -from app import db - - -class TrackColors(db.Mongo): - """ - The class for all track-related database operations. - """ - - def __init__(self): - super(TrackColors, self).__init__("ALICE_TRACK_COLORS") - self.collection = self.db["TRACK_COLORS"] - - def insert_track_color(self, track_color: dict) -> None: - """ - Inserts a new track object into the database. - """ - return self.collection.update_one( - { - "filepath": track_color["filepath"] - }, - { - "$set": track_color - }, - upsert=True, - ).upserted_id - - def get_track_color_by_track(self, filepath: str) -> dict: - """ - Returns a track color object by its filepath. - """ - track_color = self.collection.find_one({"filepath": filepath}) - return db.convert_one(track_color) diff --git a/server/app/instances.py b/server/app/instances.py index 08fb85a3..51f8fb41 100644 --- a/server/app/instances.py +++ b/server/app/instances.py @@ -1,14 +1,12 @@ """ All the MongoDB instances are created here. """ -from app.db import albums -from app.db import artists -from app.db import playlists -from app.db import trackcolors -from app.db import tracks +from app.db.mongodb import albums +from app.db.mongodb import artists +from app.db.mongodb import playlists +from app.db.mongodb import tracks -tracks_instance = tracks.AllSongs() +tracks_instance = tracks.Tracks() artist_instance = artists.Artists() -track_color_instance = trackcolors.TrackColors() album_instance = albums.Albums() playlist_instance = playlists.Playlists()