From c35205fac8b85712eb6ec1491d86c8638460ecb7 Mon Sep 17 00:00:00 2001 From: alwil17 Date: Thu, 5 Jun 2025 08:46:42 +0000 Subject: [PATCH 1/5] refactor: restructure TagDTO and introduce TagBaseDTO for better schema management --- app/application/schemas/tag_dto.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/application/schemas/tag_dto.py b/app/application/schemas/tag_dto.py index 4984dd8..728a00c 100644 --- a/app/application/schemas/tag_dto.py +++ b/app/application/schemas/tag_dto.py @@ -1,7 +1,16 @@ -from pydantic import BaseModel +from pydantic import BaseModel, Field -class TagDTO(BaseModel): - id: int - name: str +class TagBaseDTO(BaseModel): + name: str = Field(..., example="Technology") - model_config = {"from_attributes": True} +class TagDTO(TagBaseDTO): + id: int = Field(..., example=1) + + class Config: + orm_mode = True + +class TagCreateDTO(TagBaseDTO): + pass + +class TagUpdateDTO(TagBaseDTO): + pass From c15a959e740f318ad0fa02968b580522dee8ec20 Mon Sep 17 00:00:00 2001 From: alwil17 Date: Thu, 5 Jun 2025 08:47:05 +0000 Subject: [PATCH 2/5] refactor: remove unused code and improve repository structure --- .../repositories/tag_repository.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/app/infrastructure/repositories/tag_repository.py b/app/infrastructure/repositories/tag_repository.py index 24b2932..7d92d27 100644 --- a/app/infrastructure/repositories/tag_repository.py +++ b/app/infrastructure/repositories/tag_repository.py @@ -5,8 +5,11 @@ class TagRepository: def __init__(self, db: Session): self.db = db - def get_by_name(self, name: str): - return self.db.query(Tag).filter(Tag.name == name).first() + def list(self): + return self.db.query(Tag).all() + + def get(self, tag_id: int): + return self.db.query(Tag).filter(Tag.id == tag_id).first() def create(self, name: str): tag = Tag(name=name) @@ -15,11 +18,16 @@ def create(self, name: str): self.db.refresh(tag) return tag - def get_or_create(self, name: str): - tag = self.get_by_name(name) + def update(self, tag_id: int, name: str): + tag = self.get(tag_id) if tag: - return tag - return self.create(name) + tag.name = name + self.db.commit() + self.db.refresh(tag) + return tag - def list(self): - return self.db.query(Tag).all() + def delete(self, tag_id: int): + tag = self.get(tag_id) + if tag: + self.db.delete(tag) + self.db.commit() From 53ff4f078d68f341c60f4f2b0ed019697c3d0dea Mon Sep 17 00:00:00 2001 From: alwil17 Date: Thu, 5 Jun 2025 08:47:19 +0000 Subject: [PATCH 3/5] feat: implement TagService for tag management functionality --- app/application/services/tag_service.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app/application/services/tag_service.py diff --git a/app/application/services/tag_service.py b/app/application/services/tag_service.py new file mode 100644 index 0000000..16c236f --- /dev/null +++ b/app/application/services/tag_service.py @@ -0,0 +1,23 @@ +from sqlalchemy.orm import Session +from app.infrastructure.repositories.tag_repository import TagRepository +from app.application.schemas.tag_dto import TagDTO + +class TagService: + def __init__(self, db: Session): + self.db = db + self.repository = TagRepository(db) + + def list_tags(self) -> list[TagDTO]: + return self.repository.list() + + def get_tag(self, tag_id: int) -> TagDTO: + return self.repository.get(tag_id) + + def create_tag(self, name: str) -> TagDTO: + return self.repository.create(name) + + def update_tag(self, tag_id: int, name: str) -> TagDTO: + return self.repository.update(tag_id, name) + + def delete_tag(self, tag_id: int) -> None: + self.repository.delete(tag_id) From 6d7284e0e177663e3efcf37eb4ad55de12d3fb39 Mon Sep 17 00:00:00 2001 From: alwil17 Date: Thu, 5 Jun 2025 08:47:28 +0000 Subject: [PATCH 4/5] feat: enhance tag endpoints with CRUD operations and error handling --- app/api/endpoints/tag_endpoints.py | 44 +++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/app/api/endpoints/tag_endpoints.py b/app/api/endpoints/tag_endpoints.py index 40f50e8..2f529ef 100644 --- a/app/api/endpoints/tag_endpoints.py +++ b/app/api/endpoints/tag_endpoints.py @@ -1,11 +1,47 @@ -from fastapi import APIRouter, Depends +from fastapi import APIRouter, Depends, HTTPException, Path from sqlalchemy.orm import Session -from app.application.schemas.tag_dto import TagDTO +from app.application.schemas.tag_dto import TagDTO, TagCreateDTO, TagUpdateDTO +from app.application.services.tag_service import TagService from app.infrastructure.database import get_db -from app.infrastructure.repositories.tag_repository import TagRepository router = APIRouter(prefix="/tags", tags=["Tags"]) @router.get("", response_model=list[TagDTO]) def list_tags(db: Session = Depends(get_db)): - return TagRepository(db).list() + return TagService(db).list_tags() + +@router.post("", response_model=TagDTO, status_code=201) +def create_tag(tag: TagCreateDTO, db: Session = Depends(get_db)): + return TagService(db).create_tag(tag.name) + +@router.get("/{tag_id}", response_model=TagDTO) +def get_tag( + tag_id: int = Path(..., title="The ID of the tag to get"), + db: Session = Depends(get_db) +): + tag = TagService(db).get_tag(tag_id) + if not tag: + raise HTTPException(status_code=404, detail="Tag not found") + return tag + +@router.put("/{tag_id}", response_model=TagDTO) +def update_tag( + tag: TagUpdateDTO, + tag_id: int = Path(..., title="The ID of the tag to update"), + db: Session = Depends(get_db) +): + result = TagService(db).get_tag(tag_id) + if not result: + raise HTTPException(status_code=404, detail="Tag not found") + return TagService(db).update_tag(tag_id, tag.name) + +@router.delete("/{tag_id}", status_code=204) +def delete_tag( + tag_id: int = Path(..., title="The ID of the tag to delete"), + db: Session = Depends(get_db) +): + tag = TagService(db).get_tag(tag_id) + if not tag: + raise HTTPException(status_code=404, detail="Tag not found") + TagService(db).delete_tag(tag_id) + return None From 1453d1f7a477a5949acb2f496c7e1999e96dbff7 Mon Sep 17 00:00:00 2001 From: alwil17 Date: Thu, 5 Jun 2025 09:07:20 +0000 Subject: [PATCH 5/5] refactor: update TagDTO configuration for improved attribute handling --- app/application/schemas/tag_dto.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/application/schemas/tag_dto.py b/app/application/schemas/tag_dto.py index 728a00c..ee6cbfd 100644 --- a/app/application/schemas/tag_dto.py +++ b/app/application/schemas/tag_dto.py @@ -1,4 +1,4 @@ -from pydantic import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field class TagBaseDTO(BaseModel): name: str = Field(..., example="Technology") @@ -6,8 +6,7 @@ class TagBaseDTO(BaseModel): class TagDTO(TagBaseDTO): id: int = Field(..., example=1) - class Config: - orm_mode = True + model_config = ConfigDict(from_attributes=True, extra='allow') class TagCreateDTO(TagBaseDTO): pass