-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.py
224 lines (200 loc) · 7.51 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
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
213
214
215
216
217
218
219
220
221
222
223
224
import psycopg2
import psycopg2.extras
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
from typing import Optional
from db import db_connection
router = APIRouter()
class CreateCategoryDto(BaseModel):
name: Optional[str] = None
name_ru: Optional[str] = None
slug: Optional[str] = None
hidden: Optional[bool] = None
user_id: Optional[int] = None
@router.get('/v1/categories/count')
def get_v1_categories_count(conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute("SELECT COUNT(*) AS counter FROM categories")
result = cur.fetchone()
if result is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return result
finally:
conn.close()
@router.get('/v1/categories/stat')
def get_v1_categories_stat(conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
result = {}
cur.execute("SELECT COUNT(*) FROM categories")
result['total'] = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM categories WHERE name_ru IS NOT NULL")
result['in_russian'] = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM categories WHERE name IS NOT NULL")
result['in_english'] = cur.fetchone()[0]
cur.execute("SELECT COUNT(*) FROM categories WHERE name IS NOT NULL AND name_ru IS NOT NULL")
result['fully_translated'] = cur.fetchone()[0]
return result
finally:
conn.close()
@router.get('/v1/collections/{collectionId}/categories/count')
def get_v1_collections_collection_id_categories_count(collectionId, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
"""
SELECT COUNT(DISTINCT s.category_id) AS counter
FROM collections_series cs
JOIN series s
ON s.id = cs.series_id
WHERE cs.collection_id = %(collectionId)s
""", {
"collectionId": collectionId
})
result = cur.fetchone()
if result is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return result
finally:
conn.close()
@router.get('/v1/categories')
def get_list_v1_categories(conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
"""
SELECT id
, name
, name_ru
, slug
, hidden
FROM categories
""")
return cur.fetchall()
finally:
conn.close()
@router.post('/v1/categories', status_code=status.HTTP_204_NO_CONTENT)
def post_v1_categories(body: CreateCategoryDto, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor() as cur:
cur.execute(
"""
INSERT
INTO categories
( name
, name_ru
, slug
, hidden
, created_at
, created_by
, updated_at
, updated_by
)
VALUES
( %(name)s
, %(name_ru)s
, %(slug)s
, %(hidden)s
, CURRENT_TIMESTAMP
, %(user_id)s
, CURRENT_TIMESTAMP
, %(user_id)s
)
""", {
"name": body.name,
"name_ru": body.name_ru,
"slug": body.slug,
"hidden": body.hidden,
"user_id": body.user_id
})
finally:
conn.close()
@router.get('/v1/categories/search')
def get_list_v1_categories_search(hidden: bool, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
"""
SELECT id
, name
, name_ru
, slug
, hidden
FROM categories
WHERE hidden = %(hidden)s
""", {
"hidden": hidden
})
return cur.fetchall()
finally:
conn.close()
@router.get('/v1/categories/{categoryId}')
def get_v1_categories_category_id(categoryId, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
cur.execute(
"""
SELECT id
, name
, name_ru
, slug
, hidden
FROM categories
WHERE id = %(categoryId)s
""", {
"categoryId": categoryId
})
result = cur.fetchone()
if result is None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return result
finally:
conn.close()
@router.put('/v1/categories/{categoryId}', status_code=status.HTTP_204_NO_CONTENT)
def put_v1_categories_category_id(body: CreateCategoryDto, categoryId, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor() as cur:
cur.execute(
"""
UPDATE categories
SET name = %(name)s
, name_ru = %(name_ru)s
, slug = %(slug)s
, hidden = %(hidden)s
, updated_at = CURRENT_TIMESTAMP
, updated_by = %(user_id)s
WHERE id = %(categoryId)s
""", {
"name": body.name,
"name_ru": body.name_ru,
"slug": body.slug,
"hidden": body.hidden,
"user_id": body.user_id,
"categoryId": categoryId
})
finally:
conn.close()
@router.delete('/v1/categories/{categoryId}', status_code=status.HTTP_204_NO_CONTENT)
def delete_v1_categories_category_id(categoryId, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor() as cur:
cur.execute(
"""
DELETE
FROM categories
WHERE id = %(categoryId)s
""", {
"categoryId": categoryId
})
finally:
conn.close()