Skip to content

Commit

Permalink
Implemented delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
pacholoamit committed Jun 24, 2022
1 parent 36580f9 commit 9bb21d6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
12 changes: 12 additions & 0 deletions pkg/controllers/task.controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,15 @@ func GetTask(c echo.Context) error {

return c.JSON(http.StatusFound, task)
}

func DeleteTask(c echo.Context) error {
qp := c.Param("id")
id, err := strconv.Atoi(qp)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "You have provided an invalid ID")
}
models.DeleteTask(id)


return c.String(http.StatusAccepted, "Successfully deleted Task: " + qp)
}
10 changes: 8 additions & 2 deletions pkg/models/tasks.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ func GetAllTasks() []Task {
}

// Todo: Error handling if ID is not found
func GetTask(id int) Task {
var task Task
func GetTask(id int) *Task {
var task *Task
db.Find(&task, id)
return task
}

func DeleteTask(id int) *Task {
var task *Task
db.Delete(&task, id)
return task
}
2 changes: 1 addition & 1 deletion pkg/routes/tasks.routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ var TaskRoutes = func(router *echo.Echo) {
router.POST("/task", controllers.CreateTask)
router.GET("/task/:id", controllers.GetTask)
// router.PUT("/task/:id", controllers.UpdateTask)
// router.DELETE("/task/:id", controllers.DeleteTask)
router.DELETE("/task/:id", controllers.DeleteTask)
}

0 comments on commit 9bb21d6

Please sign in to comment.