Update code in endpoints/footbal.post.py
This commit is contained in:
parent
87a96ec4d8
commit
f1c260290f
57
repos/games-xxwif7/endpoints/footbal.post.py
Normal file
57
repos/games-xxwif7/endpoints/footbal.post.py
Normal file
@ -0,0 +1,57 @@
|
||||
```python
|
||||
from typing import List
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import Column, Integer, String, Boolean
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, index=True)
|
||||
description = Column(String)
|
||||
is_multiplayer = Column(Boolean, default=False)
|
||||
|
||||
games = [] # In-memory storage
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/football", status_code=201)
|
||||
async def create_game(game: GameSchema):
|
||||
"""Create a new game"""
|
||||
if request.method != "POST":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
db_game = Game(name=game.name, description=game.description, is_multiplayer=game.is_multiplayer)
|
||||
db.add(db_game)
|
||||
db.commit()
|
||||
db.refresh(db_game)
|
||||
|
||||
return {
|
||||
"method": "POST",
|
||||
"_verb": "post",
|
||||
"id": db_game.id,
|
||||
**game.dict()
|
||||
}
|
||||
|
||||
@router.get("/football", response_model=List[GameSchema])
|
||||
async def get_games():
|
||||
"""Get all games"""
|
||||
if request.method != "GET":
|
||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||
|
||||
games = db.query(Game).all()
|
||||
return games
|
||||
```
|
||||
|
||||
This code provides a `GameSchema` Pydantic model and a `Game` SQLAlchemy model for storing game data. The `create_game` endpoint allows creating a new game by sending a POST request to `/football` with the game details in the request body. The `get_games` endpoint retrieves all games from the database by sending a GET request to `/football`.
|
||||
|
||||
Note that this code assumes the existence of a SQLAlchemy database session `db`. You'll need to set up the database connection and session before using these endpoints.
|
Loading…
x
Reference in New Issue
Block a user