uso fixo de json e troca do banco para postgresql
This commit is contained in:
Binary file not shown.
@@ -27,10 +27,10 @@ def read_games(skip: int = 0, limit: int = 100, genre: Optional[str] = None, pla
|
||||
query = db.query(Game)
|
||||
|
||||
if genre:
|
||||
query = query.filter(Game.genres.cast(String).ilike(f'%"{genre}"%'))
|
||||
query = query.filter(Game.genres.contains([genre]))
|
||||
|
||||
if platform:
|
||||
query = query.filter(Game.platforms.cast(String).ilike(f'%"{platform}"%'))
|
||||
query = query.filter(Game.platforms.contains([platform]))
|
||||
|
||||
games = query.offset(skip).limit(limit).all()
|
||||
data = [GameResponse.model_validate(g).model_dump() for g in games]
|
||||
@@ -47,7 +47,7 @@ def create_game(game: GameCreate, db: Session = Depends(get_db)):
|
||||
raise HTTPException(status_code=400, detail="Um jogo com este título/slug já existe.")
|
||||
|
||||
# Cria o modelo no DB
|
||||
game_data = game.model_dump()
|
||||
game_data = game.model_dump(mode='json')
|
||||
db_game = Game(**game_data, slug=slug)
|
||||
|
||||
db.add(db_game)
|
||||
@@ -74,7 +74,7 @@ def update_game(id: str, game: GameUpdate, db: Session = Depends(get_db)):
|
||||
if not db_game:
|
||||
raise HTTPException(status_code=404, detail="Jogo não encontrado.")
|
||||
|
||||
update_data = game.model_dump(exclude_unset=True)
|
||||
update_data = game.model_dump(mode='json', exclude_unset=True)
|
||||
|
||||
if "title" in update_data:
|
||||
new_slug = generate_slug(update_data["title"])
|
||||
|
||||
Binary file not shown.
@@ -1,4 +1,5 @@
|
||||
from sqlalchemy import Column, String, Boolean, JSON
|
||||
from sqlalchemy import Column, String, Boolean
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from app.db.database import Base
|
||||
import uuid
|
||||
|
||||
@@ -12,9 +13,8 @@ class Game(Base):
|
||||
developer = Column(String)
|
||||
active = Column(Boolean, default=True)
|
||||
|
||||
# Usando JSON para simplificar arrays no MVP conforme os requisitos.
|
||||
# Em um banco relacional robusto, genres e platforms seriam tabelas M:N.
|
||||
genres = Column(JSON)
|
||||
platforms = Column(JSON)
|
||||
images = Column(JSON)
|
||||
system_requirements = Column(JSON)
|
||||
# Usando JSONB para melhor performance e suporte a buscas nativas no PostgreSQL
|
||||
genres = Column(JSONB)
|
||||
platforms = Column(JSONB)
|
||||
images = Column(JSONB)
|
||||
system_requirements = Column(JSONB)
|
||||
|
||||
Reference in New Issue
Block a user