-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (93 loc) · 2.36 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type item struct {
ID string `json:"id"`
Name string `json:"name"`
Quantity int `json:"quantity"`
}
var items = []item{
{ID: "1", Name: "Book", Quantity: 2},
{ID: "2", Name: "Movie", Quantity: 4},
{ID: "3", Name: "Table", Quantity: 5},
{ID: "4", Name: "Bottle", Quantity: 3},
}
func getItems(context *gin.Context) {
context.IndentedJSON(http.StatusOK, items)
}
func addItem(context *gin.Context) {
var newItem item
err := context.BindJSON(&newItem);
if err != nil {
return
}
items = append(items, newItem)
context.IndentedJSON(http.StatusCreated, newItem)
}
func getItemById(id string) (*item, error) {
for i, t := range items {
if t.ID == id {
return &items[i], nil
}
}
return nil, errors.New("item not found")
}
func getIndexById(id string) (int, error) {
for i, t := range items {
if t.ID == id {
return i, nil
}
}
return 0, errors.New("item not found")
}
func getItem(context *gin.Context) {
id := context.Param("id")
item, err := getItemById(id)
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Not found"})
return
}
context.IndentedJSON(http.StatusOK, item)
}
func updateItem(context *gin.Context) {
id := context.Param("id")
foundItem, err := getItemById(id)
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Item not found"})
return
}
var body item
context.Bind(&body)
fmt.Print("found", foundItem)
fmt.Print("body", body.Name)
if len(body.Name) != 0 {
foundItem.Name = body.Name
}
if body.Quantity != 0 {
foundItem.Quantity = body.Quantity
}
context.IndentedJSON(http.StatusOK, foundItem)
}
func deleteItem(context *gin.Context) {
id := context.Param("id")
index, err := getIndexById(id)
if err != nil {
context.IndentedJSON(http.StatusNotFound, gin.H{"message": "Not found"})
return
}
items = append(items[:index], items[index+1:]...)
context.IndentedJSON(http.StatusCreated, items)
}
func main() {
router := gin.Default()
router.GET("/items/:id", getItem)
router.GET("/items", getItems)
router.POST("/items", addItem)
router.PATCH("/items/:id", updateItem)
router.DELETE("/items/:id", deleteItem)
router.Run()
}