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
44 changes: 40 additions & 4 deletions app/api/endpoints/tag_endpoints.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 13 additions & 5 deletions app/application/schemas/tag_dto.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict, 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)

model_config = ConfigDict(from_attributes=True, extra='allow')

class TagCreateDTO(TagBaseDTO):
pass

class TagUpdateDTO(TagBaseDTO):
pass
23 changes: 23 additions & 0 deletions app/application/services/tag_service.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 16 additions & 8 deletions app/infrastructure/repositories/tag_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()