Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions app/api/endpoints/category_endpoints.py
Original file line number Diff line number Diff line change
@@ -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"])
Expand All @@ -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"),
Expand All @@ -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"),
Expand Down
10 changes: 9 additions & 1 deletion app/application/schemas/category_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ class CategoryDTO(BaseModel):
name: str
description: Optional[str] = None

model_config = ConfigDict(from_attributes=True)
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
4 changes: 2 additions & 2 deletions app/application/services/category_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions app/infrastructure/repositories/category_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down