dsmultimedika's picture
fix : update code
0767396
from api.router.user import user_dependency
from fastapi.responses import JSONResponse
from fastapi import APIRouter, Depends
from db.models import Category
from db.database import get_db
from db.query.query_category import CategoryQuery
from service.dto import CategoryCreate
from script.vector_db import IndexManager
from sqlalchemy.orm import Session
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
from typing import Annotated
router = APIRouter(tags=["Category"])
index_manager = IndexManager()
db_dependency = Annotated[Session, Depends(get_db)]
@router.get("/category")
async def get_all_categories_router(user: user_dependency, db: db_dependency):
if user is None or user.get("role_id") != 1:
return JSONResponse(status_code=401, content="Authentication Failed")
try:
# Logic to retrieve all categories
category_query = CategoryQuery(user)
categories = category_query.get_all_categories(db)
print(categories)
# categories = db.query(Category).all()
if not categories:
return JSONResponse(status_code=404, content="No categories found")
return {
"message": "Categories retrieved successfully",
"categories": [
{"id": cat.id, "category": cat.category} for cat in categories
],
}
except SQLAlchemyError as e:
return JSONResponse(
status_code=500, content="Database error occurred: " + str(e)
)
@router.get("/category/{category_id}")
async def get_category_by_id_router(
user: user_dependency,
db: db_dependency,
category_id: int,
):
if user is None or user.get("role_id") != 1:
return JSONResponse(status_code=401, content="Authentication Failed")
try:
# Fetch categories based on the list of provided category_ids
category_query = CategoryQuery(user)
category = category_query.get_category_by_id(db, category_id)
if category is None:
return JSONResponse(status_code=404, content="No categories found for the given IDs")
return {
"message": "Categories retrieved successfully",
"category": {"id": category.id, "category": category.category},
}
except SQLAlchemyError as e:
return JSONResponse(
status_code=500, content="Database error occurred: " + str(e)
)
@router.post("/category")
async def create_category(user: user_dependency, db: db_dependency, category: CategoryCreate):
if user is None or user.get("role_id") != 1:
return JSONResponse(status_code=401, content="Authentication Failed")
try:
# Check if category already exists
category_query = CategoryQuery(user)
existing_category = category_query.get_existing_category(db, category.category_name)
print(existing_category)
if existing_category: # Check if the category already exists
return JSONResponse(status_code=400, content={"error": "Category already exists"})
# Add category
category_query.add_category(db, category.category_name)
print("category added")
return {
"message": "Category created successfully",
}
except IntegrityError:
db.rollback()
return JSONResponse(
status_code=400,
content="Database integrity error: possibly a duplicate entry.",
)
@router.put("/category/{category_id}")
async def update_category(
user: user_dependency, db: db_dependency, category_id: int, category: CategoryCreate
):
if user is None or user.get("role_id") != 1:
return JSONResponse(status_code=401, content="Authentication Failed")
try:
# Logic to update an existing category
existing_category = (
db.query(Category).filter(Category.id == category_id).first()
)
if not existing_category:
return JSONResponse(status_code=404, content="Category not found")
existing_category.category = category.category_name
db.commit()
return {"message": "Category updated successfully"}
except SQLAlchemyError as e:
db.rollback()
return JSONResponse(
status_code=500, content="Database error occurred: " + str(e)
)
@router.delete("/category/{category_id}")
async def delete_category(user: user_dependency, db: db_dependency, category_id: int):
if user is None or user.get("role_id") != 1:
return JSONResponse(status_code=401, content="Authentication Failed")
try:
# Logic to delete an existing category
existing_category = (
db.query(Category).filter(Category.id == category_id).first()
)
if not existing_category:
return JSONResponse(status_code=404, content="Category not found")
db.delete(existing_category)
db.commit()
return {"message": "Category deleted successfully"}
except SQLAlchemyError as e:
db.rollback()
return JSONResponse(
status_code=500, content="Database error occurred: " + str(e)
)