霖雨寺
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
import sqlite3
|
||||
from typing import List
|
||||
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
|
||||
from app.database import Database
|
||||
from app.schemas import RitualService, RitualServiceCreate, RitualServiceUpdate
|
||||
from app.temple_scope import TempleId, ensure_temple_exists
|
||||
|
||||
|
||||
router = APIRouter(prefix="/ritual-services", tags=["ritual-services"])
|
||||
|
||||
|
||||
def row_to_ritual(row: sqlite3.Row) -> RitualService:
|
||||
return RitualService(
|
||||
id=row["id"],
|
||||
temple_id=row["temple_id"],
|
||||
name=row["name"],
|
||||
description=row["description"],
|
||||
price=float(row["price"]),
|
||||
is_active=bool(row["is_active"]),
|
||||
)
|
||||
|
||||
|
||||
@router.get("", response_model=List[RitualService])
|
||||
def list_rituals(db: Database, temple_id: TempleId) -> List[RitualService]:
|
||||
ensure_temple_exists(db, temple_id)
|
||||
rows = db.execute(
|
||||
"""
|
||||
SELECT id, temple_id, name, description, price, is_active
|
||||
FROM ritual_services
|
||||
WHERE temple_id = ?
|
||||
ORDER BY id
|
||||
""",
|
||||
(temple_id,),
|
||||
).fetchall()
|
||||
return [row_to_ritual(row) for row in rows]
|
||||
|
||||
|
||||
@router.post("", response_model=RitualService, status_code=status.HTTP_201_CREATED)
|
||||
def create_ritual(payload: RitualServiceCreate, db: Database, temple_id: TempleId) -> RitualService:
|
||||
ensure_temple_exists(db, temple_id)
|
||||
cursor = db.execute(
|
||||
"""
|
||||
INSERT INTO ritual_services (temple_id, name, description, price, is_active)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""",
|
||||
(temple_id, payload.name, payload.description, payload.price, int(payload.is_active)),
|
||||
)
|
||||
db.commit()
|
||||
return get_ritual(cursor.lastrowid, db, temple_id)
|
||||
|
||||
|
||||
@router.get("/{ritual_id}", response_model=RitualService)
|
||||
def get_ritual(ritual_id: int, db: Database, temple_id: TempleId) -> RitualService:
|
||||
ensure_temple_exists(db, temple_id)
|
||||
row = db.execute(
|
||||
"""
|
||||
SELECT id, temple_id, name, description, price, is_active
|
||||
FROM ritual_services
|
||||
WHERE id = ? AND temple_id = ?
|
||||
""",
|
||||
(ritual_id, temple_id),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Ritual service not found")
|
||||
return row_to_ritual(row)
|
||||
|
||||
|
||||
@router.patch("/{ritual_id}", response_model=RitualService)
|
||||
def update_ritual(ritual_id: int, payload: RitualServiceUpdate, db: Database, temple_id: TempleId) -> RitualService:
|
||||
ritual = get_ritual(ritual_id, db, temple_id)
|
||||
updates = payload.model_dump(exclude_unset=True)
|
||||
|
||||
if not updates:
|
||||
return ritual
|
||||
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE ritual_services
|
||||
SET name = ?, description = ?, price = ?, is_active = ?
|
||||
WHERE id = ? AND temple_id = ?
|
||||
""",
|
||||
(
|
||||
updates.get("name", ritual.name),
|
||||
updates.get("description", ritual.description),
|
||||
updates.get("price", ritual.price),
|
||||
int(updates.get("is_active", ritual.is_active)),
|
||||
ritual_id,
|
||||
temple_id,
|
||||
),
|
||||
)
|
||||
db.commit()
|
||||
return get_ritual(ritual_id, db, temple_id)
|
||||
|
||||
|
||||
@router.delete("/{ritual_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_ritual(ritual_id: int, db: Database, temple_id: TempleId) -> None:
|
||||
ensure_temple_exists(db, temple_id)
|
||||
cursor = db.execute(
|
||||
"DELETE FROM ritual_services WHERE id = ? AND temple_id = ?",
|
||||
(ritual_id, temple_id),
|
||||
)
|
||||
db.commit()
|
||||
if cursor.rowcount == 0:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Ritual service not found")
|
||||
Reference in New Issue
Block a user