-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
174 lines (152 loc) · 6.68 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
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
"""This is a main file to start the app, it also contains FastApi views"""
from fastapi import FastAPI, Depends
from fastapi.responses import RedirectResponse
from sqlalchemy.ext.asyncio import AsyncSession
from dao.models import User
from services import schemas
from container import user_service, table_service
from utils import get_db, get_description
from constants import API_VERSION, API_TITLE
# ------------------------------------------------------------------------
app = FastAPI(
version=API_VERSION, description=get_description(), title=API_TITLE
)
@app.get(
'/', response_class=RedirectResponse,
description='The start page of the application')
async def index_page() -> RedirectResponse:
"""This view serves to redirect requests from start route '/' to '/docs'"""
return '/docs'
@app.post(
'/user/signup', summary='Register a new user',
description='This route serves to register a new user')
async def register(
user_schema: schemas.UserRegisterSchema,
session: AsyncSession = Depends(get_db)
) -> dict[str, str]:
"""This view serves to register a new user
:param user_schema: an instance of UserRegisterSchema class
:param session: an instance of AsyncSession providing by get_db function
:return: a dictionary containing an access token
"""
token = await user_service.register(session, user_schema)
return token
@app.post(
'/user/login', summary='Sign in to user account',
description='This route serves to log in to the user account')
async def login(
user_schema: schemas.UserSchema,
session: AsyncSession = Depends(get_db)
) -> dict[str, str]:
"""This view serves to allow user to login
:param user_schema: an instance of UserRegisterSchema class
:param session: an instance of AsyncSession providing by get_db function
:return: a dictionary containing an access token
"""
token = await user_service.login(session, user_schema)
return token
@app.post(
'/user/logout', summary='Sign out from user account',
description='This route serves to allow user to log out from his account')
async def logout(
user: User = Depends(user_service.get_by_token),
session: AsyncSession = Depends(get_db)
) -> dict[str, str]:
"""This view serves to log out from account
:param user: a model representing current user
:param session: an instance of AsyncSession providing by get_db function
:return: a dictionary containing an access token
"""
await user_service.logout(session, user)
return {'message': 'Logout was successful'}
@app.get(
'/table/vacant', response_model=list[schemas.TableSchema],
summary='Get a list of all available tables',
description='This route returns all vacant tables')
async def all_tables(
session: AsyncSession = Depends(get_db)
) -> list[schemas.TableSchema]:
"""This view serves to receive all vacant tables
:param session: an instance of AsyncSession providing by get_db function
:return: a list of TableSchema instances
"""
tables = await table_service.get_all(session)
return tables
@app.get(
'/table/me', response_model=list[schemas.TableSchema],
summary='Get all tables of a current user',
description='This route returns all tables of booked by the current user')
async def client_tables(
session: AsyncSession = Depends(get_db),
user: User = Depends(user_service.get_by_token)
) -> list[schemas.TableSchema]:
"""This view serves to receive all tables booked by the current user
:param session: an instance of AsyncSession providing by get_db function
:param user: a model representing current user
:return: a list of TableSchema instances
"""
tables = await table_service.get_by_client(session, user.email)
return tables
@app.post(
'/table/book/{table_id}', summary='Booking a new table',
description='This route serves to book a new table')
async def book_table(
table_id: int, table: schemas.TableBookSchema,
session: AsyncSession = Depends(get_db),
user: User = Depends(user_service.get_by_token)
) -> dict[str, str]:
"""This view serves to book a table by its id
:param table_id: the id of the table to book
:param table: an instance of TableBookSchema class
:param session: an instance of AsyncSession providing by get_db function
:param user: a model representing current user
:return: a dictionary representing a result of the booking
"""
table.id = table_id
table.client_id = user.id
table = await table_service.book_new(session, table)
if not table:
return {'message': 'Failed to book table'}
return {'message': 'The table is booked successfully'}
@app.put(
'/table/change/{table_id}', summary='Change a booking parameters',
description='This route serves to allow the current user to change the '
'parameters of his booking such as persons amount and booking '
'time')
async def change_table_booking(
table_id: int, table: schemas.TableBookChangeSchema,
session: AsyncSession = Depends(get_db),
user: User = Depends(user_service.get_by_token)
) -> dict[str, str]:
"""This view serves to change booking options of a chosen table
:param table_id: the id of the table to book
:param table: an instance of TableBookSchema class
:param session: an instance of AsyncSession providing by get_db function
:param user: a model representing current user
:return: a dictionary representing a result of the booking
"""
table.id = table_id
table.client_id = user.id
table = await table_service.change_booking(session, table, user.id)
if not table:
return {'message': 'Failed to change bookings'}
return {'message': 'The booking was changed successfully'}
@app.delete(
'/table/cancel/{table_id}', summary='Cancel booking',
description='This route serves to cancel chosen booking of a table. You '
'cannot cancel booking less than an hour before early chosen '
'booking time')
async def cancel_booking(
table_id: int, session: AsyncSession = Depends(get_db),
user: User = Depends(user_service.get_by_token)
) -> dict[str, str]:
"""This view serves to cancel booking of a chosen table
:param table_id: the id of the table to book
:param session: an instance of AsyncSession providing by get_db function
:param user: a model representing current user
:return: a dictionary representing a result of the booking
"""
canceled = await table_service.cancel_booking(session, table_id, user.id)
if canceled:
return {'message': 'Booking is cancelled successfully'}
return {'message': 'Failed to cancel booking'}