mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 12:33:03 +00:00
refactor most things to use the database directly
This commit is contained in:
@@ -21,16 +21,17 @@ class Albums(MongoAlbums):
|
||||
"""
|
||||
album = album.__dict__
|
||||
return self.collection.update_one(
|
||||
{
|
||||
"album": album["title"],
|
||||
"artist": album["artist"]
|
||||
},
|
||||
{
|
||||
"$set": album
|
||||
},
|
||||
{"album": album["title"], "artist": album["artist"]},
|
||||
{"$set": album},
|
||||
upsert=True,
|
||||
).upserted_id
|
||||
|
||||
def insert_many(self, albums: list):
|
||||
"""
|
||||
Inserts multiple albums into the database.
|
||||
"""
|
||||
return self.collection.insert_many(albums)
|
||||
|
||||
def get_all_albums(self) -> list:
|
||||
"""
|
||||
Returns all the albums in the database.
|
||||
@@ -52,9 +53,9 @@ class Albums(MongoAlbums):
|
||||
album = self.collection.find_one({"album": name, "artist": artist})
|
||||
return convert_one(album)
|
||||
|
||||
def get_album_by_artist(self, name: str) -> dict:
|
||||
def find_album_by_hash(self, hash: str) -> dict:
|
||||
"""
|
||||
Returns a single album matching the artist in the query params.
|
||||
Returns a single album matching the hash in the query params.
|
||||
"""
|
||||
album = self.collection.find_one({"albumartist": name})
|
||||
album = self.collection.find_one({"hash": hash})
|
||||
return convert_one(album)
|
||||
|
||||
@@ -18,12 +18,8 @@ class Playlists(MongoPlaylists):
|
||||
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
|
||||
|
||||
@@ -41,25 +37,28 @@ class Playlists(MongoPlaylists):
|
||||
playlist = self.collection.find_one({"_id": ObjectId(id)})
|
||||
return convert_one(playlist)
|
||||
|
||||
def add_track_to_playlist(self, playlistid: str, track: dict) -> None:
|
||||
def set_last_updated(self, playlistid: str) -> None:
|
||||
"""
|
||||
Adds a track to a playlist.
|
||||
Sets the lastUpdated field to the current date.
|
||||
"""
|
||||
date = create_new_date()
|
||||
|
||||
return self.collection.update_one(
|
||||
{"_id": ObjectId(playlistid)},
|
||||
{"$set": {"lastUpdated": date}},
|
||||
)
|
||||
|
||||
def add_track_to_playlist(self, playlistid: str, track: dict) -> None:
|
||||
"""
|
||||
Adds a track to a playlist.
|
||||
"""
|
||||
self.collection.update_one(
|
||||
{
|
||||
"_id": ObjectId(playlistid),
|
||||
},
|
||||
{
|
||||
"$push": {
|
||||
"pre_tracks": track
|
||||
},
|
||||
"$set": {
|
||||
"lastUpdated": date
|
||||
}
|
||||
},
|
||||
{"$push": {"pre_tracks": track}},
|
||||
)
|
||||
self.set_last_updated(playlistid)
|
||||
|
||||
def get_playlist_by_name(self, name: str) -> dict:
|
||||
"""
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
This file contains the AllSongs class for interacting with track documents in MongoDB.
|
||||
"""
|
||||
import pymongo
|
||||
from app.db.mongodb import convert_many
|
||||
from app.db.mongodb import convert_one
|
||||
from app.db.mongodb import MongoTracks
|
||||
from app.db.mongodb import MongoTracks, convert_many, convert_one
|
||||
from bson import ObjectId
|
||||
|
||||
|
||||
@@ -24,17 +22,23 @@ class Tracks(MongoTracks):
|
||||
{"filepath": song_obj["filepath"]}, {"$set": song_obj}, upsert=True
|
||||
).upserted_id
|
||||
|
||||
def insert_many(self, songs: list):
|
||||
"""
|
||||
Inserts multiple songs into the database.
|
||||
"""
|
||||
return self.collection.insert_many(songs)
|
||||
|
||||
def get_all_tracks(self) -> list:
|
||||
"""
|
||||
Returns all tracks in the database.
|
||||
"""
|
||||
return convert_many(self.collection.find())
|
||||
|
||||
def get_song_by_id(self, file_id: str) -> dict:
|
||||
def get_track_by_id(self, id: str) -> dict:
|
||||
"""
|
||||
Returns a track object by its mongodb id.
|
||||
"""
|
||||
song = self.collection.find_one({"_id": ObjectId(file_id)})
|
||||
song = self.collection.find_one({"_id": ObjectId(id)})
|
||||
return convert_one(song)
|
||||
|
||||
def get_song_by_album(self, name: str, artist: str) -> dict:
|
||||
@@ -81,11 +85,20 @@ class Tracks(MongoTracks):
|
||||
|
||||
def find_songs_by_folder_og(self, query: str) -> list:
|
||||
"""
|
||||
Returns an unsorted list of all the tracks exactly matching the folder in the query params
|
||||
Returns an unsorted list of all the track matching the folder in the query params
|
||||
"""
|
||||
songs = self.collection.find({"folder": query})
|
||||
return convert_many(songs)
|
||||
|
||||
def find_tracks_inside_path_regex(self, path: str) -> list:
|
||||
"""
|
||||
Returns a list of all the tracks matching the path in the query params.
|
||||
"""
|
||||
songs = self.collection.find(
|
||||
{"filepath": {"$regex": f"^{path}", "$options": "i"}}
|
||||
)
|
||||
return convert_many(songs)
|
||||
|
||||
def find_songs_by_artist(self, query: str) -> list:
|
||||
"""
|
||||
Returns a list of all the tracks exactly matching the artists in the query params.
|
||||
@@ -128,3 +141,21 @@ class Tracks(MongoTracks):
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def find_tracks_by_hash(self, hash: str) -> list:
|
||||
"""
|
||||
Returns a list of all the tracks matching the hash in the query params.
|
||||
"""
|
||||
songs = self.collection.find({"albumhash": hash})
|
||||
return convert_many(songs)
|
||||
|
||||
def find_track_by_title_artists_album(
|
||||
self, title: str, artist: str, album: str
|
||||
) -> dict:
|
||||
"""
|
||||
Returns a single track matching the title, artist, and album in the query params.
|
||||
"""
|
||||
song = self.collection.find_one(
|
||||
{"title": title, "artists": artist, "album": album}
|
||||
)
|
||||
return convert_one(song)
|
||||
|
||||
Reference in New Issue
Block a user