1- from typing import List
1+ from typing import Annotated , List
22
3- from fastapi import APIRouter , HTTPException , Request , status
3+ from fastapi import APIRouter , Header , HTTPException , Request , status
44from pydantic import BaseModel
55
66from app .schemas import Library as LibrarySchema
77from app .schemas import LibraryNews
8+ from app .schemas import LibraryRequest as LibraryRequestSchema
89from app .schemas import Subscription as SubscriptionSchema
910from app .services .database .models import Library , Subscription
11+ from app .services .database .models .libraries_request import LibraryRequest
1012from app .services .database .orm .library import (
1113 get_libraries_by_language ,
1214 get_library_ids_by_multiple_names ,
1315 insert_library ,
1416)
17+ from app .services .database .orm .library_request import insert_library_request
1518from app .services .database .orm .subscription import upsert_multiple_subscription
1619
1720
@@ -34,27 +37,32 @@ def setup():
3437 description = "Get libraries by language" ,
3538 )
3639 async def get_by_language (request : Request , language : str ):
37- libraryList = await get_libraries_by_language (
38- language = language , session = request .app .db_session_factory
39- )
40- return [
41- LibrarySchema (
42- library_name = libraryDb .library_name ,
43- news = [
44- LibraryNews (
45- tag = news ["tag" ], description = news ["description" ]
46- )
47- for news in libraryDb .news
48- ],
49- logo = libraryDb .logo ,
50- version = libraryDb .version ,
51- release_date = libraryDb .release_date ,
52- releases_doc_url = libraryDb .releases_doc_url ,
53- fixed_release_url = libraryDb .fixed_release_url ,
54- language = libraryDb .language ,
40+ try :
41+ libraryList = await get_libraries_by_language (
42+ language = language , session = request .app .db_session_factory
5543 )
56- for libraryDb in libraryList
57- ]
44+ return [
45+ LibrarySchema (
46+ library_name = libraryDb .library_name ,
47+ news = [
48+ LibraryNews (
49+ tag = news ["tag" ], description = news ["description" ]
50+ )
51+ for news in libraryDb .news
52+ ],
53+ logo = libraryDb .logo ,
54+ version = libraryDb .version ,
55+ release_date = libraryDb .release_date ,
56+ releases_doc_url = libraryDb .releases_doc_url ,
57+ fixed_release_url = libraryDb .fixed_release_url ,
58+ language = libraryDb .language ,
59+ )
60+ for libraryDb in libraryList
61+ ]
62+ except HTTPException as e :
63+ raise e
64+ except Exception as e :
65+ HTTPException (status_code = 500 , detail = f"Unexpected error: { e } " )
5866
5967 @router .post (
6068 "" ,
@@ -80,9 +88,11 @@ async def create_library(
8088 try :
8189 await insert_library (library , request .app .db_session_factory )
8290 return LibraryResponse ()
91+ except HTTPException as e :
92+ raise e
8393 except Exception as e :
8494 raise HTTPException (
85- status_code = 500 , detail = f"Failed to create library : { e } "
95+ status_code = 500 , detail = f"Unexpected error : { e } "
8696 )
8797
8898 @router .post (
@@ -97,14 +107,22 @@ async def create_library(
97107 async def subscribe_libraries (
98108 request : Request ,
99109 body : SubscriptionSchema ,
110+ user_email : Annotated [str , Header (alias = "user-email" )],
100111 ):
101112 try :
102113 library_ids = await get_library_ids_by_multiple_names (
103114 body .libraries_list , request .app .db_session_factory
104115 )
105116
117+ if (library_ids is None ) or (len (library_ids ) == 0 ):
118+ raise HTTPException (
119+ status_code = 404 , detail = "Libraries not found"
120+ )
121+
106122 subscriptions = [
107- Subscription (email = body .email , tags = body .tags , library_id = id )
123+ Subscription (
124+ user_email = user_email , tags = body .tags , library_id = id
125+ )
108126 for id in library_ids
109127 ]
110128
@@ -113,9 +131,42 @@ async def subscribe_libraries(
113131 )
114132
115133 return SubscribeLibraryResponse ()
134+ except HTTPException as e :
135+ raise e
136+ except Exception as e :
137+ raise HTTPException (
138+ status_code = 500 , detail = f"Unexpected error: { e } "
139+ )
140+
141+ @router .post (
142+ "/request" ,
143+ response_model = LibraryResponse ,
144+ status_code = status .HTTP_200_OK ,
145+ summary = "Request a library" ,
146+ description = "Request a library to follow" ,
147+ )
148+ async def request_library (
149+ request : Request ,
150+ body : LibraryRequestSchema ,
151+ user_email : Annotated [str , Header (alias = "user-email" )],
152+ ):
153+ try :
154+ library_request = LibraryRequest (
155+ user_email = user_email ,
156+ library_name = body .library_name ,
157+ library_home_page = body .library_home_page ,
158+ )
159+
160+ await insert_library_request (
161+ library_request , request .app .db_session_factory
162+ )
163+
164+ return LibraryResponse ()
165+ except HTTPException as e :
166+ raise e
116167 except Exception as e :
117168 raise HTTPException (
118- status_code = 500 , detail = f"Subscription failed : { e } "
169+ status_code = 500 , detail = f"Unexpected error : { e } "
119170 )
120171
121172 return router
0 commit comments