22import logging
33from typing import Any , Literal
44
5- from fastapi import APIRouter , Depends , UploadFile , File , Query
5+ from fastapi import APIRouter , Depends , UploadFile , File , Query , HTTPException
66from fastapi_sqlalchemy import db
77from calendar_backend .models .db import Lecturer
8+ from calendar_backend .models .db import CommentLecturer as DbCommentLecturer
9+ from calendar_backend .models .db import Photo as DbPhoto
810
911from calendar_backend .settings import get_settings
1012from calendar_backend .methods import utils , auth
1618 LecturerPatch ,
1719 Photo ,
1820 LecturerPhotos ,
19- CommentLecturer ,
21+ CommentLecturer , LecturerCommentPost , LecturerCommentPatch , LecturerComments
2022)
23+ from calendar_backend .exceptions import ObjectNotFound
2124
2225lecturer_router = APIRouter (prefix = "/timetable/lecturer" , tags = ["Lecturer" ])
2326settings = get_settings ()
@@ -41,25 +44,17 @@ async def http_get_lecturers(
4144 query : str = "" ,
4245 limit : int = 10 ,
4346 offset : int = 0 ,
44- details : list [Literal ["photo" , "description" , "comments" , "" ]] | None = Query ([]),
4547) -> dict [str , Any ]:
4648 res = Lecturer .get_all (session = db .session ).filter (Lecturer .search (query ))
47- cnt , res = res .count (), res .offset (offset ).limit (limit ).all ()
49+ if limit :
50+ cnt , res = res .count (), res .offset (offset ).limit (limit ).all ()
51+ else :
52+ cnt , res = res .count (), res .offset (offset ).all ()
4853 for row in res :
4954 row .avatar_link = row .avatar .link if row .avatar else None
5055 result = [LecturerGet .from_orm (row ) for row in res ]
51- exclude = []
52- details = details or ["" ]
53- if "" in details :
54- exclude .append (["photo" , "description" , "comments" ])
55- if "photo" not in details :
56- exclude .append ("photo" )
57- if "description" not in details :
58- exclude .append ("description" )
59- if "comments" not in details :
60- exclude .append ("comments" )
6156 return {
62- "items" : [ row . dict ( exclude = { * exclude }) for row in result ] ,
57+ "items" : result ,
6358 "limit" : limit ,
6459 "offset" : offset ,
6560 "total" : cnt ,
@@ -68,7 +63,7 @@ async def http_get_lecturers(
6863
6964@lecturer_router .post ("/" , response_model = LecturerGet )
7065async def http_create_lecturer (lecturer : LecturerPost , _ : auth .User = Depends (auth .get_current_user )) -> LecturerGet :
71- return Lecturer .create (session = db .session , ** lecturer .dict ())
66+ return LecturerGet . from_orm ( Lecturer .create (session = db .session , ** lecturer .dict () ))
7267
7368
7469@lecturer_router .patch ("/{id}" , response_model = LecturerGet )
@@ -84,28 +79,88 @@ async def http_delete_lecturer(id: int, _: auth.User = Depends(auth.get_current_
8479 Lecturer .delete (id , session = db .session )
8580
8681
87- @lecturer_router .post ("/{id }/photo" , response_model = Photo )
88- async def http_upload_photo (id : int , photo : UploadFile = File (...)) -> Photo :
89- return Photo .from_orm (await utils .upload_lecturer_photo (id , db .session , file = photo ))
82+ @lecturer_router .post ("/{lecturer_id }/photo" , response_model = Photo )
83+ async def http_upload_photo (lecturer_id : int , photo : UploadFile = File (...)) -> Photo :
84+ return Photo .from_orm (await utils .upload_lecturer_photo (lecturer_id , db .session , file = photo ))
9085
9186
92- @lecturer_router .get ("/{id}/photo" , response_model = LecturerPhotos )
93- async def http_get_lecturer_photos (id : int ) -> LecturerPhotos :
94- lecturer = Lecturer .get (id , session = db .session )
95- lecturer .links = [row .link for row in lecturer .photos ]
96- return LecturerPhotos .from_orm (lecturer )
87+ @lecturer_router .get ("/{lecturer_id}/photo" , response_model = LecturerPhotos )
88+ async def http_get_lecturer_photos (lecturer_id : int , limit : int = 10 ,
89+ offset : int = 0 ) -> LecturerPhotos :
90+ res = DbPhoto .get_all (session = db .session ).filter (DbPhoto .lecturer_id == lecturer_id )
91+ if limit :
92+ cnt , res = res .count (), res .offset (offset ).limit (limit ).all ()
93+ else :
94+ cnt , res = res .count (), res .offset (offset ).all ()
95+ return LecturerPhotos (** {
96+ "items" : [row .link for row in res ],
97+ "limit" : limit ,
98+ "offset" : offset ,
99+ "total" : cnt
100+ })
97101
98102
99- @lecturer_router .post ("/{id }/comment" , response_model = CommentLecturer )
100- async def http_comment_lecturer (id : int , comment_text : str , author_name : str ) -> CommentLecturer :
101- return CommentLecturer .from_orm (await utils . create_comment_lecturer ( id , db .session , comment_text , author_name ))
103+ @lecturer_router .post ("/{lecturer_id }/comment/ " , response_model = CommentLecturer )
104+ async def http_comment_lecturer (lecturer_id : int , comment : LecturerCommentPost ) -> CommentLecturer :
105+ return CommentLecturer .from_orm (DbCommentLecturer . create ( lecturer_id = lecturer_id , session = db .session , ** comment . dict () ))
102106
103107
104- @lecturer_router .patch ("/{id}/comment" , response_model = CommentLecturer )
105- async def http_update_comment_lecturer (comment_id : int , new_text : str ) -> CommentLecturer :
106- return CommentLecturer .from_orm (await utils .update_comment_lecturer (comment_id , db .session , new_text ))
108+ @lecturer_router .patch ("/{lecturer_id}/comment/{id}" , response_model = CommentLecturer )
109+ async def http_update_comment_lecturer (id : int , lecturer_id : int , comment_inp : LecturerCommentPatch ) -> CommentLecturer :
110+ comment = DbCommentLecturer .get (id = id , session = db .session )
111+ if comment .lecturer_id != lecturer_id :
112+ raise ObjectNotFound (DbCommentLecturer , id )
113+ return CommentLecturer .from_orm (DbCommentLecturer .update (id , session = db .session , ** comment_inp .dict (exclude_unset = True )))
107114
108115
109116@lecturer_router .post ("/{id}/avatar" , response_model = LecturerGet )
110117async def http_set_lecturer_avatar (id : int , photo_id : int ) -> LecturerGet :
111118 return LecturerGet .from_orm (await utils .set_lecturer_avatar (id , photo_id , db .session ))
119+
120+
121+ @lecturer_router .delete ("/{lecturer_id}/comment/{id}" , response_model = None )
122+ async def http_delete_comment (id : int , lecturer_id : int , _ : auth .User = Depends (auth .get_current_user )) -> None :
123+ comment = DbCommentLecturer .get (id , session = db .session )
124+ if comment .lecturer_id != lecturer_id :
125+ raise ObjectNotFound (DbCommentLecturer , id )
126+ return DbCommentLecturer .delete (id = id , session = db .session )
127+
128+
129+ @lecturer_router .get ("/{lecturer_id}/comment/{id}" , response_model = CommentLecturer )
130+ async def http_get_comment (id : int , lecturer_id : int ) -> CommentLecturer :
131+ comment = DbCommentLecturer .get (id , session = db .session )
132+ if not comment .lecturer_id == lecturer_id :
133+ raise ObjectNotFound (DbCommentLecturer , id )
134+ return CommentLecturer .from_orm (comment )
135+
136+
137+ @lecturer_router .delete ("/{lecturer_id}/photo/{id}" , response_model = None )
138+ async def http_delete_photo (id : int , lecturer_id : int ) -> None :
139+ photo = DbPhoto .get (id , session = db .session )
140+ if photo .lecturer_id != lecturer_id :
141+ raise ObjectNotFound (DbPhoto , id )
142+ return DbPhoto .delete (id = id , session = db .session )
143+
144+
145+ @lecturer_router .get ("/{lecturer_id}/comment/" , response_model = LecturerComments )
146+ async def http_get_all_lecturer_comments (lecturer_id : int , limit : int = 10 , offset : int = 0 ) -> LecturerComments :
147+ res = DbCommentLecturer .get_all (session = db .session ).filter (DbCommentLecturer .lecturer_id == lecturer_id )
148+ if limit :
149+ cnt , res = res .count (), res .offset (offset ).limit (limit ).all ()
150+ else :
151+ cnt , res = res .count (), res .offset (offset ).all ()
152+ return LecturerComments (** {
153+ "items" : res ,
154+ "limit" : limit ,
155+ "offset" : offset ,
156+ "total" : cnt
157+ })
158+
159+
160+ @lecturer_router .get ("/{lecturer_id}/photo/{id}" , response_model = Photo )
161+ async def get_photo (id : int , lecturer_id : int ) -> Photo :
162+ photo = DbPhoto .get (id , session = db .session )
163+ if photo .lecturer_id != lecturer_id :
164+ raise ObjectNotFound (DbPhoto , id )
165+ return Photo .from_orm (photo )
166+
0 commit comments