adição do metodo DELETE, correções adicionais
This commit is contained in:
Binary file not shown.
@@ -33,7 +33,7 @@ def read_games(skip: int = 0, limit: int = 100, genre: Optional[str] = None, pla
|
|||||||
query = query.filter(Game.platforms.contains([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(mode='json') for g in games]
|
||||||
|
|
||||||
return {"success": True, "message": "Lista de jogos", "data": data}
|
return {"success": True, "message": "Lista de jogos", "data": data}
|
||||||
|
|
||||||
@@ -59,16 +59,19 @@ def create_game(game: GameCreate, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
@router.get("/{id_ou_slug}", response_model=StandardResponse)
|
@router.get("/{id_ou_slug}", response_model=StandardResponse)
|
||||||
def read_game(id_ou_slug: str, db: Session = Depends(get_db)):
|
def read_game(id_ou_slug: str, db: Session = Depends(get_db)):
|
||||||
db_game = db.query(Game).filter(or_(Game.id == id_ou_slug, Game.slug == id_ou_slug)).first()
|
if id_ou_slug.isdigit():
|
||||||
|
db_game = db.query(Game).filter(Game.id == int(id_ou_slug)).first()
|
||||||
|
else:
|
||||||
|
db_game = db.query(Game).filter(Game.slug == id_ou_slug).first()
|
||||||
|
|
||||||
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.")
|
||||||
|
|
||||||
data = GameResponse.model_validate(db_game).model_dump()
|
data = GameResponse.model_validate(db_game).model_dump(mode='json')
|
||||||
return {"success": True, "message": "Detalhes do jogo", "data": data}
|
return {"success": True, "message": "Detalhes do jogo", "data": data}
|
||||||
|
|
||||||
@router.patch("/{id}", response_model=StandardResponse)
|
@router.patch("/{id}", response_model=StandardResponse)
|
||||||
def update_game(id: str, game: GameUpdate, db: Session = Depends(get_db)):
|
def update_game(id: int, game: GameUpdate, db: Session = Depends(get_db)):
|
||||||
db_game = db.query(Game).filter(Game.id == id).first()
|
db_game = db.query(Game).filter(Game.id == id).first()
|
||||||
|
|
||||||
if not db_game:
|
if not db_game:
|
||||||
@@ -91,5 +94,20 @@ def update_game(id: str, game: GameUpdate, db: Session = Depends(get_db)):
|
|||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_game)
|
db.refresh(db_game)
|
||||||
|
|
||||||
data = GameResponse.model_validate(db_game).model_dump()
|
data = GameResponse.model_validate(db_game).model_dump(mode='json')
|
||||||
return {"success": True, "message": "Jogo atualizado com sucesso", "data": data}
|
return {"success": True, "message": "Jogo atualizado com sucesso", "data": data}
|
||||||
|
|
||||||
|
@router.delete("/{id_ou_slug}", response_model=StandardResponse)
|
||||||
|
def delete_game(id_ou_slug: str, db: Session = Depends(get_db)):
|
||||||
|
if id_ou_slug.isdigit():
|
||||||
|
db_game = db.query(Game).filter(Game.id == int(id_ou_slug)).first()
|
||||||
|
else:
|
||||||
|
db_game = db.query(Game).filter(Game.slug == id_ou_slug).first()
|
||||||
|
|
||||||
|
if not db_game:
|
||||||
|
raise HTTPException(status_code=404, detail="Jogo não encontrado.")
|
||||||
|
|
||||||
|
db.delete(db_game)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return {"success": True, "message": "Jogo removido com sucesso", "data": None}
|
||||||
|
|||||||
Binary file not shown.
@@ -1,12 +1,11 @@
|
|||||||
from sqlalchemy import Column, String, Boolean
|
from sqlalchemy import Column, String, Boolean, Integer
|
||||||
from sqlalchemy.dialects.postgresql import JSONB
|
from sqlalchemy.dialects.postgresql import JSONB
|
||||||
from app.db.database import Base
|
from app.db.database import Base
|
||||||
import uuid
|
|
||||||
|
|
||||||
class Game(Base):
|
class Game(Base):
|
||||||
__tablename__ = "games"
|
__tablename__ = "games"
|
||||||
|
|
||||||
id = Column(String, primary_key=True, default=lambda: f"gv-{uuid.uuid4().hex[:8]}")
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
title = Column(String, index=True, nullable=False)
|
title = Column(String, index=True, nullable=False)
|
||||||
slug = Column(String, unique=True, index=True, nullable=False)
|
slug = Column(String, unique=True, index=True, nullable=False)
|
||||||
description = Column(String)
|
description = Column(String)
|
||||||
|
|||||||
Binary file not shown.
@@ -34,7 +34,7 @@ class GameUpdate(BaseModel):
|
|||||||
active: Optional[bool] = None
|
active: Optional[bool] = None
|
||||||
|
|
||||||
class GameResponse(GameBase):
|
class GameResponse(GameBase):
|
||||||
id: str
|
id: int
|
||||||
slug: str
|
slug: str
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
|||||||
Reference in New Issue
Block a user