Skip to content

Commit

Permalink
rename api routers
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-hv committed Mar 20, 2024
1 parent ebd8f72 commit 34f2555
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions app/routers/api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from app.settings import router


@router.get("/tasks/")
def get_tasks(priority: Optional[str] = None, date: Optional[datetime] = None, db: Session = Depends(get_db)):
@router.get("/api/tasks/")
def api_get_tasks(priority: Optional[str] = None, date: Optional[datetime] = None, db: Session = Depends(get_db)):
# Store argument names in a tuple
arg_names = ('priority', 'date')
# Get the arguments passed in the URL
Expand All @@ -24,23 +24,35 @@ def get_tasks(priority: Optional[str] = None, date: Optional[datetime] = None, d
return tasks


@router.get("/tasks/{task_id}")
def get_task(task_id: int, db: Session = Depends(get_db)):
@router.get("/api/tasks/{task_id}")
def api_get_task(task_id: int, db: Session = Depends(get_db)):
# Get the task from the database using the task_id
task = db.query(models.Task).filter(models.Task.id == task_id).first()
# Return the task
return task


@router.post("/tasks/add/", response_model=schemas.Task)
def add_task(task: schemas.TaskCreate, db: Session = Depends(get_db)):
@router.post("/api/tasks/add/", response_model=schemas.Task)
def api_add_task(task: schemas.TaskCreate, db: Session = Depends(get_db)):
# Create a new task object using the task schema
db_task = models.Task(**task.model_dump())
if 'model_dump' in dir(task):
db_task = models.Task(**task.model_dump())
else:
db_task = models.Task(**task)
# Add the task to the database
db.add(db_task)
# Commit the changes to the database
db.commit()
# Refresh the task object
db.refresh(db_task)
# Return the task
return db_task
return db_task


@router.get('/api/tasks/delete/{task_id}')
def api_delete_task(task_id: int, db: Session = Depends(get_db)):
db.query(models.Task).filter(models.Task.id == task_id).delete()
db.commit()

return {"status": 200,
"message": "Task deleted successfully"}

0 comments on commit 34f2555

Please sign in to comment.