42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
import uuid
|
|
from typing import List
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.song import Song
|
|
from app.schemas.song import SongCreate, SongUpdate
|
|
|
|
|
|
class CRUDSong(CRUDBase[Song, SongCreate, SongUpdate]):
|
|
def create(self, db: Session, *, obj_in: SongCreate) -> Song:
|
|
song_id = str(uuid.uuid4())
|
|
db_obj = Song(
|
|
id=song_id,
|
|
title=obj_in.title,
|
|
artist_id=obj_in.artist_id,
|
|
album_id=obj_in.album_id,
|
|
file_path=obj_in.file_path,
|
|
duration=obj_in.duration,
|
|
genre=obj_in.genre,
|
|
track_number=obj_in.track_number,
|
|
)
|
|
db.add(db_obj)
|
|
db.commit()
|
|
db.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def get_by_artist(self, db: Session, *, artist_id: str, skip: int = 0, limit: int = 100) -> List[Song]:
|
|
return db.query(Song).filter(Song.artist_id == artist_id).offset(skip).limit(limit).all()
|
|
|
|
def get_by_album(self, db: Session, *, album_id: str, skip: int = 0, limit: int = 100) -> List[Song]:
|
|
return db.query(Song).filter(Song.album_id == album_id).offset(skip).limit(limit).all()
|
|
|
|
def search_by_title(self, db: Session, *, title: str, skip: int = 0, limit: int = 100) -> List[Song]:
|
|
return db.query(Song).filter(Song.title.ilike(f"%{title}%")).offset(skip).limit(limit).all()
|
|
|
|
def search_by_genre(self, db: Session, *, genre: str, skip: int = 0, limit: int = 100) -> List[Song]:
|
|
return db.query(Song).filter(Song.genre.ilike(f"%{genre}%")).offset(skip).limit(limit).all()
|
|
|
|
|
|
song = CRUDSong(Song) |