110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
import json
|
|
from typing import List, Optional
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlmodel import Session, select
|
|
from app.database import get_session
|
|
from app.models.astuce import Astuce
|
|
|
|
router = APIRouter(tags=["astuces"])
|
|
|
|
|
|
def _decode_tags(raw: Optional[str]) -> list[str]:
|
|
if not raw:
|
|
return []
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except Exception:
|
|
parsed = [p.strip() for p in raw.split(",") if p.strip()]
|
|
if not isinstance(parsed, list):
|
|
return []
|
|
return [str(x).strip().lower() for x in parsed if str(x).strip()]
|
|
|
|
|
|
def _decode_mois(raw: Optional[str]) -> list[int]:
|
|
if not raw:
|
|
return []
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except Exception:
|
|
parsed = [p.strip() for p in raw.split(",") if p.strip()]
|
|
if not isinstance(parsed, list):
|
|
return []
|
|
result: list[int] = []
|
|
for x in parsed:
|
|
try:
|
|
month = int(x)
|
|
if 1 <= month <= 12:
|
|
result.append(month)
|
|
except (TypeError, ValueError):
|
|
continue
|
|
return result
|
|
|
|
|
|
@router.get("/astuces", response_model=List[Astuce])
|
|
def list_astuces(
|
|
entity_type: Optional[str] = Query(None),
|
|
entity_id: Optional[int] = Query(None),
|
|
categorie: Optional[str] = Query(None),
|
|
tag: Optional[str] = Query(None),
|
|
mois: Optional[int] = Query(None, ge=1, le=12),
|
|
session: Session = Depends(get_session),
|
|
):
|
|
q = select(Astuce)
|
|
if entity_type:
|
|
q = q.where(Astuce.entity_type == entity_type)
|
|
if entity_id is not None:
|
|
q = q.where(Astuce.entity_id == entity_id)
|
|
|
|
if categorie:
|
|
q = q.where(Astuce.categorie == categorie)
|
|
|
|
items = session.exec(q).all()
|
|
|
|
if tag:
|
|
wanted = tag.strip().lower()
|
|
items = [a for a in items if wanted in _decode_tags(a.tags)]
|
|
|
|
if mois is not None:
|
|
# mois null/empty = astuce valable toute l'année
|
|
items = [a for a in items if not _decode_mois(a.mois) or mois in _decode_mois(a.mois)]
|
|
|
|
return items
|
|
|
|
|
|
@router.post("/astuces", response_model=Astuce, status_code=status.HTTP_201_CREATED)
|
|
def create_astuce(a: Astuce, session: Session = Depends(get_session)):
|
|
session.add(a)
|
|
session.commit()
|
|
session.refresh(a)
|
|
return a
|
|
|
|
|
|
@router.get("/astuces/{id}", response_model=Astuce)
|
|
def get_astuce(id: int, session: Session = Depends(get_session)):
|
|
a = session.get(Astuce, id)
|
|
if not a:
|
|
raise HTTPException(404, "Astuce introuvable")
|
|
return a
|
|
|
|
|
|
@router.put("/astuces/{id}", response_model=Astuce)
|
|
def update_astuce(id: int, data: Astuce, session: Session = Depends(get_session)):
|
|
a = session.get(Astuce, id)
|
|
if not a:
|
|
raise HTTPException(404, "Astuce introuvable")
|
|
for k, v in data.model_dump(exclude_unset=True, exclude={"id", "created_at"}).items():
|
|
setattr(a, k, v)
|
|
session.add(a)
|
|
session.commit()
|
|
session.refresh(a)
|
|
return a
|
|
|
|
|
|
@router.delete("/astuces/{id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
def delete_astuce(id: int, session: Session = Depends(get_session)):
|
|
a = session.get(Astuce, id)
|
|
if not a:
|
|
raise HTTPException(404, "Astuce introuvable")
|
|
session.delete(a)
|
|
session.commit()
|