basic profile
This commit is contained in:
BIN
packages/backend/app/__pycache__/crud.cpython-311.pyc
Normal file
BIN
packages/backend/app/__pycache__/crud.cpython-311.pyc
Normal file
Binary file not shown.
BIN
packages/backend/app/__pycache__/database.cpython-311.pyc
Normal file
BIN
packages/backend/app/__pycache__/database.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
packages/backend/app/__pycache__/models.cpython-311.pyc
Normal file
BIN
packages/backend/app/__pycache__/models.cpython-311.pyc
Normal file
Binary file not shown.
BIN
packages/backend/app/__pycache__/schemas.cpython-311.pyc
Normal file
BIN
packages/backend/app/__pycache__/schemas.cpython-311.pyc
Normal file
Binary file not shown.
37
packages/backend/app/crud.py
Normal file
37
packages/backend/app/crud.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from . import models, schemas
|
||||
|
||||
def get_celebrity(db: Session, celebrity_id: int):
|
||||
return db.query(models.Celebrity).filter(models.Celebrity.id == celebrity_id).first()
|
||||
|
||||
def get_celebrities(db: Session, skip: int = 0, limit: int = 100):
|
||||
return db.query(models.Celebrity).offset(skip).limit(limit).all()
|
||||
|
||||
def create_celebrity(db: Session, celebrity: schemas.CelebrityCreate):
|
||||
db_celebrity = models.Celebrity(**celebrity.model_dump())
|
||||
db.add(db_celebrity)
|
||||
db.commit()
|
||||
db.refresh(db_celebrity)
|
||||
return db_celebrity
|
||||
|
||||
def update_celebrity(db: Session, celebrity_id: int, celebrity_update: schemas.CelebrityUpdate):
|
||||
db_celebrity = get_celebrity(db, celebrity_id)
|
||||
if not db_celebrity:
|
||||
return None
|
||||
|
||||
update_data = celebrity_update.model_dump(exclude_unset=True)
|
||||
for key, value in update_data.items():
|
||||
setattr(db_celebrity, key, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(db_celebrity)
|
||||
return db_celebrity
|
||||
|
||||
def delete_celebrity(db: Session, celebrity_id: int):
|
||||
db_celebrity = get_celebrity(db, celebrity_id)
|
||||
if not db_celebrity:
|
||||
return None
|
||||
|
||||
db.delete(db_celebrity)
|
||||
db.commit()
|
||||
return db_celebrity
|
||||
25
packages/backend/app/database.py
Normal file
25
packages/backend/app/database.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os
|
||||
from sqlalchemy import create_engine
|
||||
# MODIFICA: La best practice moderna è usare DeclarativeBase
|
||||
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||
|
||||
engine = create_engine(DATABASE_URL)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
# Assicurati che Base sia definito così
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
# Dependency per ottenere una sessione DB per ogni richiesta
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -1,7 +1,11 @@
|
||||
from fastapi import FastAPI
|
||||
from .routers import celebrities # Importa il nuovo router
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# Includi il router delle celebrities nell'app principale
|
||||
app.include_router(celebrities.router)
|
||||
|
||||
@app.get("/api")
|
||||
def read_root():
|
||||
return {"message": "Ciao dal backend FastAPI!"}
|
||||
|
||||
91
packages/backend/app/models.py
Normal file
91
packages/backend/app/models.py
Normal file
@@ -0,0 +1,91 @@
|
||||
import enum
|
||||
from sqlalchemy import (Column, Integer, String, Date, Enum, Boolean,
|
||||
DECIMAL, Text, TIMESTAMP, ForeignKey)
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship # Aggiungi questo import
|
||||
|
||||
from .database import Base
|
||||
|
||||
# Definiamo gli Enum Python corrispondenti ai tipi custom di PostgreSQL
|
||||
class GenderType(str, enum.Enum):
|
||||
male = "male"
|
||||
female = "female"
|
||||
other = "other"
|
||||
|
||||
class ShoeSystemType(str, enum.Enum):
|
||||
EU = "EU"
|
||||
US = "US"
|
||||
UK = "UK"
|
||||
|
||||
class BraSystemType(str, enum.Enum):
|
||||
US = "US"
|
||||
UK = "UK"
|
||||
EU = "EU"
|
||||
FR = "FR"
|
||||
AU = "AU"
|
||||
IT = "IT"
|
||||
JP = "JP"
|
||||
|
||||
class Image(Base):
|
||||
__tablename__ = "images"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
celebrity_id = Column(Integer, ForeignKey("celebrities.id"), nullable=False)
|
||||
file_path = Column(String, nullable=False)
|
||||
caption = Column(Text, nullable=True)
|
||||
uploaded_at = Column(TIMESTAMP(timezone=True), server_default=func.now())
|
||||
|
||||
# Relazione inversa: "questa immagine appartiene a UNA celebrità".
|
||||
# Specifichiamo che deve usare la colonna `celebrity_id` di QUESTA tabella.
|
||||
celebrity = relationship("Celebrity", back_populates="images", foreign_keys=[celebrity_id])
|
||||
|
||||
|
||||
class Celebrity(Base):
|
||||
__tablename__ = "celebrities"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, unique=True, nullable=False, index=True)
|
||||
gender = Column(Enum(GenderType), nullable=False)
|
||||
birth_date = Column(Date, nullable=True)
|
||||
birth_place = Column(String, nullable=True)
|
||||
nationality = Column(String, nullable=True)
|
||||
ethnicity = Column(String, nullable=True)
|
||||
sexuality = Column(String, nullable=True)
|
||||
|
||||
hair_color = Column(String, nullable=True)
|
||||
eye_color = Column(String, nullable=True)
|
||||
height_cm = Column(Integer, nullable=True)
|
||||
weight_kg = Column(Integer, nullable=True)
|
||||
body_type = Column(String, nullable=True)
|
||||
|
||||
bust_cm = Column(Integer, nullable=True)
|
||||
waist_cm = Column(Integer, nullable=True)
|
||||
hips_cm = Column(Integer, nullable=True)
|
||||
|
||||
chest_circumference_cm = Column(Integer, nullable=True)
|
||||
|
||||
bra_band_size = Column(Integer, nullable=True)
|
||||
bra_cup_size = Column(String, nullable=True)
|
||||
bra_size_system = Column(Enum(BraSystemType), nullable=True)
|
||||
|
||||
boobs_are_natural = Column(Boolean, nullable=True)
|
||||
shoe_size = Column(DECIMAL(4, 1), nullable=True)
|
||||
shoe_size_system = Column(Enum(ShoeSystemType), nullable=True)
|
||||
|
||||
biography = Column(Text, nullable=True)
|
||||
official_website = Column(String, nullable=True)
|
||||
# 1. Relazione "una celebrità ha MOLTE immagini".
|
||||
# SQLAlchemy deve usare la FK che si trova nella tabella Image.
|
||||
images = relationship("Image", back_populates="celebrity", foreign_keys="[Image.celebrity_id]")
|
||||
# Chiave esterna per l'immagine del profilo
|
||||
profile_image_id = Column(Integer, ForeignKey("images.id", ondelete="SET NULL"), nullable=True)
|
||||
# 2. Relazione "una celebrità ha UNA immagine del profilo".
|
||||
# SQLAlchemy deve usare la FK che si trova in QUESTA tabella (celebrities).
|
||||
profile_image = relationship("Image", foreign_keys=[profile_image_id])
|
||||
|
||||
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now())
|
||||
updated_at = Column(TIMESTAMP(timezone=True), default=func.now(), onupdate=func.now())
|
||||
|
||||
# Nota: le altre tabelle (images, tattoos, etc.) andrebbero modellate qui
|
||||
# se si volessero gestire le relazioni in modo completo con SQLAlchemy.
|
||||
# Per questo esempio, ci concentriamo solo sulla tabella `celebrities`.
|
||||
Binary file not shown.
43
packages/backend/app/routers/celebrities.py
Normal file
43
packages/backend/app/routers/celebrities.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from .. import crud, models, schemas
|
||||
from ..database import get_db
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/celebrities",
|
||||
tags=["celebrities"],
|
||||
responses={404: {"description": "Not found"}},
|
||||
)
|
||||
|
||||
@router.post("/", response_model=schemas.Celebrity, status_code=201)
|
||||
def create_celebrity(celebrity: schemas.CelebrityCreate, db: Session = Depends(get_db)):
|
||||
# Qui potresti aggiungere un check per vedere se una celebrità con lo stesso nome esiste già
|
||||
return crud.create_celebrity(db=db, celebrity=celebrity)
|
||||
|
||||
@router.get("/", response_model=List[schemas.Celebrity])
|
||||
def read_celebrities(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
|
||||
celebrities = crud.get_celebrities(db, skip=skip, limit=limit)
|
||||
return celebrities
|
||||
|
||||
@router.get("/{celebrity_id}", response_model=schemas.Celebrity)
|
||||
def read_celebrity(celebrity_id: int, db: Session = Depends(get_db)):
|
||||
db_celebrity = crud.get_celebrity(db, celebrity_id=celebrity_id)
|
||||
if db_celebrity is None:
|
||||
raise HTTPException(status_code=404, detail="Celebrity not found")
|
||||
return db_celebrity
|
||||
|
||||
@router.put("/{celebrity_id}", response_model=schemas.Celebrity)
|
||||
def update_celebrity(celebrity_id: int, celebrity: schemas.CelebrityUpdate, db: Session = Depends(get_db)):
|
||||
db_celebrity = crud.update_celebrity(db, celebrity_id=celebrity_id, celebrity_update=celebrity)
|
||||
if db_celebrity is None:
|
||||
raise HTTPException(status_code=404, detail="Celebrity not found")
|
||||
return db_celebrity
|
||||
|
||||
@router.delete("/{celebrity_id}", response_model=schemas.Celebrity)
|
||||
def delete_celebrity(celebrity_id: int, db: Session = Depends(get_db)):
|
||||
db_celebrity = crud.delete_celebrity(db, celebrity_id=celebrity_id)
|
||||
if db_celebrity is None:
|
||||
raise HTTPException(status_code=404, detail="Celebrity not found")
|
||||
return db_celebrity
|
||||
73
packages/backend/app/schemas.py
Normal file
73
packages/backend/app/schemas.py
Normal file
@@ -0,0 +1,73 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from datetime import date, datetime
|
||||
from .models import GenderType, ShoeSystemType, BraSystemType
|
||||
|
||||
# Schema di base con i campi comuni
|
||||
class CelebrityBase(BaseModel):
|
||||
name: str
|
||||
gender: GenderType
|
||||
birth_date: Optional[date] = None
|
||||
birth_place: Optional[str] = None
|
||||
nationality: Optional[str] = None
|
||||
ethnicity: Optional[str] = None
|
||||
sexuality: Optional[str] = None
|
||||
hair_color: Optional[str] = None
|
||||
eye_color: Optional[str] = None
|
||||
height_cm: Optional[int] = None
|
||||
weight_kg: Optional[int] = None
|
||||
body_type: Optional[str] = None
|
||||
bust_cm: Optional[int] = None
|
||||
waist_cm: Optional[int] = None
|
||||
hips_cm: Optional[int] = None
|
||||
chest_circumference_cm: Optional[int] = None
|
||||
bra_band_size: Optional[int] = None
|
||||
bra_cup_size: Optional[str] = None
|
||||
bra_size_system: Optional[BraSystemType] = None
|
||||
boobs_are_natural: Optional[bool] = None
|
||||
shoe_size: Optional[float] = None
|
||||
shoe_size_system: Optional[ShoeSystemType] = None
|
||||
biography: Optional[str] = None
|
||||
official_website: Optional[str] = None
|
||||
profile_image_id: Optional[int] = None
|
||||
|
||||
# Schema per la creazione di una nuova celebrità (eredita da Base)
|
||||
class CelebrityCreate(CelebrityBase):
|
||||
pass
|
||||
|
||||
# Schema per l'aggiornamento (tutti i campi sono opzionali)
|
||||
class CelebrityUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
gender: Optional[GenderType] = None
|
||||
birth_date: Optional[date] = None
|
||||
birth_place: Optional[str] = None
|
||||
nationality: Optional[str] = None
|
||||
ethnicity: Optional[str] = None
|
||||
sexuality: Optional[str] = None
|
||||
hair_color: Optional[str] = None
|
||||
eye_color: Optional[str] = None
|
||||
height_cm: Optional[int] = None
|
||||
weight_kg: Optional[int] = None
|
||||
body_type: Optional[str] = None
|
||||
bust_cm: Optional[int] = None
|
||||
waist_cm: Optional[int] = None
|
||||
hips_cm: Optional[int] = None
|
||||
chest_circumference_cm: Optional[int] = None
|
||||
bra_band_size: Optional[int] = None
|
||||
bra_cup_size: Optional[str] = None
|
||||
bra_size_system: Optional[BraSystemType] = None
|
||||
boobs_are_natural: Optional[bool] = None
|
||||
shoe_size: Optional[float] = None
|
||||
shoe_size_system: Optional[ShoeSystemType] = None
|
||||
biography: Optional[str] = None
|
||||
official_website: Optional[str] = None
|
||||
profile_image_id: Optional[int] = None
|
||||
|
||||
# Schema per la lettura dei dati (include campi generati dal DB)
|
||||
class Celebrity(CelebrityBase):
|
||||
id: int
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True # Permette a Pydantic di leggere dati da un modello ORM
|
||||
Reference in New Issue
Block a user