Coverage for docs_src/tutorial/fastapi/delete/tutorial001_py39.py: 100%
69 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 00:02 +0000
1from typing import Optional 1abcd
3from fastapi import FastAPI, HTTPException, Query 1abcd
4from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcd
7class HeroBase(SQLModel): 1abcd
8 name: str = Field(index=True) 1abcd
9 secret_name: str 1abcd
10 age: Optional[int] = Field(default=None, index=True) 1abcd
13class Hero(HeroBase, table=True): 1abcd
14 id: Optional[int] = Field(default=None, primary_key=True) 1abcd
17class HeroCreate(HeroBase): 1abcd
18 pass 1abcd
21class HeroPublic(HeroBase): 1abcd
22 id: int 1abcd
25class HeroUpdate(SQLModel): 1abcd
26 name: Optional[str] = None 1abcd
27 secret_name: Optional[str] = None 1abcd
28 age: Optional[int] = None 1abcd
31sqlite_file_name = "database.db" 1abcd
32sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcd
34connect_args = {"check_same_thread": False} 1abcd
35engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcd
38def create_db_and_tables(): 1abcd
39 SQLModel.metadata.create_all(engine) 1abcd
42app = FastAPI() 1abcd
45@app.on_event("startup") 1abcd
46def on_startup(): 1abcd
47 create_db_and_tables() 1abcd
50@app.post("/heroes/", response_model=HeroPublic) 1abcd
51def create_hero(hero: HeroCreate): 1abcd
52 with Session(engine) as session: 1abcd
53 db_hero = Hero.model_validate(hero) 1abcd
54 session.add(db_hero) 1abcd
55 session.commit() 1abcd
56 session.refresh(db_hero) 1abcd
57 return db_hero 1abcd
60@app.get("/heroes/", response_model=list[HeroPublic]) 1abcd
61def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)): 1abcd
62 with Session(engine) as session: 1abcd
63 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1abcd
64 return heroes 1abcd
67@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abcd
68def read_hero(hero_id: int): 1abcd
69 with Session(engine) as session: 1abcd
70 hero = session.get(Hero, hero_id) 1abcd
71 if not hero: 1abcd
72 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
73 return hero 1abcd
76@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcd
77def update_hero(hero_id: int, hero: HeroUpdate): 1abcd
78 with Session(engine) as session: 1abcd
79 db_hero = session.get(Hero, hero_id) 1abcd
80 if not db_hero: 1abcd
81 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
82 hero_data = hero.model_dump(exclude_unset=True) 1abcd
83 for key, value in hero_data.items(): 1abcd
84 setattr(db_hero, key, value) 1abcd
85 session.add(db_hero) 1abcd
86 session.commit() 1abcd
87 session.refresh(db_hero) 1abcd
88 return db_hero 1abcd
91@app.delete("/heroes/{hero_id}") 1abcd
92def delete_hero(hero_id: int): 1abcd
93 with Session(engine) as session: 1abcd
94 hero = session.get(Hero, hero_id) 1abcd
95 if not hero: 1abcd
96 raise HTTPException(status_code=404, detail="Hero not found") 1abcd
97 session.delete(hero) 1abcd
98 session.commit() 1abcd
99 return {"ok": True} 1abcd