-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (128 loc) · 5.5 KB
/
main.py
File metadata and controls
151 lines (128 loc) · 5.5 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from fastapi import FastAPI , Depends , status , HTTPException , Query , Path , Body
from db.create_db import create_tables
from models.models_data import User , UserLog , UserTasks , AiRequest
from sqlalchemy.ext.asyncio import AsyncSession
from typing import Annotated , List
from db.data_manipulation import (create_user ,
user_verification,
create_user_task,
get_users_task,
delete_tasks,
update_tasks,
completed_tasks
)
from Depends.create_session import get_session
from Depends.auth_scheme import encode_jwt , decode_jwt
from model_ai.ai_text import sending_a_reply
import json
app = FastAPI(
title="To Do project📝"
)
@app.on_event("startup")
async def on_startup():
await create_tables()
@app.get("/home/" ,
summary="Главная страница" ,
tags=["Home⬆"])
async def home () :
return {
"message" : "Главная страница ⬆"
}
@app.post("/auth/register/" ,
summary="Регистрация ✅",
tags=["Users🙍♂️"] ,
status_code=status.HTTP_201_CREATED)
async def user_register (user : User ,
session : Annotated[AsyncSession , Depends(get_session)] ,
) :
result = await create_user(session , user)
if result :
return result
raise HTTPException(status_code=status.HTTP_409_CONFLICT,
detail= "This user already exists in the database.")
@app.post("/auth/login/" , summary="Авторизация🔐" , tags=["Users🙍♂️"])
async def user_login (userlog : UserLog ,
session : Annotated[AsyncSession , Depends(get_session)]) :
result = await user_verification(session , userlog)
if result :
res = encode_jwt(result)
return {
"token" : res
}
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
detail="Incorrect data or you are not registered")
@app.post("/tasks/" ,
summary="Добавить задачу",
tags=["Tasks📝"],
status_code=status.HTTP_201_CREATED
)
async def create_tasks (usertasks : List[ UserTasks] ,
user_id : Annotated[int , Depends(decode_jwt)],
session : Annotated[AsyncSession , Depends(get_session)],
) :
return await create_user_task(session , user_id , usertasks)
@app.get("/tasks/" ,
summary="Получить задачу",
tags=["Tasks📝"]
)
async def get_task (session : Annotated[AsyncSession , Depends(get_session)],
user_id : Annotated[int , Depends(decode_jwt)],
filtering_by_title : Annotated[str | None, Query()] = None) :
return await get_users_task(session , user_id , filtering_by_title)
@app.delete("/tasks/delete_task/{task_id}/",
summary="Удолить задачу",
tags=["Операции над задачами 🔍"]
)
async def delete_task (session : Annotated[AsyncSession , Depends(get_session)] ,
user_id : Annotated[int , Depends(decode_jwt)],
task_id : Annotated[int ,Path()]
) :
result = await delete_tasks(session , user_id , task_id)
if result :
return result
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@app.put("/tasks/update_tasks/{id}/" ,
summary="Обновление задач" ,
tags=["Операции над задачами 🔍"]
)
async def update_task (session : Annotated[AsyncSession , Depends(get_session)],
user_id : Annotated[int , Depends(decode_jwt)],
id : Annotated[int , Path()],
updated_task : UserTasks
) :
result = await update_tasks(session ,
user_id ,
id ,
updated_task)
if result :
return result
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="Taks not found"
)
@app.put("/created/{id}/" ,
summary="Выполнил задачу",
tags=["Операции над задачами 🔍"]
)
async def created_task (session : Annotated[AsyncSession , Depends(get_session)],
id : Annotated[int , Path()],
user_id : Annotated[int , Depends(decode_jwt)]
) :
result = await completed_tasks(session , user_id , id)
if result :
return result
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
detail="task not found"
)
@app.post("/task_ai/",
summary= "Создание задач с помощью ИИ",
tags=["AI 🤖"]
)
async def creat_task_ai (
user_prompt : AiRequest,
_: Annotated[int , Depends(decode_jwt)]
) :
result = await sending_a_reply(user_prompt.prompt)
if result :
result_json = json.loads(result)
return result_json
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)