From ba6e5a9c17d3c749df185b25992044ccdf7100bc Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Wed, 6 Apr 2022 21:11:27 +0200 Subject: [PATCH] delete in the backend --- Lab01/app/database/database.go | 4 ++++ Lab01/app/templates/tasks.tmpl | 12 +++++++++++- Lab01/app/webpage/webpage.go | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Lab01/app/database/database.go b/Lab01/app/database/database.go index a9eb9ed..c6d3218 100644 --- a/Lab01/app/database/database.go +++ b/Lab01/app/database/database.go @@ -45,6 +45,10 @@ func (db *Database) CreateTask(username string, description string) Task { return task } +func (db *Database) DeleteTask(id string) { + db.ORM.Delete(&Task{}, id) +} + func (db *Database) CreateUser(username string, password string) error { user := User{Username: username, Password: password} result := db.ORM.Create(&user) diff --git a/Lab01/app/templates/tasks.tmpl b/Lab01/app/templates/tasks.tmpl index 89eb3df..7a1608a 100644 --- a/Lab01/app/templates/tasks.tmpl +++ b/Lab01/app/templates/tasks.tmpl @@ -48,6 +48,16 @@ }); } + function deleteTask(id) { + axios.delete("/tasks/" + id, axiosConfig) + .then((response) => { + console.log(response); + }) + .catch((err) => { + showErrorToast(err.response.data.message); + }); + } + function addTaskToTasks(task) { const newTask = document.createElement('div'); newTask.classList.add('row', 'g-0', 'align-items-center', 'mb-1'); @@ -57,7 +67,7 @@
- + `; tasksEl.appendChild(newTask); } diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go index 7a39ab2..061e2da 100644 --- a/Lab01/app/webpage/webpage.go +++ b/Lab01/app/webpage/webpage.go @@ -55,6 +55,13 @@ func (wp *Webpage) defineRoutes() { c.JSON(200, gin.H{"task": task}) } }) + tasks.DELETE(":id", func(c *gin.Context) { + if wp.isLoggedInMiddleware(c) { + id := c.Param("id") + wp.Database.DeleteTask(id) + c.JSON(200, gin.H{"message": "success", "id": id}) + } + }) } auth := wp.Router.Group("/auth")