Skip to content

Commit

Permalink
feat: Refactor task retrieval methods and enhance error handling in t…
Browse files Browse the repository at this point in the history
…ask creation
  • Loading branch information
jmcerrejon committed Nov 17, 2024
1 parent 0514d3f commit dfe1fda
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/tasks/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_task_list(session: Session):


def get_task(id: int, session: Session):
task = session.query(models.Task).get(id)
task = session.get(models.Task, id)
if task is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=f"Task with id {id} not found"
Expand All @@ -27,15 +27,21 @@ def get_task(id: int, session: Session):


def create_task(task: schemas.TaskCreate, session: Session):
task_db = models.Task(task=task.task)
session.add(task_db)
session.commit()
session.refresh(task_db)
return schemas.OkResponse(**task_db.__dict__)
try:
task_db = models.Task(task=task.task)
session.add(task_db)
session.commit()
session.refresh(task_db)
return schemas.OkResponse(**task_db.__dict__)
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error creating task: {str(e)}",
)


def update_task(id: int, task: str, session: Session):
task_db = session.query(models.Task).get(id)
task_db = session.get(models.Task, id)
if task_db:
task_db.task = task
session.commit()
Expand All @@ -45,7 +51,7 @@ def update_task(id: int, task: str, session: Session):


def delete_task(id: int, session: Session):
task_db = session.query(models.Task).get(id)
task_db = session.get(models.Task, id)
if not task_db:
raise HTTPException(status_code=404, detail=f"Task with id {id} not found")
session.delete(task_db)
Expand Down

0 comments on commit dfe1fda

Please sign in to comment.