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 ]