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)
|
query = db.query(Game)
|
||||||
|
|
||||||
if genre:
|
if genre:
|
||||||
query = query.filter(Game.genres.cast(String).ilike(f'%"{genre}"%'))
|
query = query.filter(Game.genres.contains([genre]))
|
||||||
|
|
||||||
if platform:
|
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()
|
games = query.offset(skip).limit(limit).all()
|
||||||
data = [GameResponse.model_validate(g).model_dump() for g in games]
|
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.")
|
raise HTTPException(status_code=400, detail="Um jogo com este título/slug já existe.")
|
||||||
|
|
||||||
# Cria o modelo no DB
|
# 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_game = Game(**game_data, slug=slug)
|
||||||
|
|
||||||
db.add(db_game)
|
db.add(db_game)
|
||||||
@@ -74,7 +74,7 @@ def update_game(id: str, game: GameUpdate, db: Session = Depends(get_db)):
|
|||||||
if not db_game:
|
if not db_game:
|
||||||
raise HTTPException(status_code=404, detail="Jogo não encontrado.")
|
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:
|
if "title" in update_data:
|
||||||
new_slug = generate_slug(update_data["title"])
|
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
|
from app.db.database import Base
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
@@ -12,9 +13,8 @@ class Game(Base):
|
|||||||
developer = Column(String)
|
developer = Column(String)
|
||||||
active = Column(Boolean, default=True)
|
active = Column(Boolean, default=True)
|
||||||
|
|
||||||
# Usando JSON para simplificar arrays no MVP conforme os requisitos.
|
# Usando JSONB para melhor performance e suporte a buscas nativas no PostgreSQL
|
||||||
# Em um banco relacional robusto, genres e platforms seriam tabelas M:N.
|
genres = Column(JSONB)
|
||||||
genres = Column(JSON)
|
platforms = Column(JSONB)
|
||||||
platforms = Column(JSON)
|
images = Column(JSONB)
|
||||||
images = Column(JSON)
|
system_requirements = Column(JSONB)
|
||||||
system_requirements = Column(JSON)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user