diff --git a/backend/api/topics.py b/backend/api/topics.py index c71d352..8d2bc94 100644 --- a/backend/api/topics.py +++ b/backend/api/topics.py @@ -7,7 +7,9 @@ from backend.service.oauth import get_current_user from backend.service.topics_service import get_all_topics from backend.service.topics_service import created_topic +from backend.service.topics_service import update_topic from backend.exceptions import DuplicateUserException +from backend.exceptions import NoTopicFoundException topic_router = APIRouter( @@ -36,6 +38,7 @@ async def get_topics( headers={"WWW-Authenticate": "Bearer"}, ) + @topic_router.post("/", dependencies=[Depends(get_current_user)]) async def create_topics( name: str, @@ -57,4 +60,30 @@ async def create_topics( status_code=500, detail="Internal Server error", headers={"WWW-Authenticate": "Bearer"}, - ) \ No newline at end of file + ) + + +@topic_router.put("/", dependencies=[Depends(get_current_user)]) +async def create_topics( + old_name: str, + name: str, + db: Session = Depends(get_db), +): + """Create a new topic""" + try: + return update_topic( + db=db, + old_name=old_name, + name=name, + ) + + except NoTopicFoundException as e: + raise HTTPException(status_code=403, detail=str(e)) + + except Exception as e: + print(e) + raise HTTPException( + status_code=500, + detail="Internal Server error", + headers={"WWW-Authenticate": "Bearer"}, + ) diff --git a/backend/exceptions.py b/backend/exceptions.py index 44a69ab..3fd8180 100644 --- a/backend/exceptions.py +++ b/backend/exceptions.py @@ -32,3 +32,8 @@ class ModelsNotRetrievedException(Exception): def __init__(self): self.message = "No Ollama Models found" super().__init__(self.message) + +class NoTopicFoundException(Exception): + def __init__(self): + self.message = "No Topic found" + super().__init__(self.message) diff --git a/backend/service/topics_service.py b/backend/service/topics_service.py index b4de583..c25293b 100644 --- a/backend/service/topics_service.py +++ b/backend/service/topics_service.py @@ -5,6 +5,7 @@ from backend.models.pydantic_models import TopicPydantic from backend.models.sqlalchemy_models import Topic from backend.exceptions import DuplicateUserException +from backend.exceptions import NoTopicFoundException def get_all_topics(db: Session) -> List[TopicPydantic]: @@ -12,12 +13,14 @@ def get_all_topics(db: Session) -> List[TopicPydantic]: return [TopicPydantic(name=topic.name) for topic in db.query(Topic).all()] + def get_topic_by_name(name: str, db: Session) -> Topic | None: """Get user by name""" topic = db.query(Topic).filter(Topic.name == name).first() return topic if topic else None + def created_topic( db: Session, name: str, @@ -28,11 +31,22 @@ def created_topic( if topic_exists: raise DuplicateUserException() - topic = Topic( - name=name - ) + topic = Topic(name=name) db.add(topic) db.commit() - return TopicPydantic.model_validate( - {"name": topic.name} - ) \ No newline at end of file + return TopicPydantic.model_validate({"name": topic.name}) + + +def update_topic( + db: Session, + old_name: str, + name: str, +) -> TopicPydantic: + """Update a topic based on the information passed""" + topic_exists = get_topic_by_name(old_name, db) + if not topic_exists: + raise NoTopicFoundException() + + topic_exists.name = name + db.commit() + return TopicPydantic.model_validate({"name": topic_exists.name})