Skip to content
Snippets Groups Projects
Commit e1283220 authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

API Extension: JSON tags and DELETE

parent 72cdba60
No related branches found
No related tags found
No related merge requests found
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:]...)
}
......@@ -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))
}
......@@ -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")
}
......@@ -2,5 +2,5 @@ package model
type Todo struct {
ID int `json:"id"`
Description string `json:"description"`
Description string `json:"description" binding:"required"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment