diff --git a/code/mytodo/data/db.go b/code/mytodo/data/db.go index 38dc2faef5d7990531bb94b4644225ae37a3716f..8637d87aadb4f92b848c59ac71382a5fa12901bc 100644 --- a/code/mytodo/data/db.go +++ b/code/mytodo/data/db.go @@ -1,6 +1,36 @@ package data -import "gitlab.reutlingen-university.de/schmolli/todo/model" +import ( + "fmt" + + "gitlab.reutlingen-university.de/schmolli/todo/model" +) var Todos = []model.Todo{} var NextId = 0 + +func DeleteToDoWithId(id int) (todo *model.Todo, err error) { + delIndex := -1 + for pos, t := range Todos { + if t.ID == id { + delIndex = pos + break + } + } + if delIndex == -1 { + return nil, fmt.Errorf("todo with ID %d not found", id) + } else { + todo := Todos[delIndex] + deleteIndex(delIndex) + return &todo, nil + } +} + +func DeleteAll() { + //Create empty slice + Todos = make([]model.Todo, 0) +} + +func deleteIndex(pos int) { + Todos = append(Todos[:pos], Todos[pos+1:]...) +} diff --git a/code/mytodo/handler/todo.go b/code/mytodo/handler/todo.go index cc106cae2abd8d170c1a66ed63d1bb996e1ab53c..293dd0477c89dc108c4e14388fbd279eb8c67a3f 100644 --- a/code/mytodo/handler/todo.go +++ b/code/mytodo/handler/todo.go @@ -13,12 +13,13 @@ import ( func AddTodo(c *gin.Context) { var todo model.Todo if err := c.BindJSON(&todo); err != nil { - c.IndentedJSON(http.StatusBadRequest, fmt.Sprintf("{\"error message\" : \"%s\"}", err.Error())) + c.AbortWithError(http.StatusBadRequest, err) + } else { + todo.ID = data.NextId + data.NextId++ + data.Todos = append(data.Todos, todo) + c.IndentedJSON(http.StatusCreated, todo) } - todo.ID = data.NextId - data.NextId++ - data.Todos = append(data.Todos, todo) - c.IndentedJSON(http.StatusCreated, todo) } func GetTodos(c *gin.Context) { @@ -28,7 +29,7 @@ func GetTodos(c *gin.Context) { func GetToDoByID(c *gin.Context) { id, err := strconv.Atoi(c.Param("id")) if err != nil { - c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "illegal id "}) + c.AbortWithError(http.StatusBadRequest, err) return } @@ -38,5 +39,26 @@ func GetToDoByID(c *gin.Context) { return } } - c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Todo not found"}) + c.AbortWithError(http.StatusNotFound, fmt.Errorf("todo with ID %d not found", id)) +} + +func DeleteToDoByID(c *gin.Context) { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.AbortWithError(http.StatusBadRequest, err) + return + } + + var todo *model.Todo = nil + if todo, err = data.DeleteToDoWithId(id); err != nil { + c.AbortWithError(http.StatusNotFound, err) + } + + c.IndentedJSON(http.StatusOK, *todo) +} + +func DeleteAllTodos(c *gin.Context) { + len := len(data.Todos) + data.DeleteAll() + c.IndentedJSON(http.StatusOK, fmt.Sprintf("Deleted %d todo items", len)) } diff --git a/code/mytodo/main.go b/code/mytodo/main.go index d2aa6ca7ade0fba0bf83c10448c273eda1f76297..3b580ad2469e2ede67d5ab6d064673800dd6a416 100644 --- a/code/mytodo/main.go +++ b/code/mytodo/main.go @@ -11,5 +11,7 @@ func main() { r.GET("/mytodo/todos", handler.GetTodos) r.GET("/mytodo/todos/:id", handler.GetToDoByID) r.POST("/mytodo/todos", handler.AddTodo) + r.DELETE("/mytodo/todos/:id", handler.DeleteToDoByID) + r.DELETE("/mytodo/todos/clear", handler.DeleteAllTodos) r.Run(":10000") } diff --git a/code/mytodo/model/todo.go b/code/mytodo/model/todo.go index ce92ff0200b0247aaf26371a861b9eff99c4cbc1..1de380951d05834f20280ff15c57e8fb4107d6bd 100644 --- a/code/mytodo/model/todo.go +++ b/code/mytodo/model/todo.go @@ -2,5 +2,5 @@ package model type Todo struct { ID int `json:"id"` - Description string `json:"description"` + Description string `json:"description" binding:"required"` }