2222 CommentGet ,
2323 CommentGetAll ,
2424 CommentGetAllWithAllInfo ,
25- CommentGetAllWithLike ,
2625 CommentGetAllWithStatus ,
2726 CommentGetWithAllInfo ,
28- CommentGetWithLike ,
2927 CommentGetWithStatus ,
3028 CommentImportAll ,
3129 CommentPost ,
@@ -164,25 +162,22 @@ async def import_comments(
164162 return result
165163
166164
167- @comment .get ("/{uuid}" , response_model = CommentGetWithLike )
168- async def get_comment (uuid : UUID , user = Depends (UnionAuth ())) -> CommentGetWithLike :
165+ @comment .get ("/{uuid}" , response_model = CommentGet )
166+ async def get_comment (uuid : UUID , user = Depends (UnionAuth (auto_error = False , allow_none = False ))) -> CommentGet :
169167 """
170168 Возвращает комментарий по его UUID в базе данных RatingAPI
171169 """
172170 comment : Comment = Comment .query (session = db .session ).filter (Comment .uuid == uuid ).one_or_none ()
173171 if comment is None :
174172 raise ObjectNotFound (Comment , uuid )
175173 base_data = CommentGet .model_validate (comment )
176- return CommentGetWithLike (
177- ** base_data .model_dump (),
178- is_liked = comment .has_reaction (user .get ("id" ), Reaction .LIKE ),
179- is_disliked = comment .has_reaction (user .get ("id" ), Reaction .DISLIKE ),
180- )
174+ if user :
175+ base_data .is_liked = comment .has_reaction (user .get ("id" ), Reaction .LIKE )
176+ base_data .is_disliked = comment .has_reaction (user .get ("id" ), Reaction .DISLIKE )
177+ return base_data
181178
182179
183- @comment .get (
184- "" , response_model = Union [CommentGetAll , CommentGetAllWithLike , CommentGetAllWithAllInfo , CommentGetAllWithStatus ]
185- )
180+ @comment .get ("" , response_model = Union [CommentGetAll , CommentGetAllWithAllInfo , CommentGetAllWithStatus ])
186181async def get_comments (
187182 limit : int = 10 ,
188183 offset : int = 0 ,
@@ -196,7 +191,7 @@ async def get_comments(
196191 unreviewed : bool = False ,
197192 asc_order : bool = False ,
198193 user = Depends (UnionAuth (scopes = ["rating.comment.review" ], auto_error = False , allow_none = False )),
199- ) -> Union [CommentGetAll , CommentGetAllWithLike , CommentGetAllWithAllInfo , CommentGetAllWithStatus ]:
194+ ) -> Union [CommentGetAll , CommentGetAllWithAllInfo , CommentGetAllWithStatus ]:
200195 """
201196 Scopes: `["rating.comment.review"]`
202197
@@ -219,7 +214,6 @@ async def get_comments(
219214
220215 `asc_order` -Если передано true, сортировать в порядке возрастания. Иначе - в порядке убывания
221216 """
222- user = {"id" : 101 , "session_scopes" : []} # тестовый user_id # пустые скоупы = обычный пользователь
223217 comments_query = (
224218 Comment .query (session = db .session )
225219 .filter (Comment .search_by_lectorer_id (lecturer_id ))
@@ -245,11 +239,7 @@ async def get_comments(
245239 result = CommentGetAllWithStatus (limit = limit , offset = offset , total = len (comments ))
246240 comment_validator = CommentGetWithStatus
247241 else :
248- result = (
249- CommentGetAllWithLike (limit = limit , offset = offset , total = len (comments ))
250- if user
251- else CommentGetAll (limit = limit , offset = offset , total = len (comments ))
252- )
242+ result = CommentGetAll (limit = limit , offset = offset , total = len (comments ))
253243 comment_validator = CommentGet
254244
255245 result .comments = comments
@@ -267,22 +257,19 @@ async def get_comments(
267257 result .total = len (result .comments )
268258 comments_with_like = []
269259 current_user_id = user .get ("id" ) if user else None
260+
270261 if current_user_id and result .comments :
271262 user_reactions = Comment .reactions_for_comments (current_user_id , db .session , result .comments )
272263 else :
273264 user_reactions = {}
274265
275266 for comment in result .comments :
276267 base_data = comment_validator .model_validate (comment )
277-
278268 if current_user_id :
279269 reaction = user_reactions .get (comment .uuid )
280- comment_with_reactions = CommentGetWithLike (
281- ** base_data .model_dump (), is_liked = reaction == Reaction .LIKE , is_disliked = reaction == Reaction .DISLIKE
282- )
283- comments_with_like .append (comment_with_reactions )
284- else :
285- comments_with_like .append (base_data )
270+ base_data .is_liked = (reaction == Reaction .LIKE )
271+ base_data .is_disliked = (reaction == Reaction .DISLIKE )
272+ comments_with_like .append (base_data )
286273
287274 result .comments = comments_with_like
288275 return result
0 commit comments