diff --git a/repos/new-project-0ekspv/endpoints/new-project-0ekspv.post.py b/repos/new-project-0ekspv/endpoints/new-project-0ekspv.post.py new file mode 100644 index 0000000..76a31fc --- /dev/null +++ b/repos/new-project-0ekspv/endpoints/new-project-0ekspv.post.py @@ -0,0 +1,67 @@ +```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) + description = Column(String) + is_multiplayer = Column(Boolean) + +games = [] # In-memory storage + +from fastapi import APIRouter, HTTPException + +router = APIRouter() + +@router.post("/new-project-0ekspv") +async def save_game(game: GameSchema): + """Save a new game to the database""" + if request.method != "POST": + raise HTTPException(status_code=405, detail="Method Not Allowed") + + db_game = GameModel( + 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", + "message": "Game created successfully", + "game": { + "id": db_game.id, + "name": db_game.name, + "description": db_game.description, + "is_multiplayer": db_game.is_multiplayer + } + } + +@router.get("/games", response_model=List[GameSchema]) +async def get_games(): + """Fetch all games from the database""" + games = db.query(GameModel).all() + return games +``` + +This code provides a `GameSchema` Pydantic model for data validation, a `GameModel` SQLAlchemy model for database operations, and two FastAPI endpoints: one for saving a new game (`/new-project-0ekspv`) and another for fetching all games (`/games`). + +The `save_game` endpoint expects a `GameSchema` object in the request body, creates a `GameModel` instance from it, saves it to the database, and returns the created game object with metadata. + +The `get_games` endpoint fetches all games from the database and returns them as a list of `GameSchema` objects. + +Note: This code assumes you have set up a SQLAlchemy database connection and engine, and that the `db` object is available in the scope of these functions. \ No newline at end of file