1- from fastapi import APIRouter , HTTPException , status , Query , Body , Path
1+ from fastapi import APIRouter , HTTPException , status , Body , Path
22import uuid
33from app .schemas .album import (
44 GetAlbumsResponse ,
1111 SuccessResponse ,
1212 ErrorResponse ,
1313 ImageIdsRequest ,
14+ SetCoverImageRequest ,
1415 Album ,
1516)
1617from app .database .albums import (
2425 db_add_images_to_album ,
2526 db_remove_image_from_album ,
2627 db_remove_images_from_album ,
28+ db_update_album_cover_image ,
2729 verify_album_password ,
2830)
2931
3032router = APIRouter ()
3133
3234
33- # GET /albums/ - Get all albums
35+ # GET /albums/ - Get all albums (including locked ones)
3436@router .get ("/" , response_model = GetAlbumsResponse )
35- def get_albums (show_hidden : bool = Query (False )):
36- albums = db_get_all_albums (show_hidden )
37+ def get_albums ():
38+ """Get all albums. Always returns both locked and unlocked albums."""
39+ albums = db_get_all_albums ()
3740 album_list = []
3841 for album in albums :
42+ # Get image count for each album
43+ image_ids = db_get_album_images (album [0 ])
44+ image_count = len (image_ids )
45+
3946 album_list .append (
4047 Album (
4148 album_id = album [0 ],
4249 album_name = album [1 ],
4350 description = album [2 ] or "" ,
44- is_hidden = bool (album [3 ]),
51+ is_locked = bool (album [3 ]),
52+ cover_image_path = album [5 ] if len (album ) > 5 else None ,
53+ image_count = image_count ,
4554 )
4655 )
4756 return GetAlbumsResponse (success = True , albums = album_list )
@@ -64,7 +73,7 @@ def create_album(body: CreateAlbumRequest):
6473 album_id = str (uuid .uuid4 ())
6574 try :
6675 db_insert_album (
67- album_id , body .name , body .description , body .is_hidden , body .password
76+ album_id , body .name , body .description , body .is_locked , body .password
6877 )
6978 return CreateAlbumResponse (success = True , album_id = album_id )
7079 except Exception as e :
@@ -91,11 +100,17 @@ def get_album(album_id: str = Path(...)):
91100 )
92101
93102 try :
103+ # Get image count for the album
104+ image_ids = db_get_album_images (album_id )
105+ image_count = len (image_ids )
106+
94107 album_obj = Album (
95108 album_id = album [0 ],
96109 album_name = album [1 ],
97110 description = album [2 ] or "" ,
98- is_hidden = bool (album [3 ]),
111+ is_locked = bool (album [3 ]),
112+ cover_image_path = album [5 ] if len (album ) > 5 else None ,
113+ image_count = image_count ,
99114 )
100115 return GetAlbumResponse (success = True , data = album_obj )
101116 except Exception as e :
@@ -109,6 +124,8 @@ def get_album(album_id: str = Path(...)):
109124 )
110125
111126
127+ # PUT /albums/{album_id} - Update Album
128+ @router .put ("/{album_id}" , response_model = SuccessResponse )
112129# PUT /albums/{album_id} - Update Album
113130@router .put ("/{album_id}" , response_model = SuccessResponse )
114131def update_album (album_id : str = Path (...), body : UpdateAlbumRequest = Body (...)):
@@ -127,11 +144,11 @@ def update_album(album_id: str = Path(...), body: UpdateAlbumRequest = Body(...)
127144 "album_id" : album [0 ],
128145 "album_name" : album [1 ],
129146 "description" : album [2 ],
130- "is_hidden " : bool (album [3 ]),
147+ "is_locked " : bool (album [3 ]),
131148 "password_hash" : album [4 ],
132149 }
133150
134- if album_dict ["password_hash " ]:
151+ if album_dict ["is_locked " ]:
135152 if not body .current_password :
136153 raise HTTPException (
137154 status_code = status .HTTP_401_UNAUTHORIZED ,
@@ -154,7 +171,7 @@ def update_album(album_id: str = Path(...), body: UpdateAlbumRequest = Body(...)
154171
155172 try :
156173 db_update_album (
157- album_id , body .name , body .description , body .is_hidden , body .password
174+ album_id , body .name , body .description , body .is_locked , body .password
158175 )
159176 return SuccessResponse (success = True , msg = "Album updated successfully" )
160177 except Exception as e :
@@ -215,18 +232,18 @@ def get_album_images(
215232 "album_id" : album [0 ],
216233 "album_name" : album [1 ],
217234 "description" : album [2 ],
218- "is_hidden " : bool (album [3 ]),
235+ "is_locked " : bool (album [3 ]),
219236 "password_hash" : album [4 ],
220237 }
221238
222- if album_dict ["is_hidden " ]:
239+ if album_dict ["is_locked " ]:
223240 if not body .password :
224241 raise HTTPException (
225242 status_code = status .HTTP_401_UNAUTHORIZED ,
226243 detail = ErrorResponse (
227244 success = False ,
228245 error = "Password Required" ,
229- message = "Password is required to access this hidden album." ,
246+ message = "Password is required to access this locked album." ,
230247 ).model_dump (),
231248 )
232249 if not verify_album_password (album_id , body .password ):
@@ -355,3 +372,68 @@ def remove_images_from_album(
355372 success = False , error = "Failed to Remove Images" , message = str (e )
356373 ).model_dump (),
357374 )
375+
376+
377+ # PUT /albums/{album_id}/cover - Set album cover image
378+ @router .put ("/{album_id}/cover" , response_model = SuccessResponse )
379+ def set_album_cover_image (
380+ album_id : str = Path (...), body : SetCoverImageRequest = Body (...)
381+ ):
382+ """Set or update the cover image for an album"""
383+ album = db_get_album (album_id )
384+ if not album :
385+ raise HTTPException (
386+ status_code = status .HTTP_404_NOT_FOUND ,
387+ detail = ErrorResponse (
388+ success = False ,
389+ error = "Album Not Found" ,
390+ message = "No album exists with the provided ID." ,
391+ ).model_dump (),
392+ )
393+
394+ # Verify the image exists in the album
395+ album_image_ids = db_get_album_images (album_id )
396+ if body .image_id not in album_image_ids :
397+ raise HTTPException (
398+ status_code = status .HTTP_400_BAD_REQUEST ,
399+ detail = ErrorResponse (
400+ success = False ,
401+ error = "Image Not In Album" ,
402+ message = "The specified image is not in this album." ,
403+ ).model_dump (),
404+ )
405+
406+ try :
407+ # Get the image path from the database
408+ import sqlite3
409+ from app .config .settings import DATABASE_PATH
410+
411+ conn = sqlite3 .connect (DATABASE_PATH )
412+ cursor = conn .cursor ()
413+ cursor .execute ("SELECT path FROM images WHERE id = ?" , (body .image_id ,))
414+ result = cursor .fetchone ()
415+ conn .close ()
416+
417+ if not result :
418+ raise HTTPException (
419+ status_code = status .HTTP_404_NOT_FOUND ,
420+ detail = ErrorResponse (
421+ success = False ,
422+ error = "Image Not Found" ,
423+ message = "The specified image does not exist." ,
424+ ).model_dump (),
425+ )
426+
427+ image_path = result [0 ]
428+ db_update_album_cover_image (album_id , image_path )
429+
430+ return SuccessResponse (
431+ success = True , msg = "Album cover image updated successfully"
432+ )
433+ except Exception as e :
434+ raise HTTPException (
435+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
436+ detail = ErrorResponse (
437+ success = False , error = "Failed to Set Cover Image" , message = str (e )
438+ ).model_dump (),
439+ )
0 commit comments