Spaces:
Sleeping
Sleeping
Commit
·
1c14126
1
Parent(s):
878c73a
fix:improve error message
Browse files- api/router/book.py +19 -8
- api/router/book_collection.py +25 -6
- db/fetching.py +1 -0
- db/query/base_query.py +4 -2
- db/query/query_user_meta.py +22 -12
- service/dto.py +1 -0
- utils/error_handlers.py +18 -4
api/router/book.py
CHANGED
@@ -88,14 +88,20 @@ async def upload_file(
|
|
88 |
logging.info("Database Inserted")
|
89 |
|
90 |
return {
|
|
|
91 |
"filename": file.filename,
|
92 |
"response": response,
|
93 |
"info": "upload file successfully",
|
94 |
}
|
95 |
|
96 |
except Exception as e:
|
97 |
-
return
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
@router.put("/book/{metadata_id}")
|
101 |
async def update_metadata(
|
@@ -147,7 +153,6 @@ async def update_metadata(
|
|
147 |
|
148 |
# Update existing metadata entry
|
149 |
metadata = db.query(Metadata).filter(Metadata.id == metadata_id).first()
|
150 |
-
print(metadata)
|
151 |
|
152 |
if not metadata:
|
153 |
return JSONResponse(status_code=404, content="Metadata not found")
|
@@ -155,11 +160,10 @@ async def update_metadata(
|
|
155 |
updated_metadata = book_query.update_metadata_entry(
|
156 |
db, metadata_id, title, author, category_id, year, publisher
|
157 |
)
|
158 |
-
print(updated_metadata)
|
159 |
updated_category = category_query.get_category(db, updated_metadata.category_id)
|
160 |
-
print(updated_category)
|
161 |
|
162 |
return MetadataResponse(
|
|
|
163 |
id=metadata_id,
|
164 |
title=updated_metadata.title,
|
165 |
author=updated_metadata.author,
|
@@ -175,7 +179,10 @@ async def update_metadata(
|
|
175 |
)
|
176 |
|
177 |
except Exception as e:
|
178 |
-
return
|
|
|
|
|
|
|
179 |
|
180 |
@router.delete("/book/{metadata_id}")
|
181 |
async def delete_metadata(user: user_dependency, db: db_dependency, metadata_id: int):
|
@@ -196,8 +203,12 @@ async def delete_metadata(user: user_dependency, db: db_dependency, metadata_id:
|
|
196 |
db.delete(metadata)
|
197 |
db.commit()
|
198 |
|
199 |
-
return {"
|
200 |
|
201 |
except Exception as e:
|
202 |
-
return
|
|
|
|
|
|
|
|
|
203 |
|
|
|
88 |
logging.info("Database Inserted")
|
89 |
|
90 |
return {
|
91 |
+
"status": "success",
|
92 |
"filename": file.filename,
|
93 |
"response": response,
|
94 |
"info": "upload file successfully",
|
95 |
}
|
96 |
|
97 |
except Exception as e:
|
98 |
+
return {
|
99 |
+
"status": "error",
|
100 |
+
"filename": "",
|
101 |
+
"response": "",
|
102 |
+
"info": "upload file failed",
|
103 |
+
"error_message":handle_exception(e)
|
104 |
+
}
|
105 |
|
106 |
@router.put("/book/{metadata_id}")
|
107 |
async def update_metadata(
|
|
|
153 |
|
154 |
# Update existing metadata entry
|
155 |
metadata = db.query(Metadata).filter(Metadata.id == metadata_id).first()
|
|
|
156 |
|
157 |
if not metadata:
|
158 |
return JSONResponse(status_code=404, content="Metadata not found")
|
|
|
160 |
updated_metadata = book_query.update_metadata_entry(
|
161 |
db, metadata_id, title, author, category_id, year, publisher
|
162 |
)
|
|
|
163 |
updated_category = category_query.get_category(db, updated_metadata.category_id)
|
|
|
164 |
|
165 |
return MetadataResponse(
|
166 |
+
status="success",
|
167 |
id=metadata_id,
|
168 |
title=updated_metadata.title,
|
169 |
author=updated_metadata.author,
|
|
|
179 |
)
|
180 |
|
181 |
except Exception as e:
|
182 |
+
return {
|
183 |
+
"status":"error",
|
184 |
+
"error_message":handle_exception(e)
|
185 |
+
}
|
186 |
|
187 |
@router.delete("/book/{metadata_id}")
|
188 |
async def delete_metadata(user: user_dependency, db: db_dependency, metadata_id: int):
|
|
|
203 |
db.delete(metadata)
|
204 |
db.commit()
|
205 |
|
206 |
+
return {"status": "delete successfully"}
|
207 |
|
208 |
except Exception as e:
|
209 |
+
return {
|
210 |
+
"status": "error",
|
211 |
+
"message": "delete failed",
|
212 |
+
"error_message": handle_exception(e)
|
213 |
+
}
|
214 |
|
api/router/book_collection.py
CHANGED
@@ -31,9 +31,9 @@ async def get_book_collection(user: user_dependency, db: db_dependency):
|
|
31 |
}
|
32 |
except Exception as e:
|
33 |
return {
|
34 |
-
"status": "
|
35 |
"book_collection": [],
|
36 |
-
"
|
37 |
}
|
38 |
|
39 |
|
@@ -52,7 +52,12 @@ async def request_book_collection(
|
|
52 |
return user_meta_query.insert_user_meta_entries(db, metadata_ids)
|
53 |
|
54 |
except Exception as e:
|
55 |
-
return
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
|
58 |
@router.put("/book_collection")
|
@@ -69,7 +74,13 @@ async def update_book_collection(
|
|
69 |
return user_meta_query.update_user_meta_entries(db, metadata_ids)
|
70 |
|
71 |
except Exception as e:
|
72 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
|
75 |
@router.delete("/book_collection/{metadata_id}")
|
@@ -87,7 +98,11 @@ async def delete_book_collection(
|
|
87 |
db, metadata_id=metadata_id
|
88 |
)
|
89 |
except Exception as e:
|
90 |
-
return
|
|
|
|
|
|
|
|
|
91 |
|
92 |
|
93 |
@router.delete("/all_collections")
|
@@ -106,4 +121,8 @@ async def delete_all_book(user: user_dependency, db: db_dependency):
|
|
106 |
"message": f"Deleted book collection for user {user.get('id')}",
|
107 |
}
|
108 |
except Exception as e:
|
109 |
-
return
|
|
|
|
|
|
|
|
|
|
31 |
}
|
32 |
except Exception as e:
|
33 |
return {
|
34 |
+
"status": "error",
|
35 |
"book_collection": [],
|
36 |
+
"error_message": handle_exception(e)
|
37 |
}
|
38 |
|
39 |
|
|
|
52 |
return user_meta_query.insert_user_meta_entries(db, metadata_ids)
|
53 |
|
54 |
except Exception as e:
|
55 |
+
return {
|
56 |
+
"status": "error",
|
57 |
+
"message": "User meta entries failed to added.",
|
58 |
+
"metadata_ids": [], # Include the metadata IDs in the result
|
59 |
+
"error_message":handle_exception(e)
|
60 |
+
}
|
61 |
|
62 |
|
63 |
@router.put("/book_collection")
|
|
|
74 |
return user_meta_query.update_user_meta_entries(db, metadata_ids)
|
75 |
|
76 |
except Exception as e:
|
77 |
+
return {
|
78 |
+
"status": "error",
|
79 |
+
"added_meta": [],
|
80 |
+
"deleted_meta": [],
|
81 |
+
"kept_meta": [],
|
82 |
+
"message":handle_exception(e)
|
83 |
+
}
|
84 |
|
85 |
|
86 |
@router.delete("/book_collection/{metadata_id}")
|
|
|
98 |
db, metadata_id=metadata_id
|
99 |
)
|
100 |
except Exception as e:
|
101 |
+
return {
|
102 |
+
"status": "error",
|
103 |
+
"message": f"Book user with id {metadata_id} deleted successfully.",
|
104 |
+
"error_message": handle_exception(e)
|
105 |
+
}
|
106 |
|
107 |
|
108 |
@router.delete("/all_collections")
|
|
|
121 |
"message": f"Deleted book collection for user {user.get('id')}",
|
122 |
}
|
123 |
except Exception as e:
|
124 |
+
return {
|
125 |
+
"status": "error",
|
126 |
+
"message": f"Delete failed for user {user.get('id')}",
|
127 |
+
"error_message": handle_exception(e)
|
128 |
+
}
|
db/fetching.py
CHANGED
@@ -42,6 +42,7 @@ class DataFetching:
|
|
42 |
|
43 |
return [
|
44 |
MetadataResponse(
|
|
|
45 |
id = id,
|
46 |
title=title,
|
47 |
author=author,
|
|
|
42 |
|
43 |
return [
|
44 |
MetadataResponse(
|
45 |
+
status="success",
|
46 |
id = id,
|
47 |
title=title,
|
48 |
author=author,
|
db/query/base_query.py
CHANGED
@@ -49,10 +49,12 @@ class BaseQuery:
|
|
49 |
db.add_all(entries)
|
50 |
return self._handle_commit(db)
|
51 |
|
52 |
-
def delete(self, db, model, id, filter_conditions=None):
|
53 |
"""Delete an entry by ID with optional filter conditions."""
|
54 |
# Build the query to select the entry
|
55 |
-
|
|
|
|
|
56 |
if filter_conditions:
|
57 |
query = query.where(*filter_conditions)
|
58 |
|
|
|
49 |
db.add_all(entries)
|
50 |
return self._handle_commit(db)
|
51 |
|
52 |
+
def delete(self, db, model, id=None, filter_conditions=None):
|
53 |
"""Delete an entry by ID with optional filter conditions."""
|
54 |
# Build the query to select the entry
|
55 |
+
if id :
|
56 |
+
query = select(model).where(model.id == id)
|
57 |
+
|
58 |
if filter_conditions:
|
59 |
query = query.where(*filter_conditions)
|
60 |
|
db/query/query_user_meta.py
CHANGED
@@ -6,7 +6,7 @@ from db.query.base_query import BaseQuery
|
|
6 |
class UserMetaQuery(BaseQuery):
|
7 |
def __init__(self, user):
|
8 |
super().__init__(user)
|
9 |
-
|
10 |
def get_user_meta_entries(self, db):
|
11 |
"""Fetch all user meta entries joined with metadata and category."""
|
12 |
join_models = [Metadata, Category]
|
@@ -14,7 +14,7 @@ class UserMetaQuery(BaseQuery):
|
|
14 |
User_Meta.metadata_id == Metadata.id,
|
15 |
Metadata.category_id == Category.id,
|
16 |
]
|
17 |
-
|
18 |
filter_conditions = [User_Meta.user_id == self.user_id]
|
19 |
|
20 |
result = self.get_with_joins(
|
@@ -23,11 +23,10 @@ class UserMetaQuery(BaseQuery):
|
|
23 |
join_models=join_models,
|
24 |
join_conditions=join_conditions,
|
25 |
filter_conditions=filter_conditions,
|
26 |
-
multiple=True
|
27 |
)
|
28 |
return result
|
29 |
|
30 |
-
|
31 |
def insert_user_meta_entries(self, db, metadata_ids):
|
32 |
"""Insert new user meta entries."""
|
33 |
user_meta_entries = [
|
@@ -38,16 +37,19 @@ class UserMetaQuery(BaseQuery):
|
|
38 |
# Use the method from BaseQuery to insert entries
|
39 |
self.insert_entries(db, user_meta_entries)
|
40 |
return {
|
|
|
41 |
"message": "User meta entries added successfully.",
|
42 |
"metadata_ids": metadata_ids, # Include the metadata IDs in the result
|
43 |
}
|
44 |
-
|
45 |
def update_user_meta_entries(self, db, metadata_ids):
|
46 |
"""Update user meta entries: keep, delete, or add new entries based on metadata_ids."""
|
47 |
filter_conditions = [User_Meta.user_id == self.user_id]
|
48 |
-
|
49 |
# Fetch existing user meta entries
|
50 |
-
existing_user_meta = self.get(
|
|
|
|
|
51 |
existing_user_meta = [user_meta[0] for user_meta in existing_user_meta]
|
52 |
existing_meta_ids = [entry.metadata_id for entry in existing_user_meta]
|
53 |
|
@@ -62,7 +64,10 @@ class UserMetaQuery(BaseQuery):
|
|
62 |
|
63 |
# Delete entries that are no longer in the updated metadata_ids list
|
64 |
if metadata_to_delete:
|
65 |
-
db.query(User_Meta).filter(
|
|
|
|
|
|
|
66 |
|
67 |
# Add new entries for metadata that are not in the existing user meta
|
68 |
for meta_id in metadata_to_add:
|
@@ -70,7 +75,7 @@ class UserMetaQuery(BaseQuery):
|
|
70 |
self.add(db, new_entry)
|
71 |
|
72 |
db.commit()
|
73 |
-
|
74 |
return {
|
75 |
"status": "success",
|
76 |
"added_meta": list(metadata_to_add),
|
@@ -80,10 +85,15 @@ class UserMetaQuery(BaseQuery):
|
|
80 |
|
81 |
def delete_user_meta(self, db, metadata_id):
|
82 |
"""Delete user meta entries by metadata_id."""
|
83 |
-
filter_conditions = [
|
84 |
-
|
|
|
|
|
85 |
self.delete(db, model=User_Meta, filter_conditions=filter_conditions)
|
86 |
-
return {
|
|
|
|
|
|
|
87 |
|
88 |
def delete_all_user_meta(self, db):
|
89 |
"""Delete all user meta entries for a user."""
|
|
|
6 |
class UserMetaQuery(BaseQuery):
|
7 |
def __init__(self, user):
|
8 |
super().__init__(user)
|
9 |
+
|
10 |
def get_user_meta_entries(self, db):
|
11 |
"""Fetch all user meta entries joined with metadata and category."""
|
12 |
join_models = [Metadata, Category]
|
|
|
14 |
User_Meta.metadata_id == Metadata.id,
|
15 |
Metadata.category_id == Category.id,
|
16 |
]
|
17 |
+
|
18 |
filter_conditions = [User_Meta.user_id == self.user_id]
|
19 |
|
20 |
result = self.get_with_joins(
|
|
|
23 |
join_models=join_models,
|
24 |
join_conditions=join_conditions,
|
25 |
filter_conditions=filter_conditions,
|
26 |
+
multiple=True,
|
27 |
)
|
28 |
return result
|
29 |
|
|
|
30 |
def insert_user_meta_entries(self, db, metadata_ids):
|
31 |
"""Insert new user meta entries."""
|
32 |
user_meta_entries = [
|
|
|
37 |
# Use the method from BaseQuery to insert entries
|
38 |
self.insert_entries(db, user_meta_entries)
|
39 |
return {
|
40 |
+
"status":"success",
|
41 |
"message": "User meta entries added successfully.",
|
42 |
"metadata_ids": metadata_ids, # Include the metadata IDs in the result
|
43 |
}
|
44 |
+
|
45 |
def update_user_meta_entries(self, db, metadata_ids):
|
46 |
"""Update user meta entries: keep, delete, or add new entries based on metadata_ids."""
|
47 |
filter_conditions = [User_Meta.user_id == self.user_id]
|
48 |
+
|
49 |
# Fetch existing user meta entries
|
50 |
+
existing_user_meta = self.get(
|
51 |
+
db, model=User_Meta, filter_conditions=filter_conditions, multiple=True
|
52 |
+
)
|
53 |
existing_user_meta = [user_meta[0] for user_meta in existing_user_meta]
|
54 |
existing_meta_ids = [entry.metadata_id for entry in existing_user_meta]
|
55 |
|
|
|
64 |
|
65 |
# Delete entries that are no longer in the updated metadata_ids list
|
66 |
if metadata_to_delete:
|
67 |
+
db.query(User_Meta).filter(
|
68 |
+
User_Meta.user_id == self.user_id,
|
69 |
+
User_Meta.metadata_id.in_(metadata_to_delete),
|
70 |
+
).delete(synchronize_session=False)
|
71 |
|
72 |
# Add new entries for metadata that are not in the existing user meta
|
73 |
for meta_id in metadata_to_add:
|
|
|
75 |
self.add(db, new_entry)
|
76 |
|
77 |
db.commit()
|
78 |
+
|
79 |
return {
|
80 |
"status": "success",
|
81 |
"added_meta": list(metadata_to_add),
|
|
|
85 |
|
86 |
def delete_user_meta(self, db, metadata_id):
|
87 |
"""Delete user meta entries by metadata_id."""
|
88 |
+
filter_conditions = [
|
89 |
+
User_Meta.metadata_id == metadata_id,
|
90 |
+
User_Meta.user_id == self.user_id,
|
91 |
+
]
|
92 |
self.delete(db, model=User_Meta, filter_conditions=filter_conditions)
|
93 |
+
return {
|
94 |
+
"status": "success",
|
95 |
+
"message": f"Book user with id {metadata_id} deleted successfully.",
|
96 |
+
}
|
97 |
|
98 |
def delete_all_user_meta(self, db):
|
99 |
"""Delete all user meta entries for a user."""
|
service/dto.py
CHANGED
@@ -69,6 +69,7 @@ class MetadataRequest(BaseModel):
|
|
69 |
|
70 |
|
71 |
class MetadataResponse(BaseModel):
|
|
|
72 |
id : int
|
73 |
title: str
|
74 |
author: str
|
|
|
69 |
|
70 |
|
71 |
class MetadataResponse(BaseModel):
|
72 |
+
status:str
|
73 |
id : int
|
74 |
title: str
|
75 |
author: str
|
utils/error_handlers.py
CHANGED
@@ -4,17 +4,31 @@ from fastapi.responses import JSONResponse
|
|
4 |
def handle_exception(e: Exception):
|
5 |
"""Helper function to handle exceptions in a consistent way."""
|
6 |
return JSONResponse(
|
7 |
-
status_code=500,
|
|
|
|
|
|
|
|
|
|
|
8 |
)
|
9 |
|
10 |
|
11 |
def handle_error(e, message):
|
12 |
-
return JSONResponse(
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
def not_found_error(message):
|
16 |
-
return JSONResponse(
|
|
|
|
|
|
|
17 |
|
18 |
|
19 |
def no_entries_found(message):
|
20 |
-
return JSONResponse(
|
|
|
|
|
|
|
|
4 |
def handle_exception(e: Exception):
|
5 |
"""Helper function to handle exceptions in a consistent way."""
|
6 |
return JSONResponse(
|
7 |
+
status_code=500,
|
8 |
+
content={
|
9 |
+
"status": "exception error",
|
10 |
+
"message": "An unexpected error occurred",
|
11 |
+
"details": str(e),
|
12 |
+
},
|
13 |
)
|
14 |
|
15 |
|
16 |
def handle_error(e, message):
|
17 |
+
return JSONResponse(
|
18 |
+
status_code=500,
|
19 |
+
content={"status": "error", "error": f"error in {message}", "details": str(e)},
|
20 |
+
)
|
21 |
|
22 |
|
23 |
def not_found_error(message):
|
24 |
+
return JSONResponse(
|
25 |
+
status_code=404,
|
26 |
+
content={"status": "error", "error": "Not Found", "details": message},
|
27 |
+
)
|
28 |
|
29 |
|
30 |
def no_entries_found(message):
|
31 |
+
return JSONResponse(
|
32 |
+
status_code=404,
|
33 |
+
content={"status": "error", "error": "No Entries Found", "details": message},
|
34 |
+
)
|