-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
98 lines (67 loc) · 3.1 KB
/
app.py
File metadata and controls
98 lines (67 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from fastapi import FastAPI , Depends , Request , HTTPException
from schemas import ToDoSchemain ,ToDoSchemaout
from sqlalchemy import create_engine , Column , String , Boolean , Integer
from sqlalchemy.orm import sessionmaker , Session
from sqlalchemy.ext.declarative import declarative_base
from typing import List
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
# SQLITE DB Settings
DATABASE_URL = "sqlite:///todo.db" # connection string
engine = create_engine(DATABASE_URL,connect_args={"check_same_thread":False})# make the connection
SessionLocal = sessionmaker(autoflush=False,autocommit=False,bind=engine) # open the connection
Base = declarative_base() #
# design db tables
class ToDoModel(Base):
__tablename__ = "todo"
id = Column(Integer,primary_key=True, autoincrement=True)
title = Column(String)
description = Column(String)
completed = Column(Boolean,default=False) # boolean 0 or 1
# create db tables
Base.metadata.create_all(bind=engine)
# function to create db session
def get_db():
db = SessionLocal()
return db
@app.get("/todos",response_model =List[ToDoSchemaout])
def todo_list(db:Session = Depends(get_db)):
return db.query(ToDoModel).all() # return all todos from sql-alchemy : select * from todo
@app.get("/todos/{todo_id}",response_model=ToDoSchemaout)
def todo_detail(todo_id:int,db:Session = Depends(get_db)):
single_todo = db.query(ToDoModel).filter(ToDoModel.id == todo_id).first()
if single_todo is None:
raise HTTPException(detail="Todo not found",status_code=404)
return single_todo
@app.put("/todos/{todo_id}")
def edit_todo(todo_id: int, todo: ToDoSchemain, db: Session = Depends(get_db)):
single_todo = db.query(ToDoModel).filter(ToDoModel.id == todo_id).first()
if single_todo is None:
raise HTTPException(detail="Todo not found", status_code=404)
# استخدم todo.dict() بدلاً من single_todo.dict()
updated_data = todo.dict(exclude_unset=True)
for key, value in updated_data.items():
setattr(single_todo, key, value)
db.commit()
db.refresh(single_todo)
return single_todo
@app.post("/todos",response_model =ToDoSchemaout)
def create_todo(todo:ToDoSchemain,db:Session = Depends(get_db)):
new_todo = ToDoModel(**todo.model_dump())
db.add(new_todo) # save in db :memory
db.commit() # apply changes in db :harddisk
db.refresh(new_todo) # current db object see new changes
return new_todo
@app.delete("/todos/{todo_id}",response_model=ToDoSchemaout)
def delete_todo(todo_id:int,db:Session = Depends(get_db)):
single_todo = db.query(ToDoModel).filter(ToDoModel.id == todo_id).first()
if single_todo is None:
raise HTTPException(detail="Todo not found",status_code=404)
db.delete(single_todo)
db.commit()
return single_todo # return json
@app.get("/all") # return List
def todos_list_template(request:Request,db:Session = Depends(get_db)):
todos = db.query(ToDoModel).all()
return templates.TemplateResponse("todos.html",{"request":request,"todos":todos})