File size: 2,014 Bytes
1bdfad3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from db.query.query_user_meta import UserMetaQuery
from db.query.query_book import BookQuery
from service.dto import MetadataResponse
from fastapi.responses import JSONResponse

class DataFetching:
    def __init__(self, user, db):
        self.user = user
        self.db = db
        
    def collection_fetching(self):
        user_meta_query = UserMetaQuery(self.user)
        user_meta_entries = user_meta_query.get_user_meta_entries(self.db)
        
        if not user_meta_entries or user_meta_entries==[]:
            return {"info": "No book collection found"}
        
        # Extract relevant data from the user_meta_entries
        
        
        results = [
            {
                "user_id": user_meta.user_id,  # Ensure you include user_id if needed
                "metadata_id": metadata.id,
                "title": metadata.title,
                "author": metadata.author,
                "category_name": category.category,
                "year": metadata.year,
                "publisher": metadata.publisher,
            }
            for user_meta, metadata, category in user_meta_entries  # Unpack the tuple
        ]
        
        print("Hasil akhir ", results)
        
        # Extract relevant data from the user_meta_entries
        return results
    
    def metadata_fetching(self):
        book_query = BookQuery(self.user)
        book_query_entries = book_query.get_book(self.db)
        
        return [
            MetadataResponse(
                id = id,
                title=title,
                author=author,
                category=category,
                category_id = category_id,
                year=year,
                publisher=publisher,
                thumbnail=(
                    thumbnail if thumbnail else None
                ),  # Ensure None if thumbnail is not present
            )
            for id, title, author, category, category_id, year, publisher, thumbnail in book_query_entries
        ]