Update code in endpoints/basketball.post.py
This commit is contained in:
parent
ce046e7fad
commit
68f3d9698d
81
repos/games-xxwif7/endpoints/basketball.post.py
Normal file
81
repos/games-xxwif7/endpoints/basketball.post.py
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
```python
|
||||||
|
from typing import List
|
||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from sqlalchemy import Column, Integer, String, ForeignKey
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class GameCreate(GameBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GameUpdate(GameBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GameInDBBase(GameBase):
|
||||||
|
id: int
|
||||||
|
owner_id: int
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
class GameInDB(GameInDBBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, index=True)
|
||||||
|
description = Column(String, index=True)
|
||||||
|
price = Column(Integer)
|
||||||
|
owner_id = Column(Integer, ForeignKey("users.id"))
|
||||||
|
|
||||||
|
owner = relationship("User", back_populates="games")
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException, Depends
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from database import get_db
|
||||||
|
from models import Game
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
@router.post("/basketball", response_model=GameInDB)
|
||||||
|
async def create_game(
|
||||||
|
game: GameCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
if request.method != "POST":
|
||||||
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
|
db_game = Game(**game.dict(), owner_id=current_user.id)
|
||||||
|
db.add(db_game)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(db_game)
|
||||||
|
return {
|
||||||
|
"method": "POST",
|
||||||
|
"_verb": "post",
|
||||||
|
**db_game.__dict__
|
||||||
|
}
|
||||||
|
|
||||||
|
@router.get("/basketball", response_model=List[GameInDB])
|
||||||
|
async def get_games(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: User = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
games = db.query(Game).filter(Game.owner_id == current_user.id).all()
|
||||||
|
return [{
|
||||||
|
"method": "GET",
|
||||||
|
"_verb": "get",
|
||||||
|
**game.__dict__
|
||||||
|
} for game in games]
|
||||||
|
```
|
||||||
|
|
||||||
|
This code provides a FastAPI endpoint for creating and fetching games in a database. It includes Pydantic models for data validation, SQLAlchemy models for database operations, and the necessary API routes with proper method handling and responses. The `GameBase` class defines the common fields for a game, while `GameCreate` and `GameUpdate` inherit from it for create and update operations, respectively. The `GameInDB` class represents the game data returned from the database, including the ID and owner ID fields.
|
||||||
|
|
||||||
|
The `create_game` route handles POST requests to `/basketball` and creates a new game in the database associated with the current user. The `get_games` route retrieves all games owned by the current user.
|
||||||
|
|
||||||
|
Note: This code assumes the existence of a `User` model, a `get_current_user` dependency, and a `get_db` dependency for obtaining a database session. You may need to define these components based on your project's requirements.
|
Loading…
x
Reference in New Issue
Block a user