|
1
|
|
|
package handlers |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"memnixrest/core" |
|
5
|
|
|
"memnixrest/database" |
|
6
|
|
|
"memnixrest/models" |
|
7
|
|
|
"net/http" |
|
8
|
|
|
"strconv" |
|
9
|
|
|
|
|
10
|
|
|
"github.com/gofiber/fiber/v2" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
// GET |
|
14
|
|
|
|
|
15
|
|
|
// GetAllMem |
|
16
|
|
|
func GetAllMem(c *fiber.Ctx) error { |
|
17
|
|
|
db := database.DBConn |
|
18
|
|
|
|
|
19
|
|
|
var mems []models.Mem |
|
20
|
|
|
|
|
21
|
|
|
if res := db.Find(&mems); res.Error != nil { |
|
22
|
|
|
|
|
23
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
24
|
|
|
Success: false, |
|
25
|
|
|
Message: "Get All mems", |
|
26
|
|
|
Data: nil, |
|
27
|
|
|
Count: 0, |
|
28
|
|
|
}) |
|
29
|
|
|
} |
|
30
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
31
|
|
|
Success: true, |
|
32
|
|
|
Message: "Get All mems", |
|
33
|
|
|
Data: mems, |
|
34
|
|
|
Count: len(mems), |
|
35
|
|
|
}) |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// GetMemByID |
|
40
|
|
|
func GetMemByID(c *fiber.Ctx) error { |
|
41
|
|
|
id := c.Params("id") |
|
42
|
|
|
db := database.DBConn |
|
43
|
|
|
|
|
44
|
|
|
mem := new(models.Mem) |
|
45
|
|
|
|
|
46
|
|
|
if err := db.Joins("User").Joins("Card").First(&mem, id).Error; err != nil { |
|
47
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
48
|
|
|
Success: false, |
|
49
|
|
|
Message: err.Error(), |
|
50
|
|
|
Data: nil, |
|
51
|
|
|
Count: 0, |
|
52
|
|
|
}) |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
56
|
|
|
Success: true, |
|
57
|
|
|
Message: "Success get mem by ID.", |
|
58
|
|
|
Data: *mem, |
|
59
|
|
|
Count: 1, |
|
60
|
|
|
}) |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// GetMemByCardAndUser |
|
64
|
|
|
func GetMemByCardAndUser(c *fiber.Ctx) error { |
|
65
|
|
|
userID := c.Params("userID") |
|
66
|
|
|
cardID := c.Params("cardID") |
|
67
|
|
|
|
|
68
|
|
|
db := database.DBConn |
|
69
|
|
|
|
|
70
|
|
|
mem := new(models.Mem) |
|
71
|
|
|
|
|
72
|
|
|
if err := db.Joins("User").Joins("Card").Where("mems.user_id = ? AND mems.card_id = ?", userID, cardID).First(&mem).Error; err != nil { |
|
73
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
74
|
|
|
Success: false, |
|
75
|
|
|
Message: err.Error(), |
|
76
|
|
|
Data: nil, |
|
77
|
|
|
Count: 0, |
|
78
|
|
|
}) |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
82
|
|
|
Success: true, |
|
83
|
|
|
Message: "Success get mem by UserID & CardID.", |
|
84
|
|
|
Data: *mem, |
|
85
|
|
|
Count: 1, |
|
86
|
|
|
}) |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// GetNextMem |
|
90
|
|
|
func GetNextMem(c *fiber.Ctx) error { |
|
91
|
|
|
userIDTemp := c.Params("userID") |
|
92
|
|
|
deckIDTemp := c.Params("deckID") |
|
93
|
|
|
|
|
94
|
|
|
userID, _ := strconv.Atoi(userIDTemp) |
|
95
|
|
|
deckID, _ := strconv.Atoi(deckIDTemp) |
|
96
|
|
|
|
|
97
|
|
|
res := core.FetchNextCard(c, uint(userID), uint(deckID)) |
|
98
|
|
|
|
|
99
|
|
|
if !res.Success { |
|
100
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
101
|
|
|
Success: false, |
|
102
|
|
|
Message: "Next card not found", |
|
103
|
|
|
Data: nil, |
|
104
|
|
|
Count: 0, |
|
105
|
|
|
}) |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
109
|
|
|
Success: true, |
|
110
|
|
|
Message: "Success get card by ID.", |
|
111
|
|
|
Data: res.Data, |
|
112
|
|
|
Count: 1, |
|
113
|
|
|
}) |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// GetTodayNextMem |
|
118
|
|
|
func GetTodayNextMem(c *fiber.Ctx) error { |
|
119
|
|
|
userIDTemp := c.Params("userID") |
|
120
|
|
|
deckIDTemp := c.Params("deckID") |
|
121
|
|
|
|
|
122
|
|
|
userID, _ := strconv.Atoi(userIDTemp) |
|
123
|
|
|
deckID, _ := strconv.Atoi(deckIDTemp) |
|
124
|
|
|
|
|
125
|
|
|
res := core.FetchNextTodayCard(c, uint(userID), uint(deckID)) |
|
126
|
|
|
if !res.Success { |
|
127
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
128
|
|
|
Success: false, |
|
129
|
|
|
Message: "No more card for today!", |
|
130
|
|
|
Data: nil, |
|
131
|
|
|
Count: 0, |
|
132
|
|
|
}) |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
136
|
|
|
Success: true, |
|
137
|
|
|
Message: "Success get card Today's card.", |
|
138
|
|
|
Data: res.Data, |
|
139
|
|
|
Count: 1, |
|
140
|
|
|
}) |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// POST |
|
144
|
|
|
|
|
145
|
|
|
// CreateNewMem |
|
146
|
|
|
func CreateNewMem(c *fiber.Ctx) error { |
|
147
|
|
|
db := database.DBConn // DB Conn |
|
148
|
|
|
|
|
149
|
|
|
mem := new(models.Mem) |
|
150
|
|
|
|
|
151
|
|
|
if err := c.BodyParser(&mem); err != nil { |
|
152
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
|
153
|
|
|
Success: false, |
|
154
|
|
|
Message: err.Error(), |
|
155
|
|
|
Data: nil, |
|
156
|
|
|
Count: 0, |
|
157
|
|
|
}) |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
db.Preload("User").Preload("Card").Create(mem) |
|
161
|
|
|
|
|
162
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
163
|
|
|
Success: true, |
|
164
|
|
|
Message: "Success register a new mem", |
|
165
|
|
|
Data: *mem, |
|
166
|
|
|
Count: 1, |
|
167
|
|
|
}) |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// PUT |
|
171
|
|
|
|
|
172
|
|
|
// UpdateMemByID |
|
173
|
|
|
func UpdateMemByID(c *fiber.Ctx) error { |
|
174
|
|
|
db := database.DBConn // DB Conn |
|
175
|
|
|
|
|
176
|
|
|
// Params |
|
177
|
|
|
id := c.Params("id") |
|
178
|
|
|
|
|
179
|
|
|
mem := new(models.Mem) |
|
180
|
|
|
|
|
181
|
|
|
if err := db.First(&mem, id).Error; err != nil { |
|
182
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
183
|
|
|
Success: false, |
|
184
|
|
|
Message: err.Error(), |
|
185
|
|
|
Data: nil, |
|
186
|
|
|
Count: 0, |
|
187
|
|
|
}) |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if err := UpdateMem(c, mem); err != nil { |
|
191
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
192
|
|
|
Success: false, |
|
193
|
|
|
Message: "Couldn't update the mem", |
|
194
|
|
|
Data: nil, |
|
195
|
|
|
Count: 0, |
|
196
|
|
|
}) |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return c.JSON(models.ResponseHTTP{ |
|
200
|
|
|
Success: true, |
|
201
|
|
|
Message: "Success update mem by Id.", |
|
202
|
|
|
Data: *mem, |
|
203
|
|
|
Count: 1, |
|
204
|
|
|
}) |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// UpdateMem |
|
208
|
|
|
func UpdateMem(c *fiber.Ctx, m *models.Mem) error { |
|
209
|
|
|
db := database.DBConn // DB Conn |
|
210
|
|
|
|
|
211
|
|
|
if err := c.BodyParser(&m); err != nil { |
|
212
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
|
213
|
|
|
Success: false, |
|
214
|
|
|
Message: err.Error(), |
|
215
|
|
|
Data: nil, |
|
216
|
|
|
Count: 0, |
|
217
|
|
|
}) |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
db.Save(m) |
|
221
|
|
|
|
|
222
|
|
|
return nil |
|
223
|
|
|
} |
|
224
|
|
|
|