- Backend FastAPI con multi-LLM (Claude/OpenAI/Gemini) - Publishing su Facebook, Instagram, YouTube, TikTok - Calendario editoriale con awareness levels (PAS, AIDA, BAB...) - Design system Editorial Fresh (Fraunces + DM Sans) - Scheduler automatico, gestione commenti AI, affiliate links Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
"""Affiliate links CRUD router.
|
|
|
|
Manages affiliate links that can be injected into generated content.
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..auth import get_current_user
|
|
from ..database import get_db
|
|
from ..models import AffiliateLink
|
|
from ..schemas import AffiliateLinkCreate, AffiliateLinkResponse, AffiliateLinkUpdate
|
|
|
|
router = APIRouter(
|
|
prefix="/api/affiliates",
|
|
tags=["affiliates"],
|
|
dependencies=[Depends(get_current_user)],
|
|
)
|
|
|
|
|
|
@router.get("/", response_model=list[AffiliateLinkResponse])
|
|
def list_affiliate_links(
|
|
character_id: int | None = Query(None),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""List all affiliate links, optionally filtered by character."""
|
|
query = db.query(AffiliateLink)
|
|
if character_id is not None:
|
|
query = query.filter(AffiliateLink.character_id == character_id)
|
|
return query.order_by(AffiliateLink.created_at.desc()).all()
|
|
|
|
|
|
@router.get("/{link_id}", response_model=AffiliateLinkResponse)
|
|
def get_affiliate_link(link_id: int, db: Session = Depends(get_db)):
|
|
"""Get a single affiliate link by ID."""
|
|
link = db.query(AffiliateLink).filter(AffiliateLink.id == link_id).first()
|
|
if not link:
|
|
raise HTTPException(status_code=404, detail="Affiliate link not found")
|
|
return link
|
|
|
|
|
|
@router.post("/", response_model=AffiliateLinkResponse, status_code=201)
|
|
def create_affiliate_link(data: AffiliateLinkCreate, db: Session = Depends(get_db)):
|
|
"""Create a new affiliate link."""
|
|
link = AffiliateLink(**data.model_dump())
|
|
db.add(link)
|
|
db.commit()
|
|
db.refresh(link)
|
|
return link
|
|
|
|
|
|
@router.put("/{link_id}", response_model=AffiliateLinkResponse)
|
|
def update_affiliate_link(
|
|
link_id: int, data: AffiliateLinkUpdate, db: Session = Depends(get_db)
|
|
):
|
|
"""Update an affiliate link."""
|
|
link = db.query(AffiliateLink).filter(AffiliateLink.id == link_id).first()
|
|
if not link:
|
|
raise HTTPException(status_code=404, detail="Affiliate link not found")
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
for key, value in update_data.items():
|
|
setattr(link, key, value)
|
|
db.commit()
|
|
db.refresh(link)
|
|
return link
|
|
|
|
|
|
@router.delete("/{link_id}", status_code=204)
|
|
def delete_affiliate_link(link_id: int, db: Session = Depends(get_db)):
|
|
"""Delete an affiliate link."""
|
|
link = db.query(AffiliateLink).filter(AffiliateLink.id == link_id).first()
|
|
if not link:
|
|
raise HTTPException(status_code=404, detail="Affiliate link not found")
|
|
db.delete(link)
|
|
db.commit()
|