|
|
@@ -2,6 +2,7 @@ import sqlite3
|
|
|
from abc import abstractmethod
|
|
|
|
|
|
from core import constants
|
|
|
+from core.models import MusicFolder, Track, Tag, Profile
|
|
|
|
|
|
|
|
|
class Repository:
|
|
|
@@ -28,8 +29,9 @@ class Repository:
|
|
|
cur = self.execute(f"SELECT * FROM {self.TABLE_NAME};", )
|
|
|
return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
|
|
|
- def get_by(self, **kwargs):
|
|
|
- pass
|
|
|
+ def get_by(self, field, val):
|
|
|
+ cur = self.execute(f"SELECT * FROM {self.TABLE_NAME} WHERE {field}=?;", val)
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
|
|
|
def get_by_raw_sql(self, sql, parameters=None):
|
|
|
parameters = parameters if parameters is not None else []
|
|
|
@@ -54,7 +56,8 @@ class Repository:
|
|
|
if commit:
|
|
|
self.commit()
|
|
|
|
|
|
- def delete(self, model, commit=False, permanent=False):
|
|
|
+ def delete(self, model, commit=False):
|
|
|
+ self.execute(f"DELETE FROM {self.TABLE_NAME} WHERE id=?;", model.id)
|
|
|
if commit:
|
|
|
self.commit()
|
|
|
|
|
|
@@ -70,27 +73,76 @@ class Repository:
|
|
|
|
|
|
class MusicFolderRepository(Repository):
|
|
|
TABLE_NAME = "MusicFolders"
|
|
|
+ MODEL_CLS = MusicFolder
|
|
|
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
|
-class ProfileRepository(Repository):
|
|
|
- TABLE_NAME = "Profiles"
|
|
|
+class TagRepository(Repository):
|
|
|
+ TABLE_NAME = "Tags"
|
|
|
+ MODEL_CLS = Tag
|
|
|
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
|
|
|
|
+ def get_by_track(self, track):
|
|
|
+ cur = self.execute(
|
|
|
+ f"""SELECT *
|
|
|
+ FROM Tags t
|
|
|
+ INNER JOIN TracksTags tt
|
|
|
+ ON tt.tag_id = t.id
|
|
|
+ WHERE tt.track_id=?;""", track.id)
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
|
|
|
-class TagRepository(Repository):
|
|
|
- TABLE_NAME = "Tags"
|
|
|
+
|
|
|
+class TrackRepository(Repository):
|
|
|
+ TABLE_NAME = "Tracks"
|
|
|
+ MODEL_CLS = Track
|
|
|
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
|
|
|
|
+ def get_by_tag(self, tag):
|
|
|
+ cur = self.execute(
|
|
|
+ f"""SELECT *
|
|
|
+ FROM Tracks t
|
|
|
+ INNER JOIN TracksTags tt
|
|
|
+ ON tt.track_id = t.id
|
|
|
+ WHERE tt.tag_id=?;""", tag.id)
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
+
|
|
|
+ def get_by_tags(self, tags):
|
|
|
+ cur = self.execute(
|
|
|
+ f"""SELECT *
|
|
|
+ FROM Tracks t
|
|
|
+ INNER JOIN TracksTags tt
|
|
|
+ ON tt.track_id = t.id
|
|
|
+ WHERE tt.tag_id in ({', '.join(['?' for _ in tags])};""", *[tag.id for tag in tags])
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
|
|
|
-class TrackRepository(Repository):
|
|
|
- TABLE_NAME = "Tracks"
|
|
|
+ def get_by_session(self, session):
|
|
|
+ cur = self.execute(
|
|
|
+ f"""SELECT *
|
|
|
+ FROM Tracks t
|
|
|
+ INNER JOIN SessionsTracks st
|
|
|
+ ON st.track_id = t.id
|
|
|
+ WHERE st.session_id=?;""", session.id)
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|
|
|
+
|
|
|
+
|
|
|
+class SessionRepository(Repository):
|
|
|
+ TABLE_NAME = "Sessions"
|
|
|
+ MODEL_CLS = Track
|
|
|
|
|
|
def __init__(self):
|
|
|
super().__init__()
|
|
|
+
|
|
|
+ def get_by_track(self, track):
|
|
|
+ cur = self.execute(
|
|
|
+ f"""SELECT *
|
|
|
+ FROM Tracks t
|
|
|
+ INNER JOIN SessionsTracks st
|
|
|
+ ON st.session_id = t.id
|
|
|
+ WHERE st.track_id=?;""", track.id)
|
|
|
+ return [self.MODEL_CLS(**row) for row in cur.fetchall()]
|