-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (149 loc) · 3.83 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"time"
)
type Task struct {
ID uuid.UUID `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type TaskRequest struct {
Title string `json:"title"`
Description string `json:"description"`
}
type CacheItem struct {
Data interface{}
ExpiresAt time.Time
}
var (
tasks = make(map[uuid.UUID]Task)
taskCount = 0
)
func main() {
app := fiber.New()
group := app.Group("/api")
group.Post("/tasks", CreateTask)
group.Get("/tasks/:id", GetTaskByID)
group.Get("/tasks", ListTasks)
group.Delete("/tasks/:id", DeleteTaskByID)
group.Put("/tasks/:id", UpdateTaskByID)
err := app.Listen(":3000")
if err != nil {
panic(err)
}
}
func ListTasks(c *fiber.Ctx) error {
cacheKey := "tasks_cache_key"
if c.Get(cacheKey) != "" {
return c.JSON(c.Get(cacheKey))
}
tasksList := getTasksList()
cacheItem := &CacheItem{
Data: tasksList,
ExpiresAt: time.Now().Add(5 * time.Minute),
}
out, err := json.Marshal(cacheItem)
if err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when marshalling the cache item")
}
c.Set(cacheKey, string(out))
return c.JSON(fiber.Map{"data": tasksList, "count": taskCount})
}
func CreateTask(c *fiber.Ctx) error {
var payload TaskRequest
if err := c.BodyParser(&payload); err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when parsing the request body")
}
task := Task{
ID: uuid.New(),
Title: payload.Title,
Description: payload.Description,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
taskCount++
tasks[task.ID] = task
c.Set("tasks_cache_key", "")
return c.JSON(task)
}
func GetTaskByID(c *fiber.Ctx) error {
id := c.Params("id")
if c.Get(id) != "" {
return c.JSON(c.Get(id))
}
s, err := uuid.Parse(id)
if err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when parsing the uuid")
}
if task, ok := tasks[s]; ok {
cacheItem := &CacheItem{
Data: task,
ExpiresAt: time.Now().Add(5 * time.Minute),
}
out, err := json.Marshal(cacheItem)
if err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when marshalling the cache item")
}
c.Set(id, string(out))
return c.JSON(task)
}
return c.Status(fiber.StatusNotFound).SendString("Task not found")
}
func DeleteTaskByID(c *fiber.Ctx) error {
id := c.Params("id")
s, err := uuid.Parse(id)
if err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when parsing the uuid")
}
if _, ok := tasks[s]; ok {
delete(tasks, s)
taskCount--
c.Set(id, "")
return c.SendString("Task deleted successfully")
}
return c.Status(fiber.StatusNotFound).SendString("Task not found")
}
func UpdateTaskByID(c *fiber.Ctx) error {
id := c.Params("id")
s, err := uuid.Parse(id)
if err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when parsing the uuid")
}
var updatedTask Task
var payload TaskRequest
if err := c.BodyParser(&payload); err != nil {
return c.Status(fiber.StatusBadRequest).
SendString("An error occurred when parsing the request body")
}
if _, ok := tasks[s]; ok {
updatedTask = Task{
ID: tasks[s].ID,
Title: payload.Title,
Description: payload.Description,
CreatedAt: tasks[s].CreatedAt,
UpdatedAt: time.Now(),
}
tasks[s] = updatedTask
c.Set(id, "")
return c.JSON(updatedTask)
}
return c.Status(fiber.StatusNotFound).SendString("Task not found")
}
func getTasksList() []Task {
taskList := make([]Task, 0, len(tasks))
for _, task := range tasks {
taskList = append(taskList, task)
}
return taskList
}