From 865feb7b43c72abf8e74d29fae7930615bc19a3b Mon Sep 17 00:00:00 2001 From: alwil17 Date: Wed, 4 Jun 2025 16:18:32 +0000 Subject: [PATCH 1/4] feat: enhance category creation and update endpoints with DTOs --- app/api/endpoints/category_endpoints.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/api/endpoints/category_endpoints.py b/app/api/endpoints/category_endpoints.py index 496bbfb..3658390 100644 --- a/app/api/endpoints/category_endpoints.py +++ b/app/api/endpoints/category_endpoints.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, Depends, HTTPException, Path from sqlalchemy.orm import Session from app.application.services.category_service import CategoryService -from app.application.schemas.category_dto import CategoryDTO +from app.application.schemas.category_dto import CategoryDTO, CategoryCreateDTO, CategoryUpdateDTO from app.infrastructure.database import get_db router = APIRouter(prefix="/categories", tags=["Categories"]) @@ -11,10 +11,9 @@ def list_categories(db: Session = Depends(get_db)): return CategoryService(db).list_categories() @router.post("", response_model=CategoryDTO, status_code=201) -def create_category(name: str, db: Session = Depends(get_db)): - return CategoryService(db).create_category(name) +def create_category(category: CategoryCreateDTO, db: Session = Depends(get_db)): + return CategoryService(db).create_category(category.name, category.description) -# Get a specific category by ID @router.get("/{category_id}", response_model=CategoryDTO) def get_category( category_id: int = Path(..., title="The ID of the category to get"), @@ -25,20 +24,17 @@ def get_category( raise HTTPException(status_code=404, detail="Category not found") return category -# Update a category @router.put("/{category_id}", response_model=CategoryDTO) def update_category( + category: CategoryUpdateDTO, category_id: int = Path(..., title="The ID of the category to update"), - name: str = None, - description: str = None, db: Session = Depends(get_db) ): - category = CategoryService(db).get_category(category_id) - if not category: + result = CategoryService(db).get_category(category_id) + if not result: raise HTTPException(status_code=404, detail="Category not found") - return CategoryService(db).update_category(category_id, name, description) + return CategoryService(db).update_category(category_id, category.name, category.description) -# Delete a category @router.delete("/{category_id}", status_code=204) def delete_category( category_id: int = Path(..., title="The ID of the category to delete"), From e8d0c9903afa56d0c8a66ba1e68509f5c5d7949c Mon Sep 17 00:00:00 2001 From: alwil17 Date: Wed, 4 Jun 2025 16:18:39 +0000 Subject: [PATCH 2/4] feat: add CategoryCreateDTO and CategoryUpdateDTO schemas for category management --- app/application/schemas/category_dto.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/application/schemas/category_dto.py b/app/application/schemas/category_dto.py index 96a0703..54fd356 100644 --- a/app/application/schemas/category_dto.py +++ b/app/application/schemas/category_dto.py @@ -6,4 +6,12 @@ class CategoryDTO(BaseModel): name: str description: Optional[str] = None - model_config = ConfigDict(from_attributes=True) \ No newline at end of file + model_config = ConfigDict(from_attributes=True) + +class CategoryCreateDTO(BaseModel): + name: str + description: Optional[str] = None + +class CategoryUpdateDTO(BaseModel): + name: Optional[str] = None + description: Optional[str] = None \ No newline at end of file From 8623bba0586ae907aab85e965cdb959ad4be992b Mon Sep 17 00:00:00 2001 From: alwil17 Date: Wed, 4 Jun 2025 16:18:45 +0000 Subject: [PATCH 3/4] feat: update create_category method to include optional description parameter --- app/application/services/category_service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/application/services/category_service.py b/app/application/services/category_service.py index 10f15cf..2a1a2dc 100644 --- a/app/application/services/category_service.py +++ b/app/application/services/category_service.py @@ -7,8 +7,8 @@ def __init__(self, db_session): def list_categories(self): return self.repo.list() - def create_category(self, name: str): - return self.repo.create(name) + def create_category(self, name: str, description: str = None): + return self.repo.create(name, description) # Get a specific category by ID def get_category(self, category_id: int): From 071105ad0defc568e7b8ed491b1e94db7d9bc57a Mon Sep 17 00:00:00 2001 From: alwil17 Date: Wed, 4 Jun 2025 16:18:52 +0000 Subject: [PATCH 4/4] feat: update create method in CategoryRepository to include optional description parameter --- app/infrastructure/repositories/category_repository.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/infrastructure/repositories/category_repository.py b/app/infrastructure/repositories/category_repository.py index c374f1f..308e4aa 100644 --- a/app/infrastructure/repositories/category_repository.py +++ b/app/infrastructure/repositories/category_repository.py @@ -11,8 +11,8 @@ def list(self): def get(self, id: int): return self.db.query(Category).filter(Category.id == id).first() - def create(self, name: str): - cat = Category(name=name) + def create(self, name: str, description: str = None): + cat = Category(name=name, description=description) self.db.add(cat) self.db.commit() self.db.refresh(cat)