|
4 | 4 | from src.api.v1.collections.deps import CollectionServiceDepends
|
5 | 5 | from src.api.v1.collections.models import Collection
|
6 | 6 | from src.api.v1.collections.schemas import CollectionCreateRequest, CollectionResponse, CollectionUpdateRequest
|
| 7 | +from src.api.v1.quotes.deps import QuoteServiceDepends |
| 8 | +from src.api.v1.quotes.schemas import QuoteCollectionsResponse, QuoteResponse |
7 | 9 | from src.api.v1.users.me.deps import CurrentUser, CurrentUserOrNone
|
8 | 10 | from src.i18n import gettext as _
|
9 | 11 |
|
@@ -70,7 +72,71 @@ def delete_collection(id: int, current_user: CurrentUser, service: CollectionSer
|
70 | 72 | service.delete_collection(collection)
|
71 | 73 |
|
72 | 74 |
|
73 |
| -# TODO: |
74 |
| -# @router.get("/{collection_id}/quotes") |
75 |
| -# @router.post("/{collection_id}/quotes") |
76 |
| -# @router.delete("/{collection_id}/quotes/{quote_id}") |
| 75 | +@router.get("/{collection_id}/quotes", response_model=list[QuoteResponse]) |
| 76 | +def get_collection_quotes( |
| 77 | + collection_id: int, |
| 78 | + search_params: SearchParamsDepends, |
| 79 | + current_user: CurrentUserOrNone, |
| 80 | + collection_service: CollectionServiceDepends, |
| 81 | + quote_service: QuoteServiceDepends, |
| 82 | +): |
| 83 | + collection = collection_service.get_collection(collection_id) |
| 84 | + |
| 85 | + if not collection: |
| 86 | + raise HTTPException(status.HTTP_404_NOT_FOUND, _("No collection found with the ID %s." % (collection_id,))) |
| 87 | + |
| 88 | + is_private = collection.visibility == Collection.Visibility.PRIVATE |
| 89 | + has_access = current_user and current_user.id == collection.created_by_user_id |
| 90 | + |
| 91 | + if is_private and not has_access: |
| 92 | + raise HTTPException(status.HTTP_403_FORBIDDEN, _("Access denied to this private collection.")) |
| 93 | + |
| 94 | + return quote_service.get_collection_quotes(collection_id, search_params) |
| 95 | + |
| 96 | + |
| 97 | +@router.post("/{collection_id}/quotes", response_model=QuoteCollectionsResponse, status_code=status.HTTP_201_CREATED) |
| 98 | +def add_quote_to_collection( |
| 99 | + collection_id: int, |
| 100 | + quote_id: int, |
| 101 | + current_user: CurrentUser, |
| 102 | + collection_service: CollectionServiceDepends, |
| 103 | + quote_service: QuoteServiceDepends, |
| 104 | +): |
| 105 | + quote = quote_service.get_quote_by_id(quote_id) |
| 106 | + |
| 107 | + if not quote: |
| 108 | + raise HTTPException(status.HTTP_404_NOT_FOUND, _("No quote found with the ID %s." % (quote_id,))) |
| 109 | + |
| 110 | + collection = collection_service.get_collection(collection_id) |
| 111 | + |
| 112 | + if not collection: |
| 113 | + raise HTTPException(status.HTTP_404_NOT_FOUND, _("No collection found with the ID %s." % (collection_id,))) |
| 114 | + |
| 115 | + if current_user.id != collection.created_by_user_id: |
| 116 | + raise HTTPException(status.HTTP_403_FORBIDDEN, _("Access denied to this private collection.")) |
| 117 | + |
| 118 | + return collection_service.add_quote_to_collection(quote, collection) |
| 119 | + |
| 120 | + |
| 121 | +@router.delete("/{collection_id}/quotes/{quote_id}", status_code=status.HTTP_204_NO_CONTENT) |
| 122 | +def remove_quote_from_collection( |
| 123 | + collection_id: int, |
| 124 | + quote_id: int, |
| 125 | + current_user: CurrentUser, |
| 126 | + collection_service: CollectionServiceDepends, |
| 127 | + quote_service: QuoteServiceDepends, |
| 128 | +): |
| 129 | + quote = quote_service.get_quote_by_id(quote_id) |
| 130 | + |
| 131 | + if not quote: |
| 132 | + raise HTTPException(status.HTTP_404_NOT_FOUND, _("No quote found with the ID %s." % (quote_id,))) |
| 133 | + |
| 134 | + collection = collection_service.get_collection(collection_id) |
| 135 | + |
| 136 | + if not collection: |
| 137 | + raise HTTPException(status.HTTP_404_NOT_FOUND, _("No collection found with the ID %s." % (collection_id,))) |
| 138 | + |
| 139 | + if current_user.id != collection.created_by_user_id: |
| 140 | + raise HTTPException(status.HTTP_403_FORBIDDEN, _("Access denied to this private collection.")) |
| 141 | + |
| 142 | + collection_service.remove_quote_from_collection(quote, collection) |
0 commit comments