-
-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathresources.py
More file actions
257 lines (242 loc) · 7.43 KB
/
resources.py
File metadata and controls
257 lines (242 loc) · 7.43 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from databases import Database
from fastapi import APIRouter, Body, Depends, Request, Query
from fastapi.responses import JSONResponse
from loguru import logger
from backend.db import get_db
from backend.models.dtos.mapping_issues_dto import MappingIssueCategoryDTO
from backend.models.dtos.user_dto import AuthUserDTO
from backend.services.mapping_issues_service import MappingIssueCategoryService
from backend.services.users.authentication_service import admin_only
router = APIRouter(
prefix="/tasks",
tags=["issues"],
responses={404: {"description": "Not found"}},
)
ISSUE_NOT_FOUND = "Mapping-issue category not found"
@router.get("/issues/categories/{category_id}/")
async def get_issue(category_id: int, db: Database = Depends(get_db)):
"""
Get specified mapping-issue category
---
tags:
- issues
produces:
- application/json
parameters:
- name: category_id
in: path
description: The unique mapping-issue category ID
required: true
type: integer
default: 1
responses:
200:
description: Mapping-issue category found
404:
description: Mapping-issue category not found
500:
description: Internal Server Error
"""
category_dto = await MappingIssueCategoryService.get_mapping_issue_category_as_dto(
category_id, db
)
return category_dto.model_dump(by_alias=True)
@router.patch("/issues/categories/{category_id}/")
async def patch_issue(
request: Request,
category_id: int,
user: AuthUserDTO = Depends(admin_only),
db: Database = Depends(get_db),
data: MappingIssueCategoryDTO = Body(...),
):
"""
Update an existing mapping-issue category
---
tags:
- issues
produces:
- application/json
parameters:
- in: header
name: Authorization
description: Base64 encoded session token
required: true
type: string
default: Token sessionTokenHere==
- name: category_id
in: path
description: The unique mapping-issue category ID
required: true
type: integer
default: 1
- in: body
name: body
required: true
description: JSON object for updating a mapping-issue category
schema:
properties:
name:
type: string
description:
type: string
responses:
200:
description: Mapping-issue category updated
400:
description: Invalid Request
401:
description: Unauthorized - Invalid credentials
404:
description: Mapping-issue category not found
500:
description: Internal Server Error
"""
try:
category_dto = data
category_dto.category_id = category_id
except Exception as e:
logger.error(f"Error validating request: {str(e)}")
return JSONResponse(
content={
"Error": "Unable to update mapping issue category",
"SubCode": "InvalidData",
},
status_code=400,
)
updated_category = await MappingIssueCategoryService.update_mapping_issue_category(
category_dto, db
)
return updated_category.model_dump(by_alias=True)
@router.delete("/issues/categories/{category_id}/")
async def delete_issue(
request: Request,
category_id: int,
user: AuthUserDTO = Depends(admin_only),
db: Database = Depends(get_db),
):
"""
Delete the specified mapping-issue category.
Note that categories can be deleted only if they have never been associated with a task.\
To instead archive a used category that is no longer needed, \
update the category with its archived flag set to true.
---
tags:
- issues
produces:
- application/json
parameters:
- in: header
name: Authorization
description: Base64 encoded session token
required: true
type: string
default: Token sessionTokenHere==
- name: category_id
in: path
description: The unique mapping-issue category ID
required: true
type: integer
default: 1
responses:
200:
description: Mapping-issue category deleted
401:
description: Unauthorized - Invalid credentials
404:
description: Mapping-issue category not found
500:
description: Internal Server Error
"""
await MappingIssueCategoryService.delete_mapping_issue_category(category_id, db)
return JSONResponse(
content={"Success": "Mapping-issue category deleted"}, status_code=200
)
@router.get("/issues/categories/")
async def get_issues_categories(
include_archived: bool = Query(
False,
alias="includeArchived",
description="Optional filter to include archived categories",
),
db: Database = Depends(get_db),
):
"""
Gets all mapping issue categories
---
tags:
- issues
produces:
- application/json
parameters:
- in: query
name: includeArchived
description: Optional filter to include archived categories
type: boolean
default: false
responses:
200:
description: Mapping issue categories
500:
description: Internal Server Error
"""
categories = await MappingIssueCategoryService.get_all_mapping_issue_categories(
include_archived, db
)
return categories.model_dump(by_alias=True)
@router.post("/issues/categories/", response_model=MappingIssueCategoryDTO)
async def post_issues_categories(
request: Request,
user: AuthUserDTO = Depends(admin_only),
db: Database = Depends(get_db),
data: dict = Body(...),
):
"""
Creates a new mapping-issue category
---
tags:
- issues
produces:
- application/json
parameters:
- in: header
name: Authorization
description: Base64 encoded session token
required: true
type: string
default: Token sessionTokenHere==
- in: body
name: body
required: true
description: JSON object for creating a new mapping-issue category
schema:
properties:
name:
type: string
required: true
description:
type: string
responses:
200:
description: New mapping-issue category created
400:
description: Invalid Request
401:
description: Unauthorized - Invalid credentials
500:
description: Internal Server Error
"""
try:
category_dto = MappingIssueCategoryDTO(**data)
except Exception as e:
logger.error(f"Error validating request: {str(e)}")
return JSONResponse(
content={
"Error": "Unable to create a new mapping issue category",
"SubCode": "InvalidData",
},
status_code=400,
)
new_category_id = await MappingIssueCategoryService.create_mapping_issue_category(
category_dto, db
)
return JSONResponse(content={"categoryId": new_category_id}, status_code=200)