-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_association_control.py
212 lines (191 loc) · 8.86 KB
/
api_association_control.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from fastapi import APIRouter, Request, Response, Form
from fastapi.responses import JSONResponse
import tools
from ow_config import MAIN_URL
from sqlalchemy.orm import sessionmaker
from sqlalchemy import insert
from sql_logic import sql_catalog as catalog
import standarts
router = APIRouter()
ASSOCIATION_RESPONSES = {
202: {
"description": "Запрос успешно обработан.",
"content": {"application/json": {"example": "Complite"}},
},
409: {
"description": "Запрашиваемое состояние уже реализовано.",
"content": {"application/json": {"example": "The association is already present"}},
},
}
@router.post(
MAIN_URL+"/association/game/genre",
tags=["Association", "Game", "Genre"],
summary="Создание ассоциации между игрой и жанром",
status_code=202,
responses={
**ASSOCIATION_RESPONSES,
401: standarts.responses[401],
403: standarts.responses["admin"][403],
}
)
async def association_game_with_genre(
response: Response,
request: Request,
game_id: int = Form(..., description="ID игры"),
mode: bool = Form(..., description="Режим работы функции. True - добавление ассоциации. False - удаление ассоциации."),
genre_id: int = Form(..., description="ID жанра")
):
access_result = await tools.access_admin(response=response, request=request)
if access_result == True:
session = sessionmaker(bind=catalog.engine)()
if mode:
output = session.query(catalog.game_genres).filter_by(game_id=game_id, genre_id=genre_id).first()
if output is None:
insert_statement = insert(catalog.game_genres).values(game_id=game_id, genre_id=genre_id)
session.execute(insert_statement)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
session.close()
return JSONResponse(status_code=409, content="The association is already present")
else:
delete_genre_association = catalog.game_genres.delete().where(catalog.game_genres.c.game_id == game_id,
catalog.game_genres.c.genre_id == genre_id)
# Выполнение операции DELETE
session.execute(delete_genre_association)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
return access_result
@router.post(
MAIN_URL+"/association/game/tag",
tags=["Association", "Game", "Tag"],
summary="Создание ассоциации между игрой и тегом",
status_code=202,
responses={
**ASSOCIATION_RESPONSES,
401: standarts.responses[401],
403: standarts.responses["admin"][403],
}
)
async def association_game_with_tag(
response: Response,
request: Request,
game_id: int = Form(..., description="ID игры"),
mode: bool = Form(..., description="Режим работы функции. True - добавление ассоциации. False - удаление ассоциации."),
tag_id: int = Form(..., description="ID тега")
):
access_result = await tools.access_admin(response=response, request=request)
if access_result == True:
session = sessionmaker(bind=catalog.engine)()
if mode:
output = session.query(catalog.allowed_mods_tags).filter_by(game_id=game_id, tag_id=tag_id).first()
if output is None:
insert_statement = insert(catalog.allowed_mods_tags).values(game_id=game_id, tag_id=tag_id)
session.execute(insert_statement)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
session.close()
return JSONResponse(status_code=409, content="The association is already present")
else:
delete_tags_association = catalog.allowed_mods_tags.delete().where(catalog.allowed_mods_tags.c.game_id == game_id,
catalog.allowed_mods_tags.c.tag_id == tag_id)
# Выполнение операции DELETE
session.execute(delete_tags_association)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
return access_result
@router.post(
MAIN_URL+"/association/mod/tag",
tags=["Association", "Mod", "Tag"],
summary="Создание ассоциации между модом и тегом",
status_code=202,
responses={
**ASSOCIATION_RESPONSES,
401: standarts.responses[401],
403: standarts.responses["non-admin"][403],
}
)
async def association_mod_with_tag(
response: Response,
request: Request,
mod_id: int = Form(..., description="ID мода"),
mode: bool = Form(..., description="Режим работы функции. True - добавление ассоциации. False - удаление ассоциации."),
tag_id: int = Form(..., description="ID тега")
):
access_result = await tools.access_mods(response=response, request=request, mods_ids=mod_id)
if access_result == True:
session = sessionmaker(bind=catalog.engine)()
if mode:
output = session.query(catalog.mods_tags).filter_by(mod_id=mod_id, tag_id=tag_id).first()
if output is None:
insert_statement = insert(catalog.mods_tags).values(mod_id=mod_id, tag_id=tag_id)
session.execute(insert_statement)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
session.close()
return JSONResponse(status_code=409, content="The association is already present")
else:
delete_tags_association = catalog.mods_tags.delete().where(catalog.mods_tags.c.mod_id == mod_id,
catalog.mods_tags.c.tag_id == tag_id)
# Выполнение операции DELETE
session.execute(delete_tags_association)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
return access_result
@router.post(
MAIN_URL+"/association/mod/dependencie",
tags=["Association", "Mod"],
summary="Создание ассоциации между модом и зависимостью",
status_code=202,
responses={
**ASSOCIATION_RESPONSES,
401: standarts.responses[401],
403: standarts.responses["non-admin"][403],
}
)
async def association_mod_with_dependencie(
response: Response,
request: Request,
mod_id: int = Form(..., description="ID мода"),
mode: bool = Form(..., description="Режим работы функции. True - добавление ассоциации. False - удаление ассоциации."),
dependencie: int = Form(..., description="ID зависимости (мода)")
):
"""
Создание ассоциативной зависимости между модом и другим модом в качестве зависимости.
"""
access_result = await tools.access_mods(response=response, request=request, mods_ids=mod_id)
if access_result == True:
session = sessionmaker(bind=catalog.engine)()
if mode:
output = session.query(catalog.mods_dependencies).filter_by(mod_id=mod_id, dependence=dependencie).first()
if output is None:
insert_statement = insert(catalog.mods_dependencies).values(mod_id=mod_id, dependence=dependencie)
session.execute(insert_statement)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
session.close()
return JSONResponse(status_code=409, content="The association is already present")
else:
delete_dependence_association = catalog.mods_dependencies.delete().where(
catalog.mods_dependencies.c.mod_id == mod_id,
catalog.mods_dependencies.c.dependence == dependencie)
# Выполнение операции DELETE
session.execute(delete_dependence_association)
session.commit()
session.close()
return JSONResponse(status_code=202, content="Complite")
else:
return access_result