This commit is contained in:
Waylon Walker 2023-06-09 16:04:58 -05:00
parent 1a0bf1adb9
commit c3db85a209
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
21 changed files with 647 additions and 658 deletions

View file

@ -1,18 +1,9 @@
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import SQLModel, Session
from fastapi import APIRouter, Depends
from sqlmodel import SQLModel
from learn_sql_model.api.user import oauth2_scheme
from learn_sql_model.api.websocket_connection_manager import manager
from learn_sql_model.config import Config, get_config
from learn_sql_model.models.hero import (
Hero,
HeroCreate,
HeroDelete,
HeroRead,
HeroUpdate,
)
from learn_sql_model.config import get_config, get_session
from learn_sql_model.models.hero import Hero, HeroCreate, HeroRead, HeroUpdate
hero_router = APIRouter()
@ -22,52 +13,73 @@ def on_startup() -> None:
SQLModel.metadata.create_all(get_config().database.engine)
@hero_router.get("/items/")
async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
return {"token": token}
@hero_router.get("/hero/{id}")
async def get_hero(id: int, config: Config = Depends(get_config)) -> Hero:
@hero_router.get("/hero/{hero_id}")
async def get_hero(
*,
session: Session = Depends(get_session),
hero_id: int,
) -> HeroRead:
"get one hero"
return Hero().get(id=id, config=config)
@hero_router.get("/h/{id}")
async def get_h(id: int, config: Config = Depends(get_config)) -> Hero:
"get one hero"
return Hero().get(id=id, config=config)
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
return hero
@hero_router.post("/hero/")
async def post_hero(hero: HeroCreate) -> HeroRead:
async def post_hero(
*,
session: Session = Depends(get_session),
hero: HeroCreate,
) -> HeroRead:
"read all the heros"
config = get_config()
hero = hero.post(config=config)
db_hero = Hero.from_orm(hero)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
await manager.broadcast({hero.json()}, id=1)
return hero
return db_hero
@hero_router.patch("/hero/")
async def patch_hero(hero: HeroUpdate) -> HeroRead:
async def patch_hero(
*,
session: Session = Depends(get_session),
hero: HeroUpdate,
) -> HeroRead:
"read all the heros"
config = get_config()
hero = hero.update(config=config)
db_hero = session.get(Hero, hero.id)
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
for key, value in hero.dict(exclude_unset=True).items():
setattr(db_hero, key, value)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
await manager.broadcast({hero.json()}, id=1)
return hero
return db_hero
@hero_router.delete("/hero/{hero_id}")
async def delete_hero(hero_id: int):
async def delete_hero(
*,
session: Session = Depends(get_session),
hero_id: int,
):
"read all the heros"
hero = HeroDelete(id=hero_id)
config = get_config()
hero = hero.delete(config=config)
hero = session.get(Hero, hero_id)
if not hero:
raise HTTPException(status_code=404, detail="Hero not found")
session.delete(hero)
session.commit()
await manager.broadcast(f"deleted hero {hero_id}", id=1)
return hero
return {"ok": True}
@hero_router.get("/heros/")
async def get_heros(config: Config = Depends(get_config)) -> list[Hero]:
async def get_heros(
*,
session: Session = Depends(get_session),
) -> list[Hero]:
"get all heros"
return Hero().get(config=config)
return HeroRead.list(session=session)

View file

@ -1,45 +0,0 @@
from typing import Annotated
from fastapi import APIRouter, Depends
from sqlmodel import SQLModel
from learn_sql_model.api.user import oauth2_scheme
from learn_sql_model.config import Config, get_config
from learn_sql_model.models.new import new
new_router = APIRouter()
@new_router.on_event("startup")
def on_startup() -> None:
SQLModel.metadata.create_all(get_config().database.engine)
@new_router.get("/items/")
async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
return {"token": token}
@new_router.get("/new/{id}")
def get_new(id: int, config: Config = Depends(get_config)) -> new:
"get one new"
return new().get(id=id, config=config)
@new_router.get("/h/{id}")
def get_h(id: int, config: Config = Depends(get_config)) -> new:
"get one new"
return new().get(id=id, config=config)
@new_router.post("/new/")
def post_new(new: new, config: Config = Depends(get_config)) -> new:
"read all the news"
new.post(config=config)
return new
@new_router.get("/news/")
def get_news(config: Config = Depends(get_config)) -> list[new]:
"get all news"
return new().get(config=config)

View file

@ -69,4 +69,4 @@ async def websocket_endpoint(websocket: WebSocket):
except WebSocketDisconnect:
manager.disconnect(websocket, id)
await manager.broadcast(f"Client #{client_id} left the chat", id)
await manager.broadcast(f"Client #{id} left the chat", id)