forked from aleksandr-slobodian/go-simple-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (122 loc) · 2.93 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
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
package main
import (
"errors"
"net/http"
"strconv"
"sync"
"github.com/gin-gonic/gin"
)
type todo struct {
ID int `json:"id"`
Item string `json:"item"`
Completed bool `json:"completed"`
}
var (
todos = []todo{}
nextID int = 1
idMutex sync.Mutex
)
func generateID() int {
idMutex.Lock()
defer idMutex.Unlock()
id := nextID
nextID++
return id
}
func getTodoById(s string) (*todo, error) {
id, err := strconv.Atoi(s)
if err != nil {
return nil, errors.New("invalid todo id")
}
for i, t := range todos {
if t.ID == id {
return &todos[i], nil
}
}
return nil, errors.New("todo not found")
}
func getTodo(context *gin.Context) {
id := context.Param("id")
todo, err := getTodoById(id)
if err != nil {
context.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
context.IndentedJSON(http.StatusOK, todo)
}
func getTodos(context *gin.Context) {
context.IndentedJSON(http.StatusOK, todos)
}
func createTodo(context *gin.Context) {
var todo todo
if err := context.ShouldBindJSON(&todo); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "invalid data"})
return
}
if todo.Item == "" {
context.JSON(http.StatusBadRequest, gin.H{"error": "Item is required"})
return
}
todo.ID = generateID()
todos = append(todos, todo)
context.IndentedJSON(http.StatusCreated, todo)
}
func toggleTodoStatus(context *gin.Context) {
id := context.Param("id")
todo, err := getTodoById(id)
if err != nil {
context.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
todo.Completed = !todo.Completed
context.IndentedJSON(http.StatusOK, todo)
}
func updateTodo(context *gin.Context) {
id := context.Param("id")
todoLocal, err := getTodoById(id)
if err != nil {
context.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
var todoData todo
if err := context.ShouldBindJSON(&todoData); err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "invalid data"})
return
}
todoLocal.Item = todoData.Item
todoLocal.Completed = todoData.Completed
context.IndentedJSON(http.StatusOK, todoLocal)
}
func deleteTodoById(s string) (*todo, error) {
id, err := strconv.Atoi(s)
if err != nil {
return nil, errors.New("invalid todo id")
}
for i, t := range todos {
if t.ID == id {
deletedTodo := t
todos = append(todos[:i], todos[i+1:]...)
return &deletedTodo, nil
}
}
return nil, errors.New("todo not found")
}
func deleteTodo(context *gin.Context) {
id := context.Param("id")
todo, err := deleteTodoById(id)
if err != nil {
context.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
}
context.IndentedJSON(http.StatusOK, todo)
}
func main() {
router := gin.Default()
router.GET("/todos", getTodos)
router.POST("/todos", createTodo)
router.GET("/todos/:id", getTodo)
router.PATCH("/todos/:id", toggleTodoStatus)
router.PUT("/todos/:id", updateTodo)
router.DELETE("/todos/:id", deleteTodo)
router.Run("localhost:9191")
}