From 3bf957ee30fc6338d7ddf686d6a5f063dbc049ff Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Thu, 7 Apr 2022 16:05:43 +0200 Subject: [PATCH] tasks can be marked as finished --- Lab01/app/database/database.go | 18 ++++++++++++++++-- Lab01/app/templates/tasks.tmpl | 11 +++++++++-- Lab01/app/webpage/webpage.go | 22 +++++++++++++++++++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Lab01/app/database/database.go b/Lab01/app/database/database.go index df27a29..014d6ae 100644 --- a/Lab01/app/database/database.go +++ b/Lab01/app/database/database.go @@ -45,8 +45,22 @@ 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) DeleteTask(id string) bool { + result := db.ORM.Delete(&Task{}, id) + if result.RowsAffected == 0 { + return false + } + return true +} + +func (db *Database) UpdateTask(id string, done string) bool { + task := Task{} + db.ORM.First(&task, id) + result := db.ORM.Model(&task).Update("done", done) + if result.RowsAffected == 0 { + return false + } + return true } func (db *Database) CreateUser(username string, password string) error { diff --git a/Lab01/app/templates/tasks.tmpl b/Lab01/app/templates/tasks.tmpl index 971c200..52e3d05 100644 --- a/Lab01/app/templates/tasks.tmpl +++ b/Lab01/app/templates/tasks.tmpl @@ -50,7 +50,7 @@ } function deleteTask(id) { - axios.delete("/tasks/" + id, axiosConfig) + axios.delete("/tasks", {params: {id: id}, headers: {username: username}}) .then(() => { getAllTasks(); }) @@ -59,6 +59,13 @@ }); } + function updateTask(id, done) { + axios.put("/tasks", null, {params: {id: id, done: done}, headers: {username: username}}) + .catch((err) => { + showErrorToast(err.response.data.message); + }); + } + function addTaskToTasks(task, number) { tasks.push(task); const newTask = document.createElement('div'); @@ -68,7 +75,7 @@
${number}
${task.Description}
- +
`; diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go index d47e0ff..4646945 100644 --- a/Lab01/app/webpage/webpage.go +++ b/Lab01/app/webpage/webpage.go @@ -55,10 +55,26 @@ func (wp *Webpage) defineRoutes() { c.JSON(200, gin.H{"task": task}) } }) - tasks.DELETE(":id", func(c *gin.Context) { + tasks.DELETE("", func(c *gin.Context) { if wp.isLoggedInMiddleware(c) { - id := c.Param("id") - wp.Database.DeleteTask(id) + id := c.Query("id") + ok := wp.Database.DeleteTask(id) + if !ok { + c.JSON(500, gin.H{"message": "cannot delete task"}) + return + } + c.JSON(200, gin.H{"message": "success"}) + } + }) + tasks.PUT("", func(c *gin.Context) { + if wp.isLoggedInMiddleware(c) { + id := c.Query("id") + done := c.Query("done") + ok := wp.Database.UpdateTask(id, done) + if !ok { + c.JSON(500, gin.H{"message": "cannot update task"}) + return + } c.JSON(200, gin.H{"message": "success"}) } })