Skip to content

Commit

Permalink
Added code to update topics
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilRaikar17 committed Jan 18, 2025
1 parent 6730072 commit e8dfdea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
31 changes: 30 additions & 1 deletion backend/api/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -57,4 +60,30 @@ async def create_topics(
status_code=500,
detail="Internal Server error",
headers={"WWW-Authenticate": "Bearer"},
)
)


@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"},
)
5 changes: 5 additions & 0 deletions backend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 20 additions & 6 deletions backend/service/topics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
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]:
"""Get list of all Users"""

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,
Expand All @@ -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}
)
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})

0 comments on commit e8dfdea

Please sign in to comment.