Spaces:
Sleeping
Sleeping
File size: 3,916 Bytes
b39c0ba |
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
from db.query.base_query import BaseQuery
from db.models import Metadata, Category, Session_Publisher
from fastapi.responses import JSONResponse
class BookQuery(BaseQuery):
def __init__(self, user):
super().__init__(user)
def add_book(self, db, title, author, category_id, year, publisher):
new_book = Metadata(
title=title,
author=author,
category_id=category_id,
year=year,
publisher=publisher,
)
self.add(db, new_book)
def get_book(self, db):
model = Metadata
metadata_columns = [
Metadata.id,
Metadata.title,
Metadata.author,
Category.category, # Assuming this is the correct field for category name
Category.id,
Metadata.year,
Metadata.publisher,
Metadata.thumbnail,
]
join_models = [Category]
join_conditions = [Metadata.category_id == Category.id]
result = self.get_with_joins(
db,
model=model,
columns=metadata_columns,
join_models=join_models,
join_conditions=join_conditions,
multiple=True,
)
print("result", result)
return result
def update_metadata_entry(
self, db, metadata_id, title, author, category_id, year, publisher
):
model = Metadata
# Define filter conditions based on the metadata_id
filter_conditions = [Metadata.id == metadata_id]
# Prepare the update data
update_data = {
"title": title,
"author": author,
"category_id": category_id,
"year": year,
"publisher": publisher,
}
# Call the update_entries method to update the metadata
update_response = self.update_entries(
db,
model=model,
update_data=update_data,
filter_conditions=filter_conditions,
)
if isinstance(update_response, JSONResponse):
return update_response # Return error response if any
# Fetch the updated metadata to retrieve the new category
updated_metadata = db.query(Metadata).filter(Metadata.id == metadata_id).first()
if not updated_metadata:
return JSONResponse(status_code=404, content="Metadata not found")
return updated_metadata
def update_book(self, db, book_id, title, author):
update_data = {"title": title, "author": author}
self.update(db, Metadata, book_id, update_data)
def delete_book(self, db, book_id):
self.delete(db, Metadata, book_id)
def get_books(self, db):
return self.get(db, model=Metadata, multiple=True)
def get_metadata_books(self, db, metadata_id):
return self.get(db, Metadata, id=metadata_id)
# def get_title_from_session(self, db, metadata_id, session_id):
# model = Session_Publisher
# columns = [Metadata.title]
# join_models = [Session_Publisher.id == session_id, Metadata.id == metadata_id]
# titles = self.get_all_with_join_columns(db, model, columns, join_models)
# return titles
def get_title_from_session(self, db, metadata_id, session_id):
model = Session_Publisher
columns = [Metadata.title]
join_models = [Session_Publisher]
join_conditions = [Metadata.id == metadata_id]
filter_conditions = [
Session_Publisher.user_id == self.user_id,
Session_Publisher.id == session_id,
]
titles = self.get_with_joins(
db,
model=model,
columns=columns,
join_models=join_models,
join_conditions=join_conditions,
filter_conditions=filter_conditions,
multiple=True,
)
return titles
|