tasks can be marked as finished

This commit is contained in:
Florian Hoss 2022-04-07 16:05:43 +02:00
parent 5e63fc1e38
commit 3bf957ee30
3 changed files with 44 additions and 7 deletions

View file

@ -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 {

View file

@ -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 @@
<div class="col-1 text-center">${number}</div>
<div class="col-8">${task.Description}</div>
<div class="col-2 form-check form-switch d-flex justify-content-center">
<input class="text-center form-check-input" type="checkbox" ${task.Done && "checked"} role="switch" id="flexSwitchCheckDefault">
<input onchange="updateTask(${task.ID},this.checked)" class="text-center form-check-input" type="checkbox" ${task.Done && "checked"} role="switch" id="flexSwitchCheckDefault">
</div>
<button onclick="deleteTask(${task.ID})" class="col-1 btn btn-danger"><i class="bi bi-trash"></i></button>
`;

View file

@ -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"})
}
})