File size: 2,862 Bytes
25e4412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Saved mixes per song (only the requesting visitor's songs)."""

from __future__ import annotations

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import Response
from sqlmodel import Session, col, select

from stemdeck.db import get_session
from stemdeck.library import owned_song
from stemdeck.models import Mix
from stemdeck.schemas import MixIn, MixOut, MixUpdateIn, mix_out
from stemdeck.settings import get_settings
from stemdeck.visitors import current_owner

router = APIRouter(tags=["mixes"])

MAX_MIXES_PER_SONG_PUBLIC = 30


def _mix_or_404(session: Session, song_id: str, mix_id: str, owner: str) -> Mix:
    owned_song(session, song_id, owner)
    mix = session.get(Mix, mix_id)
    if mix is None or mix.song_id != song_id:
        raise HTTPException(404, "Mix not found.")
    return mix


@router.get("/songs/{song_id}/mixes", response_model=list[MixOut])
def list_mixes(song_id: str, session: Session = Depends(get_session),

               owner: str = Depends(current_owner)) -> list[MixOut]:
    owned_song(session, song_id, owner)
    mixes = session.exec(select(Mix).where(Mix.song_id == song_id).order_by(col(Mix.created_at))).all()
    return [mix_out(m) for m in mixes]


@router.post("/songs/{song_id}/mixes", response_model=MixOut, status_code=201)
def create_mix(song_id: str, body: MixIn, session: Session = Depends(get_session),

               owner: str = Depends(current_owner)) -> MixOut:
    owned_song(session, song_id, owner)
    if get_settings().public:
        count = len(session.exec(select(Mix.id).where(Mix.song_id == song_id)).all())
        if count >= MAX_MIXES_PER_SONG_PUBLIC:
            raise HTTPException(409, f"This song already has {count} saved mixes. Delete one first.")
    mix = Mix(song_id=song_id, name=body.name.strip(), settings=body.settings)
    session.add(mix)
    session.commit()
    session.refresh(mix)
    return mix_out(mix)


@router.put("/songs/{song_id}/mixes/{mix_id}", response_model=MixOut)
def update_mix(song_id: str, mix_id: str, body: MixUpdateIn, session: Session = Depends(get_session),

               owner: str = Depends(current_owner)) -> MixOut:
    mix = _mix_or_404(session, song_id, mix_id, owner)
    if body.name is not None:
        mix.name = body.name.strip()
    if body.settings is not None:
        mix.settings = body.settings
    session.add(mix)
    session.commit()
    session.refresh(mix)
    return mix_out(mix)


@router.delete("/songs/{song_id}/mixes/{mix_id}", status_code=204)
def delete_mix(song_id: str, mix_id: str, session: Session = Depends(get_session),

               owner: str = Depends(current_owner)) -> Response:
    session.delete(_mix_or_404(session, song_id, mix_id, owner))
    session.commit()
    return Response(status_code=204)