-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (85 loc) · 3.99 KB
/
main.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import time
from typing import List
import uvicorn
from fastapi import Depends, FastAPI
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse
from starlette.responses import FileResponse
from starlette.staticfiles import StaticFiles
from db import get_ds
from dbal.data_store import DataStore
from dbal.project import Project
from dbal.projects_dao_ex import ProjectsDao
from dbal.task import Task
from dbal.tasks_dao_ex import TasksDao
from schemas import *
app = FastAPI(title="SDM + FastAPI + SQLAlchemy",
description="Quick Demo of how to use SQL DAL Maker + Python + FastAPI + SQLAlchemy",
version="1.0.0", )
# https://stackoverflow.com/questions/65916537/a-minimal-fastapi-example-loading-index-html
app.mount("/static", StaticFiles(directory="static"), name="static")
# https://stackoverflow.com/questions/65916537/a-minimal-fastapi-example-loading-index-html
# Serving only index.html
@app.get("/")
async def read_index():
return FileResponse('index.html')
@app.exception_handler(Exception)
def validation_exception_handler(request, err):
base_error_message = f"Failed to execute: {request.method}: {request.url}"
return JSONResponse(status_code=400, content={"message": f"{base_error_message}. Detail: {err}"})
@app.middleware("http")
async def add_process_time_header(request, call_next):
# print('inside middleware!')
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
response.headers["X-Process-Time"] = str(f'{process_time:0.4f} sec')
return response
@app.get('/api/projects', tags=["ProjectList"], response_model=List[SchemaProjectLi])
def get_all_projects(ds: DataStore = Depends(get_ds)):
return ProjectsDao(ds).get_all_projects()
@app.post('/api/projects', tags=["ProjectList"], status_code=201)
async def project_create(item_request: SchemaProjectCreateUpdate, ds: DataStore = Depends(get_ds)):
project = Project(p_name=item_request.p_name)
ProjectsDao(ds).create_project(project)
ds.commit()
@app.get('/api/projects/{p_id}', tags=["Project"], response_model=SchemaProject)
def project_read(p_id: int, ds: DataStore = Depends(get_ds)):
return ProjectsDao(ds).read_project(p_id)
@app.put('/api/projects/{p_id}', tags=["Project"])
async def project_update(p_id: int, item_request: SchemaProjectCreateUpdate, ds: DataStore = Depends(get_ds)):
ProjectsDao(ds).rename_project(p_id, item_request.p_name)
ds.commit()
@app.delete('/api/projects/{p_id}', tags=["Project"], status_code=204)
async def project_delete(p_id: int, ds: DataStore = Depends(get_ds)):
ProjectsDao(ds).delete_project(p_id)
ds.commit()
@app.get('/api/projects/{p_id}/tasks', tags=["ProjectTaskLI"], response_model=List[SchemaProjectTaskLI])
def project_tasks(p_id: int, ds: DataStore = Depends(get_ds)):
return TasksDao(ds).get_project_tasks(p_id)
@app.post('/api/projects/{p_id}/tasks', tags=["ProjectTaskLI"], status_code=201)
async def task_create(p_id: int, item_request: SchemaTaskCreate, ds: DataStore = Depends(get_ds)):
j = jsonable_encoder(item_request)
task = Task()
task.p_id = p_id
task.t_subject = j['t_subject']
task.t_date = datetime.now().strftime("%Y-%m-%d")
task.t_priority = 1
task.t_comments = ''
TasksDao(ds).create_task(task)
ds.commit()
@app.get('/api/tasks/{t_id}', tags=["Task"], response_model=SchemaTaskEdit)
def task_read(t_id: int, ds: DataStore = Depends(get_ds)):
return TasksDao(ds).read_task(t_id)
@app.put('/api/tasks/{t_id}', tags=["Task"])
async def task_update(t_id: int, item_request: SchemaTaskEdit, ds: DataStore = Depends(get_ds)):
j = jsonable_encoder(item_request)
del j['t_id']
TasksDao(ds).update_task(t_id, j)
ds.commit()
@app.delete('/api/tasks/{t_id}', tags=["Task"], status_code=204)
async def task_delete(t_id: int, ds: DataStore = Depends(get_ds)):
TasksDao(ds).delete_task(t_id)
ds.commit()
if __name__ == '__main__':
uvicorn.run("main:app", port=9000, reload=True)