-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer.go
82 lines (70 loc) · 1.67 KB
/
pointer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// Todo represents a todo item
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Status string `json:"status"`
}
var todos []Todo
func main() {
router := gin.Default()
// Get all todos
router.GET("/todos", func(c *gin.Context) {
c.JSON(http.StatusOK, todos)
})
// Create a new todo
router.POST("/todos", func(c *gin.Context) {
var todo Todo
if err := c.ShouldBindJSON(&todo); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
todos = append(todos, todo)
c.JSON(http.StatusCreated, todo)
})
// Get a specific todo
router.GET("/todos/:id", func(c *gin.Context) {
id,_:=strconv.Atoi(c.Param("id"))
for _, todo := range todos {
if todo.ID == id {
c.JSON(http.StatusOK, todo)
return
}
}
c.JSON(http.StatusNotFound, gin.H{"error": "Todo not found"})
})
// Update a specific todo
router.PUT("/todos/:id", func(c *gin.Context) {
id,_:=strconv.Atoi(c.Param("id"))
var todo Todo
if err := c.ShouldBindJSON(&todo); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
for i, t := range todos {
if t.ID == id {
todos[i] = todo
c.JSON(http.StatusOK, todo)
return
}
}
c.JSON(http.StatusNotFound, gin.H{"error": "Todo not found"})
})
// Delete a specific todo
router.DELETE("/todos/:id", func(c *gin.Context) {
id,_:=strconv.Atoi(c.Param("id"))
for i, todo := range todos {
if todo.ID == id {
todos = append(todos[:i], todos[i+1:]...)
c.JSON(http.StatusOK, gin.H{"message": "Todo deleted"})
return
}
}
})
router.Run("localhost:8080")
}