-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
165 lines (132 loc) · 3.21 KB
/
main.go
File metadata and controls
165 lines (132 loc) · 3.21 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)
type Todo struct {
Id int `json:"id"`
Name string `json:"name"`
Completed bool `json:"completed"`
}
var todos = []*Todo{
{Id: 1, Name: "Cleber", Completed: false},
{Id: 2, Name: "Claudio", Completed: false},
}
func main() {
app := fiber.New()
// Default middleware config
app.Use(logger.New())
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
SetupTodosRoutes(app)
err := app.Listen(":3000")
if err != nil {
panic(err)
}
}
func SetupApiV1(app *fiber.App) {
v1 := app.Group("/v1")
SetupTodosRoutes(v1)
}
func SetupTodosRoutes(grp fiber.Router) {
todosRoutes := grp.Group("/todos")
todosRoutes.Get("/", GetTodos)
todosRoutes.Post("/", CreateTodo)
todosRoutes.Get("/:id", GetTodo)
todosRoutes.Delete("/:id", DeleteTodo)
todosRoutes.Patch("/:id", UpdateTodo)
}
func GetTodos(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(todos)
}
func CreateTodo(c *fiber.Ctx) error {
type request struct {
Name string `json:"name"`
}
var body request
err := c.BodyParser(&body)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "cannot parse json",
})
return c.Status(fiber.StatusOK).JSON("OK")
}
todo := &Todo{
Id: len(todos) + 1,
Name: body.Name,
Completed: false,
}
todos = append(todos, todo)
return c.Status(fiber.StatusCreated).JSON(todos)
}
func GetTodo(c *fiber.Ctx) error {
paramsId := c.Params("id")
id, err := strconv.Atoi(paramsId)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "cannot parse id",
})
}
for _, todo := range todos {
if todo.Id == id {
return c.Status(fiber.StatusOK).JSON(todo)
}
}
return c.Status(fiber.StatusNotFound).JSON("Not found.")
}
func DeleteTodo(c *fiber.Ctx) error {
paramsId := c.Params("id")
id, err := strconv.Atoi(paramsId)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "cannot parse id",
})
}
for i, todo := range todos {
if todo.Id == id {
todos = append(todos[0:i], todos[i+1:]...)
return c.Status(fiber.StatusOK).JSON("Todo Deleted.")
}
}
return c.Status(fiber.StatusNotFound).JSON("Not found.")
}
func UpdateTodo(c *fiber.Ctx) error {
type request struct {
Name *string `json:"name"` // o * serve para deixar o campo automaticamente obrigatório
Completed *bool `json:"completed"`
}
paramsId := c.Params("id")
id, err := strconv.Atoi(paramsId)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "cannot parse id",
})
}
var body request
err = c.BodyParser(&body)
if err != nil {
c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"error": "Cannot parse body",
})
}
var todo *Todo
for _, t := range todos {
if t.Id == id {
todo = t
break
}
}
if todo == nil {
return c.Status(fiber.StatusNotFound).JSON("Todo not found...")
}
if body.Name != nil {
todo.Name = *body.Name // o * é utilizado para receber o valor do ponteiro
}
if body.Completed != nil {
todo.Completed = *body.Completed
}
// agora é certeza que o Todo foi atualizado
return c.Status(fiber.StatusOK).JSON(todo)
}