mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 20:43:04 +00:00
breakdown models.py into db module and object models
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
from app import db
|
||||
from bson import ObjectId
|
||||
|
||||
|
||||
class Artists(db.Mongo):
|
||||
"""
|
||||
The artist class for all artist related database operations.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(Artists, self).__init__("ALL_ARTISTS")
|
||||
self.collection = self.db["THEM_ARTISTS"]
|
||||
|
||||
def insert_artist(self, artist_obj: dict) -> None:
|
||||
"""
|
||||
Inserts an artist into the database.
|
||||
"""
|
||||
self.collection.update_one(
|
||||
artist_obj, {"$set": artist_obj}, upsert=True
|
||||
).upserted_id
|
||||
|
||||
def get_all_artists(self) -> list:
|
||||
"""
|
||||
Returns a list of all artists in the database.
|
||||
"""
|
||||
return self.collection.find()
|
||||
|
||||
def get_artist_by_id(self, artist_id: str) -> dict:
|
||||
"""
|
||||
Returns an artist matching the mongo Id.
|
||||
"""
|
||||
return self.collection.find_one({"_id": ObjectId(artist_id)})
|
||||
|
||||
def get_artists_by_name(self, query: str):
|
||||
"""
|
||||
Returns all the artists matching the query.
|
||||
"""
|
||||
return self.collection.find({"name": query}).limit(20)
|
||||
Reference in New Issue
Block a user