Further improvements

This commit is contained in:
2026-02-22 15:32:45 +02:00
parent c5ef4406f6
commit ef31852fd8
6 changed files with 179 additions and 28 deletions

View File

@@ -95,7 +95,7 @@ async def download_binary(
raise HTTPException(status_code=404, detail="Binary not built yet for this melody")
melody = await service.get_built_melody(melody_id)
filename = f"{melody.name}.bsm"
filename = f"{melody.pid or melody.name}.bsm"
return FileResponse(
path=str(path),

View File

@@ -130,7 +130,20 @@ async def get_built_melody(melody_id: str) -> BuiltMelodyInDB:
return _row_to_built_melody(row)
async def _check_unique(name: str, pid: str, exclude_id: Optional[str] = None) -> None:
"""Raise 409 if name or PID is already taken by another archetype."""
rows = await db.list_built_melodies()
for row in rows:
if exclude_id and row["id"] == exclude_id:
continue
if row["name"].lower() == name.lower():
raise HTTPException(status_code=409, detail=f"An archetype with the name '{name}' already exists.")
if pid and row.get("pid") and row["pid"].lower() == pid.lower():
raise HTTPException(status_code=409, detail=f"An archetype with the PID '{pid}' already exists.")
async def create_built_melody(data: BuiltMelodyCreate) -> BuiltMelodyInDB:
await _check_unique(data.name, data.pid or "")
melody_id = str(uuid.uuid4())
await db.insert_built_melody(
melody_id=melody_id,
@@ -150,6 +163,8 @@ async def update_built_melody(melody_id: str, data: BuiltMelodyUpdate) -> BuiltM
new_pid = data.pid if data.pid is not None else row["pid"]
new_steps = data.steps if data.steps is not None else row["steps"]
await _check_unique(new_name, new_pid or "", exclude_id=melody_id)
await db.update_built_melody(melody_id, name=new_name, pid=new_pid, steps=new_steps)
return await get_built_melody(melody_id)