Api routes refactor

This commit is contained in:
marc
2025-07-28 22:45:45 +02:00
parent 6aad7a069e
commit 8cdb9340fd
26 changed files with 103 additions and 29 deletions

View File

@@ -0,0 +1,13 @@
from folkugat_web.api.router import get_router
from . import editor, index, links, lyrics, properties, scores
router = get_router()
router.include_router(editor.router)
router.include_router(index.router)
router.include_router(links.router)
router.include_router(lyrics.router)
router.include_router(properties.router)
router.include_router(scores.router)
__all__ = ["router"]

View File

@@ -0,0 +1,15 @@
from fastapi import Request
from folkugat_web.api.router import get_router
from folkugat_web.fragments import tema
from folkugat_web.services import auth
router = get_router()
@router.get("/api/tema/{tema_id}/editor/title")
def title_editor(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
):
return tema.title_editor(request=request, logged_in=logged_in, tema_id=tema_id)

View File

@@ -0,0 +1,107 @@
from typing import Annotated
from fastapi import HTTPException, Request
from fastapi.params import Form
from fastapi.responses import HTMLResponse
from folkugat_web.api.router import get_router
from folkugat_web.fragments import tema, temes
from folkugat_web.services import auth
from folkugat_web.services import files as files_service
from folkugat_web.services.temes import links as links_service
from folkugat_web.services.temes import lyrics as lyrics_service
from folkugat_web.services.temes import properties as properties_service
from folkugat_web.services.temes import query as temes_q
from folkugat_web.services.temes import scores as scores_service
from folkugat_web.services.temes import write as temes_w
from folkugat_web.templates import templates
from folkugat_web.utils import FnChain
router = get_router()
@router.get("/tema/{tema_id}")
def page(request: Request, logged_in: auth.LoggedIn, tema_id: int):
return templates.TemplateResponse(
"index.html",
{
"request": request,
"page_title": "Folkugat",
"content": f"/api/tema/{tema_id}",
"logged_in": logged_in,
}
)
@router.get("/api/tema/{tema_id}")
def contingut(request: Request, logged_in: auth.LoggedIn, tema_id: int):
tema = temes_q.get_tema_by_id(tema_id)
if not tema:
raise HTTPException(status_code=404, detail="Could not find tune")
tema = (
FnChain.transform(tema) |
temes_q.tema_compute_stats |
links_service.add_links_to_tema |
lyrics_service.add_lyrics_to_tema |
scores_service.add_scores_to_tema |
properties_service.add_properties_to_tema
).result()
return temes.tema(request, logged_in, tema)
@router.delete("/api/tema/{tema_id}")
def delete_tema(_: auth.RequireLogin, tema_id: int):
tema = temes_q.get_tema_by_id(tema_id)
if not tema:
raise HTTPException(status_code=404, detail="Could not find tune")
tema = temes_q.tema_compute_stats(tema=tema)
if tema.stats:
raise HTTPException(status_code=400, detail="Can't delete a tune that has been played in a set!")
temes_w.delete_tema(tema_id=tema_id)
files_service.clean_orphan_files()
return HTMLResponse(headers={
'HX-Redirect': '/temes'
})
@router.post("/api/tema")
def create_tema(_: auth.RequireLogin, title: Annotated[str, Form()] = ""):
new_tema = temes_w.create_tema(title=title)
return HTMLResponse(headers={
'HX-Redirect': f'/tema/{new_tema.id}'
})
@router.get("/api/tema/{tema_id}/title")
def title(request: Request, logged_in: auth.LoggedIn, tema_id: int):
return tema.title(request=request, tema_id=tema_id, logged_in=logged_in)
@router.put("/api/tema/{tema_id}/title")
def set_title(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
title: Annotated[str, Form()],
):
new_tema = temes_w.update_title(tema_id=tema_id, title=title)
return tema.title(request=request, tema=new_tema, logged_in=logged_in)
@router.put("/api/tema/{tema_id}/visible")
def set_visible(request: Request, logged_in: auth.RequireLogin, tema_id: int):
new_tema = temes_w.set_visibility(tema_id=tema_id, hidden=False)
return tema.visibility(
request=request,
logged_in=logged_in,
tema=new_tema,
)
@router.put("/api/tema/{tema_id}/invisible")
def set_invisible(request: Request, logged_in: auth.RequireLogin, tema_id: int):
new_tema = temes_w.set_visibility(tema_id=tema_id, hidden=True)
return tema.visibility(
request=request,
logged_in=logged_in,
tema=new_tema,
)

View File

@@ -0,0 +1,169 @@
from typing import Annotated
from fastapi import HTTPException, Request, UploadFile
from fastapi.params import File, Form, Param
from fastapi.responses import HTMLResponse
from folkugat_web.api.router import get_router
from folkugat_web.fragments.tema import links as links_fragments
from folkugat_web.model import temes as model
from folkugat_web.services import auth
from folkugat_web.services import files as files_service
from folkugat_web.services.temes import links as links_service
from folkugat_web.services.temes import query as temes_q
router = get_router()
@router.get("/api/tema/{tema_id}/link/{link_id}")
def link(request: Request, logged_in: auth.LoggedIn, tema_id: int, link_id: int):
link = links_service.get_link_by_id(link_id=link_id, tema_id=tema_id)
if not link:
raise HTTPException(status_code=404, detail="Could not find link!")
return links_fragments.link(request=request, logged_in=logged_in, link=link)
@router.put("/api/tema/{tema_id}/link/{link_id}")
async def set_link(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
link_id: int,
content_type: Annotated[model.ContentType, Form()],
title: Annotated[str | None, Form()] = None,
url: Annotated[str | None, Form()] = None,
upload_file: Annotated[UploadFile | None, File()] = None,
):
if upload_file:
url = await files_service.store_file(tema_id=tema_id, upload_file=upload_file)
link_type = links_service.guess_link_type(url or '')
new_link = model.Link(
id=link_id,
tema_id=tema_id,
content_type=content_type,
link_type=link_type,
url=(url or '').strip(),
title=(title or '').strip(),
)
links_service.update_link(link=new_link)
return links_fragments.link(request=request, logged_in=logged_in, link=new_link)
@router.post("/api/tema/{tema_id}/link")
def create_link(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
):
link = links_service.create_link(tema_id=tema_id)
return links_fragments.link_editor(
request=request,
logged_in=logged_in,
link=link,
)
@router.delete("/api/tema/{tema_id}/link/{link_id}")
def delete_link(
_logged_in: auth.RequireLogin,
tema_id: int,
link_id: int,
):
links_service.delete_link(link_id=link_id, tema_id=tema_id)
files_service.clean_orphan_files()
return HTMLResponse(
headers={
"HX-Trigger": f"reload-tema-{tema_id}-score"
}
)
@router.get("/api/tema/{tema_id}/link/{link_id}/icon")
def link_icon(
request: Request,
logged_in: auth.LoggedIn,
tema_id: int,
link_id: int,
content_type: Annotated[model.ContentType, Param()],
url: Annotated[str, Param()],
):
link = model.Link(
id=link_id,
tema_id=tema_id,
content_type=content_type,
link_type=links_service.guess_link_type(url),
url=url,
)
return links_fragments.link_icon(
request=request,
logged_in=logged_in,
link=link,
)
@router.get("/api/tema/{tema_id}/score")
def get_score(
request: Request,
logged_in: auth.LoggedIn,
tema_id: int,
):
tema = temes_q.get_tema_by_id(tema_id)
if not tema:
raise HTTPException(status_code=404, detail="Could not find tune")
tema = links_service.add_links_to_tema(tema)
return links_fragments.score(
request=request,
logged_in=logged_in,
tema=tema,
)
@router.get("/api/tema/{tema_id}/editor/link/{link_id}")
def link_editor(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
link_id: int,
):
link = links_service.get_link_by_id(link_id=link_id, tema_id=tema_id)
if not link:
raise HTTPException(status_code=404, detail="Could not find link!")
return links_fragments.link_editor(
request=request,
logged_in=logged_in,
link=link,
)
@router.get("/api/tema/{tema_id}/editor/link/{link_id}/url")
def link_editor_url_input(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
link_id: int,
):
link = links_service.get_link_by_id(link_id=link_id, tema_id=tema_id)
if not link:
raise HTTPException(status_code=404, detail="Could not find link!")
return links_fragments.link_editor_url(
request=request,
logged_in=logged_in,
link=link,
)
@router.get("/api/tema/{tema_id}/editor/link/{link_id}/file")
def link_editor_file_input(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
link_id: int,
):
link = links_service.get_link_by_id(link_id=link_id, tema_id=tema_id)
if not link:
raise HTTPException(status_code=404, detail="Could not find link!")
return links_fragments.link_editor_file(
request=request,
logged_in=logged_in,
link=link,
)

View File

@@ -0,0 +1,81 @@
import dataclasses
from typing import Annotated
from fastapi import HTTPException, Request
from fastapi.params import Form
from fastapi.responses import HTMLResponse
from folkugat_web.api.router import get_router
from folkugat_web.fragments.tema import lyrics as lyrics_fragments
from folkugat_web.services import auth
from folkugat_web.services.temes import lyrics as lyrics_service
router = get_router()
@router.get("/api/tema/{tema_id}/lyric/{lyric_id}")
def lyric(
request: Request,
logged_in: auth.LoggedIn,
tema_id: int,
lyric_id: int,
):
lyric = lyrics_service.get_lyric_by_id(lyric_id=lyric_id, tema_id=tema_id)
if not lyric:
raise HTTPException(status_code=404, detail="Could not find lyric!")
return lyrics_fragments.lyric(request=request, logged_in=logged_in, lyric=lyric)
@router.put("/api/tema/{tema_id}/lyric/{lyric_id}")
def set_lyric(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
lyric_id: int,
title: Annotated[str, Form()],
content: Annotated[str, Form()],
):
lyric = lyrics_service.get_lyric_by_id(lyric_id=lyric_id, tema_id=tema_id)
if not lyric:
raise HTTPException(status_code=404, detail="Could not find lyric!")
new_lyric = lyrics_service.update_lyric(lyric=lyric, title=title, content=content)
return lyrics_fragments.lyric(request=request, logged_in=logged_in, lyric=new_lyric)
@router.post("/api/tema/{tema_id}/lyric")
def add_lyric(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
):
lyric = lyrics_service.create_lyric(tema_id=tema_id)
return lyrics_fragments.lyric_editor(
request=request,
logged_in=logged_in,
lyric=lyric,
)
@router.delete("/api/tema/{tema_id}/lyric/{lyric_id}")
def delete_lyric(
tema_id: int,
lyric_id: int,
):
lyrics_service.delete_lyric(lyric_id=lyric_id, tema_id=tema_id)
return HTMLResponse()
@router.get("/api/tema/{tema_id}/editor/lyric/{lyric_id}")
def lyric_editor(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
lyric_id: int,
):
lyric = lyrics_service.get_lyric_by_id(lyric_id=lyric_id, tema_id=tema_id)
if not lyric:
raise HTTPException(status_code=404, detail="Could not find lyric!")
return lyrics_fragments.lyric_editor(
request=request,
logged_in=logged_in,
lyric=lyric,
)

View File

@@ -0,0 +1,75 @@
import dataclasses
from typing import Annotated
from fastapi import HTTPException, Request
from fastapi.params import Form
from fastapi.responses import HTMLResponse
from folkugat_web.api.router import get_router
from folkugat_web.fragments.tema import properties as properties_fragments
from folkugat_web.model import temes as model
from folkugat_web.services import auth
from folkugat_web.services.temes import properties as properties_service
router = get_router()
@router.get("/api/tema/{tema_id}/property/{property_id}")
def property_(request: Request, logged_in: auth.LoggedIn, tema_id: int, property_id: int):
property = properties_service.get_property_by_id(property_id=property_id, tema_id=tema_id)
if not property:
raise HTTPException(status_code=404, detail="Could not find lyric!")
return properties_fragments.property_(request=request, logged_in=logged_in, property=property)
@router.put("/api/tema/{tema_id}/property/{property_id}")
def set_property(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
property_id: int,
field: Annotated[model.PropertyField, Form()],
value: Annotated[str, Form()],
):
property = properties_service.get_property_by_id(property_id=property_id, tema_id=tema_id)
if not property:
raise HTTPException(status_code=404, detail="Could not find lyric!")
new_property = dataclasses.replace(property, field=field, value=value.strip())
properties_service.update_property(property=new_property)
return properties_fragments.property_(request=request, logged_in=logged_in, property=new_property)
@router.post("/api/tema/{tema_id}/property")
def add_property(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
):
property = properties_service.create_property(tema_id=tema_id)
return properties_fragments.property_editor(
request=request,
logged_in=logged_in,
property=property,
)
@router.delete("/api/tema/{tema_id}/property/{property_id}")
def delete_property(_: auth.RequireLogin, tema_id: int, property_id: int):
properties_service.delete_property(property_id=property_id, tema_id=tema_id)
return HTMLResponse()
@router.get("/api/tema/{tema_id}/editor/property/{property_id}")
def property_editor(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
property_id: int,
):
property = properties_service.get_property_by_id(property_id=property_id, tema_id=tema_id)
if not property:
raise HTTPException(status_code=404, detail="Could not find lyric!")
return properties_fragments.property_editor(
request=request,
logged_in=logged_in,
property=property,
)

View File

@@ -0,0 +1,132 @@
import dataclasses
from typing import Annotated
from fastapi import HTTPException, Request
from fastapi.params import Form
from fastapi.responses import HTMLResponse
from folkugat_web.api.router import get_router
from folkugat_web.fragments import temes
from folkugat_web.fragments.tema import scores as scores_fragments
from folkugat_web.services import auth, files
from folkugat_web.services.lilypond import build as lilypond_build
from folkugat_web.services.lilypond import render as lilypond_render
from folkugat_web.services.lilypond import source as lilypond_source
from folkugat_web.services.temes import scores as scores_service
router = get_router()
@router.get("/api/tema/{tema_id}/score/{score_id}")
def score(request: Request, logged_in: auth.LoggedIn, tema_id: int, score_id: int):
score = scores_service.get_score_by_id(score_id=score_id, tema_id=tema_id)
if not score:
raise HTTPException(status_code=404, detail="Could not find lyric!")
return scores_fragments.score(request=request, logged_in=logged_in, score=score)
@router.put("/api/tema/{tema_id}/score/{score_id}")
async def set_score(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
score_id: int,
title: Annotated[str, Form()],
source: Annotated[str, Form()],
):
source = source.strip()
score = scores_service.get_score_by_id(score_id=score_id, tema_id=tema_id)
if not score:
raise HTTPException(status_code=404, detail="Could not find lyric!")
new_score = await scores_service.render_score(score=score, title=title, source=source)
scores_service.update_score(score=new_score)
files.clean_orphan_files()
return scores_fragments.score(request=request, logged_in=logged_in, score=new_score)
@router.post("/api/tema/{tema_id}/score")
def add_score(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
):
score = scores_service.create_score(tema_id=tema_id)
return scores_fragments.score_editor(
request=request,
logged_in=logged_in,
score=score,
)
@router.delete("/api/tema/{tema_id}/score/{score_id}")
def delete_score(request: Request, logged_in: auth.RequireLogin, tema_id: int, score_id: int):
scores_service.delete_score(score_id=score_id, tema_id=tema_id)
files.clean_orphan_files()
return HTMLResponse()
@router.get("/api/tema/{tema_id}/editor/score/{score_id}")
def score_editor(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
score_id: int,
):
score = scores_service.get_score_by_id(score_id=score_id, tema_id=tema_id)
if not score:
raise HTTPException(status_code=404, detail="Could not find lyric!")
return scores_fragments.score_editor(
request=request,
logged_in=logged_in,
score=score,
)
@router.post("/api/tema/{tema_id}/editor/score/{score_id}/render")
async def render(
request: Request,
_: auth.RequireLogin,
tema_id: int,
score_id: int,
source: Annotated[str, Form()],
):
tune = lilypond_build.tune_from_tema_id(tema_id=tema_id, score_source=source)
full_source = lilypond_source.tune_source(tune=tune)
png_result = await lilypond_render.render(
source=full_source,
output=lilypond_render.RenderOutput.PNG_CROPPED,
)
if output_filename := png_result.result:
score_render_url = files.get_db_file_path(output_filename)
return temes.score_render(request=request, score_id=score_id, score_render_url=score_render_url)
else:
return temes.score_render(request=request, score_id=score_id, errors=png_result.error)
@router.put("/api/tema/{tema_id}/score/{score_id}/show")
async def show_score(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
score_id: int,
):
score = scores_service.get_score_by_id(score_id=score_id, tema_id=tema_id)
if not score:
raise HTTPException(status_code=404, detail="Could not find lyric!")
new_score = dataclasses.replace(score, hidden=False)
scores_service.update_score(score=new_score)
return scores_fragments.score(request=request, logged_in=logged_in, score=new_score)
@router.put("/api/tema/{tema_id}/score/{score_id}/hide")
async def hide_score(
request: Request,
logged_in: auth.RequireLogin,
tema_id: int,
score_id: int,
):
score = scores_service.get_score_by_id(score_id=score_id, tema_id=tema_id)
if not score:
raise HTTPException(status_code=404, detail="Could not find lyric!")
new_score = dataclasses.replace(score, hidden=True)
scores_service.update_score(score=new_score)
return scores_fragments.score(request=request, logged_in=logged_in, score=new_score)