-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
176 lines (127 loc) · 5.76 KB
/
routes.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import schemas
from typing import List
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
from database import get_db
from sqlalchemy.orm import Session
from crud import (
create_user, get_users, get_user, update_user, delete_user,
create_direction, get_directions, get_direction, update_direction, delete_direction,
create_idea, get_ideas, get_idea, update_idea, delete_idea
)
router_websocket = APIRouter()
router_users = APIRouter(prefix='/users', tags=['user'])
router_directions = APIRouter(prefix='/directions', tags=['direction'])
router_ideas = APIRouter(prefix='/ideas', tags=['idea'])
# WebSocket
class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_personal_message(self, message: str, websocket: WebSocket):
await websocket.send_text(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
async def notify_clients(message: str):
for connection in manager.active_connections:
await connection.send_text(message)
@router_websocket.websocket("/ws/{user_id}")
async def websocket_endpoint(websocket: WebSocket, user_id: int):
await manager.connect(websocket)
await manager.send_personal_message(f"Hi!")
try:
while True:
data = await websocket.receive_text()
create_idea()
await manager.broadcast(f"User #{user_id} says: {data}")
except WebSocketDisconnect:
manager.disconnect(websocket)
# Users
@router_users.post("/", response_model=schemas.User)
async def create_user_route(schema: schemas.UserCreate, db: Session = Depends(get_db)):
user = create_user(db, schema)
await notify_clients(f"user created: {user.first_name}")
return user
@router_users.get("/", response_model=List[schemas.User])
async def read_users(db: Session = Depends(get_db)):
users = get_users(db)
return users
@router_users.get("/{user_id}", response_model=schemas.User)
async def read_user(user_id: int, db: Session = Depends(get_db)):
user = get_user(db, user_id)
return user
@router_users.put("/{user_id}")
async def update_user_route(user_id: int, schema: schemas.UserUpdate, db: Session = Depends(get_db)):
updated_user = update_user(db, user_id, schema)
if updated_user:
await notify_clients(f"user updated: {updated_user.name}")
return updated_user
return {"error": "User not found"}
@router_users.delete("/{user_id}")
async def delete_user_route(user_id: int, db: Session = Depends(get_db)):
deleted = delete_user(db, user_id)
if deleted:
await notify_clients(f"User deleted: ID {user_id}")
return {"message": "Use deleted"}
return {"error": "User not found"}
# Directions
@router_directions.post("/", response_model=schemas.Direction)
async def create_direction_route(direction_data: schemas.DirectionCreate, db: Session = Depends(get_db)):
direction = create_direction(db, direction_data)
await notify_clients(f"new direction added: {direction.name}")
return direction
@router_directions.get("/", response_model=List[schemas.Direction])
async def read_directions(db: Session = Depends(get_db)):
directions = get_directions(db)
return directions
@router_directions.get("/{direction_id}", response_model=schemas.Direction)
async def read_direction(direction_id: int, db: Session = Depends(get_db)):
direction = get_direction(db, direction_id)
return direction
@router_directions.put("/{direction_id}", response_model=schemas.Direction)
async def update_direction_route(direction_id: int, direction_data: schemas.DirectionUpdate, db: Session = Depends(get_db)):
updated_direction = update_direction(db, direction_id, direction_data)
if updated_direction:
await notify_clients(f"Direction {updated_direction.name} updated")
return updated_direction
return {"error": "Direction not found"}
@router_directions.delete("/{direction_id}")
async def delete_direction_route(direction_id: int, db: Session = Depends(get_db)):
deleted = delete_direction(db, direction_id)
if deleted:
await notify_clients(f"Direction deleted: ID {direction_id}")
return {"message": "Direction deleted"}
return {"error": "Direction not found"}
# Ideas
@router_ideas.post("/", response_model=schemas.Idea)
async def create_idea_route(data: schemas.IdeaCreate, db: Session = Depends(get_db)):
idea = create_idea(db, data)
await notify_clients(f"new idea added: {idea.name}")
return idea
@router_ideas.get("/", response_model=List[schemas.Idea])
async def read_ideas(db: Session = Depends(get_db)):
ideas = get_ideas(db)
return ideas
@router_ideas.get("/{idea_id}", response_model=schemas.Idea)
async def read_idea(idea_id: int, db: Session = Depends(get_db)):
idea = get_idea(db, idea_id)
return idea
@router_ideas.put("/{idea_id}")
async def update_idea_route(idea_id: int, schema: schemas.IdeaUpdate, db: Session = Depends(get_db)):
updated_idea = update_idea(db, idea_id, schema)
if updated_idea:
await notify_clients(f"Idea updated: {updated_idea.name}")
return updated_idea
return {"error": "Idea not found"}
@router_ideas.delete("/{idea_id}")
async def delete_idea_route(idea_id: int, db: Session = Depends(get_db)):
deleted = delete_idea(db, idea_id)
if deleted:
await notify_clients(f"Idea deleted: ID {idea_id}")
return {"message": "Idea deleted"}
return {"error": "Idea not found"}