File size: 3,116 Bytes
d57efd6
 
b39c0ba
d57efd6
 
 
b39c0ba
 
 
d57efd6
 
b39c0ba
d57efd6
 
 
 
b39c0ba
 
 
 
 
d57efd6
 
 
b39c0ba
 
 
d57efd6
 
b39c0ba
d57efd6
 
0767396
d57efd6
 
 
 
b39c0ba
d57efd6
b39c0ba
 
 
 
d57efd6
b39c0ba
d57efd6
b39c0ba
 
d57efd6
 
0767396
d57efd6
 
 
 
b39c0ba
d57efd6
b39c0ba
 
 
 
d57efd6
 
b39c0ba
 
d57efd6
b39c0ba
0767396
d57efd6
 
 
 
b39c0ba
d57efd6
b39c0ba
 
 
 
d57efd6
 
b39c0ba
 
 
d57efd6
b39c0ba
0767396
d57efd6
 
 
b39c0ba
 
 
 
 
d57efd6
 
b39c0ba
 
d57efd6
 
 
b39c0ba
d57efd6
 
0767396
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from typing import Annotated, List, Optional
from api.router.user import user_dependency
from api.auth import check_user_authentication
from fastapi import APIRouter, Depends
from db.database import get_db
from sqlalchemy.orm import Session
from db.query.query_user_meta import UserMetaQuery
from db.fetching import DataFetching
from utils.error_handlers import handle_exception


router = APIRouter(tags=["Book_Collection"])
db_dependency = Annotated[Session, Depends(get_db)]


@router.get("/book_collection")
async def get_book_collection(user: user_dependency, db: db_dependency):
    """Fetch user's book collection."""
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    try:

        fetching = DataFetching(user, db)
        book_collection = fetching.collection_fetching()
        
        return {
            "status": "success",
            "book_collection": book_collection,
        }
    except Exception as e:
        return handle_exception(e)


@router.post("/book_collection")
async def request_book_collection(
    user: user_dependency, db: db_dependency, metadata_ids: List[Optional[int]]
):
    """Insert book collection metadata for a user."""
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    user_meta_query = UserMetaQuery(user)

    try:
        return user_meta_query.insert_user_meta_entries(db, metadata_ids)

    except Exception as e:
        return handle_exception(e)


@router.put("/book_collection")
async def update_book_collection(
    user: user_dependency, db: db_dependency, metadata_ids: List[Optional[int]]
):
    """Update user's book collection metadata."""
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    try:
        user_meta_query = UserMetaQuery(user)
        return user_meta_query.update_user_meta_entries(db, metadata_ids)

    except Exception as e:
        return handle_exception(e)


@router.delete("/book_collection/{metadata_id}")
async def delete_book_collection(
    user: user_dependency, db: db_dependency, metadata_id: int
):
    """Delete a specific book collection entry."""
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    try:
        user_meta_query = UserMetaQuery(user)
        return user_meta_query.delete_user_meta(
            db, metadata_id=metadata_id
        )
    except Exception as e:
        return handle_exception(e)


@router.delete("/all_collections")
async def delete_all_book(user: user_dependency, db: db_dependency):
    """Delete all book collections for the authenticated user."""
    auth_response = check_user_authentication(user)
    if auth_response:
        return auth_response

    try:
        user_meta_query = UserMetaQuery(user)
        user_meta_query.delete_all_user_meta(db)

        return {
            "status": "success",
            "message": f"Deleted book collection for user {user.get('id')}",
        }
    except Exception as e:
        return handle_exception(e)