From ec0075993e218fe485b05e39a5a543dd9e177e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Thu, 10 Oct 2024 23:15:49 +0200 Subject: [PATCH] remove task --- api/endpotins.go | 1 + api/task/task.go | 27 +++++++++++++++++++++++++++ database/task/task.go | 10 ++++++++++ 3 files changed, 38 insertions(+) diff --git a/api/endpotins.go b/api/endpotins.go index 619d5dd..b637148 100644 --- a/api/endpotins.go +++ b/api/endpotins.go @@ -30,6 +30,7 @@ func Listen(address string, path string) { apiAdmin := api.Group("admin").Use(auth.LoggedIn).Use(auth.IsAdmin) { apiAdmin.POST("task/add", task.Add) + apiAdmin.POST("task/remove/:id", task.Remove) } apiTest := api.Group("test").Use(auth.LoggedIn).Use(auth.IsAdmin) diff --git a/api/task/task.go b/api/task/task.go index fe456d1..fdac55c 100644 --- a/api/task/task.go +++ b/api/task/task.go @@ -2,6 +2,7 @@ package task import ( "net/http" + "strconv" "git.tek.govt.hu/dowerx/szoe-pontok/database/task" "git.tek.govt.hu/dowerx/szoe-pontok/model" @@ -49,3 +50,29 @@ func Add(c *gin.Context) { }) } } + +func Remove(c *gin.Context) { + id := c.Param("id") + + idInt, err := strconv.Atoi(id) + + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": http.StatusBadRequest, + "error": err.Error(), + }) + return + } + + if err := task.Remove(idInt); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "status": http.StatusBadRequest, + "error": err.Error(), + }) + } else { + c.JSON(http.StatusOK, gin.H{ + "status": http.StatusOK, + "message": "removed", + }) + } +} diff --git a/database/task/task.go b/database/task/task.go index 33a3dc8..46cb4a8 100644 --- a/database/task/task.go +++ b/database/task/task.go @@ -17,3 +17,13 @@ func Add(task model.Task) error { return err } +func Remove(id int) error { + db := database.GetDB() + + _, err := db.NamedExec( + `delete from "task" where "id" = :id`, map[string]interface{}{ + "id": id, + }) + + return err +}