|
from typing import Union,Optional |
|
from fastapi import FastAPI,Header,HTTPException |
|
from pydantic import BaseModel |
|
from typing_extensions import Annotated |
|
from sqlmodel import SQLModel,create_engine,Field,Session,update |
|
|
|
app = FastAPI() |
|
|
|
|
|
|
|
|
|
DATABASE_URL = "sqlite:///./sql_app.db" |
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread":False}) |
|
|
|
def creat_db_tables(): |
|
SQLModel.metadata.create_all(engine) |
|
|
|
class Users(SQLModel, table=True): |
|
id : Optional[int] = Field(default=None,primary_key = True) |
|
name : str |
|
city : str |
|
email : str |
|
|
|
users = { |
|
1 : {'id':1, |
|
"name":"John", |
|
"city":"Gandhinagar", |
|
"email":"john@gmail.com"}, |
|
|
|
2 : {'id':2, |
|
"name":"Emili", |
|
"city":"Surat", |
|
"email":"emili@gmail.com"}, |
|
|
|
3 : {'id':3, |
|
"name":"Miya", |
|
"city":"Bombay", |
|
"email":"miya@gmail.com"} |
|
} |
|
|
|
class User(BaseModel): |
|
id : int |
|
name : str |
|
city : str |
|
email : str |
|
|
|
class UserUpdate(BaseModel): |
|
name : str |
|
city : str |
|
email : str |
|
|
|
@app.on_event("startup") |
|
def on_startup(): |
|
creat_db_tables() |
|
|
|
@app.get("/") |
|
def index(): |
|
return {"Message" : "Hello World"} |
|
|
|
@app.get("/test") |
|
def test(): |
|
a = 5 |
|
return {"Message" : "Testing World", |
|
"Value" : a} |
|
|
|
@app.get("/users", status_code=200) |
|
def get_users(x_api_key: Annotated[Union[str,None], Header()], city:str=None): |
|
|
|
|
|
|
|
|
|
|
|
with Session(engine) as session: |
|
users = session.query(Users).all() |
|
print(f'users list {users}') |
|
return {"message": "Users list", "data":users,"header":x_api_key} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/users/{user_id}") |
|
def get_user_by_id(user_id:int): |
|
with Session(engine) as session: |
|
user = session.query(Users).filter(User.id == user_id).one_or_none() |
|
|
|
return {"message": "Users Details", "data": user} |
|
|
|
@app.post("/users",status_code=201) |
|
|
|
|
|
|
|
|
|
def create_user(user:Users): |
|
with Session(engine) as session: |
|
session.add(user) |
|
session.commit() |
|
session.refresh(user) |
|
return {"message":"New User", "data": user} |
|
|
|
@app.put("/users/{user_id}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_user(user_id:int, user:Users): |
|
with Session(engine) as session: |
|
user_exist = session.query(Users).filter(Users.id == user_id).one_or_none() |
|
if not user_exist: |
|
raise HTTPException(404, 'Invalid User ID') |
|
|
|
user_exist.name = user.name |
|
user_exist.city = user.city |
|
user_exist.email = user.email |
|
session.add(user_exist) |
|
session.commit() |
|
session.refresh(user_exist) |
|
return {"message":"User Updated Successfully","data":user_exist} |
|
|
|
|
|
|
|
@app.delete("/users/{user_id}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_user(user_id:int): |
|
with Session(engine) as session: |
|
user_exist = session.query(Users).filter(Users.id == user_id).one_or_none() |
|
if not user_exist: |
|
raise HTTPException(404, 'Invalid User ID') |
|
|
|
session.delete(user_exist) |
|
session.commit() |
|
return {"message":"User Delted Successfully", "data":user_exist} |