|
1
|
|
|
package controllers |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"memnixrest/app/database" |
|
5
|
|
|
"memnixrest/app/models" |
|
6
|
|
|
"net/http" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/gofiber/fiber/v2" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// GET |
|
12
|
|
|
|
|
13
|
|
|
// GetAllMem |
|
14
|
|
|
func GetAllMem(c *fiber.Ctx) error { |
|
15
|
|
|
db := database.DBConn |
|
16
|
|
|
|
|
17
|
|
|
var mems []models.Mem |
|
18
|
|
|
|
|
19
|
|
|
if res := db.Find(&mems); res.Error != nil { |
|
20
|
|
|
|
|
21
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
|
22
|
|
|
Success: false, |
|
23
|
|
|
Message: "Get All mems", |
|
24
|
|
|
Data: nil, |
|
25
|
|
|
Count: 0, |
|
26
|
|
|
}) |
|
27
|
|
|
} |
|
28
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
|
29
|
|
|
Success: true, |
|
30
|
|
|
Message: "Get All mems", |
|
31
|
|
|
Data: mems, |
|
32
|
|
|
Count: len(mems), |
|
33
|
|
|
}) |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// GetMemByID |
|
38
|
|
|
func GetMemByID(c *fiber.Ctx) error { |
|
39
|
|
|
id := c.Params("id") |
|
40
|
|
|
db := database.DBConn |
|
41
|
|
|
|
|
42
|
|
|
mem := new(models.Mem) |
|
43
|
|
|
|
|
44
|
|
|
if err := db.Joins("User").Joins("Card").First(&mem, id).Error; err != nil { |
|
45
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
46
|
|
|
Success: false, |
|
47
|
|
|
Message: err.Error(), |
|
48
|
|
|
Data: nil, |
|
49
|
|
|
Count: 0, |
|
50
|
|
|
}) |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
|
54
|
|
|
Success: true, |
|
55
|
|
|
Message: "Success get mem by ID.", |
|
56
|
|
|
Data: *mem, |
|
57
|
|
|
Count: 1, |
|
58
|
|
|
}) |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// GetMemByCardAndUser |
|
62
|
|
|
func GetMemByCardAndUser(c *fiber.Ctx) error { |
|
63
|
|
|
userID := c.Params("userID") |
|
64
|
|
|
cardID := c.Params("cardID") |
|
65
|
|
|
|
|
66
|
|
|
db := database.DBConn |
|
67
|
|
|
|
|
68
|
|
|
mem := new(models.Mem) |
|
69
|
|
|
|
|
70
|
|
|
if err := db.Joins("User").Joins("Card").Where("mems.user_id = ? AND mems.card_id = ?", userID, cardID).First(&mem).Error; err != nil { |
|
71
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
72
|
|
|
Success: false, |
|
73
|
|
|
Message: err.Error(), |
|
74
|
|
|
Data: nil, |
|
75
|
|
|
Count: 0, |
|
76
|
|
|
}) |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
|
80
|
|
|
Success: true, |
|
81
|
|
|
Message: "Success get mem by UserID & CardID.", |
|
82
|
|
|
Data: *mem, |
|
83
|
|
|
Count: 1, |
|
84
|
|
|
}) |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// POST |
|
88
|
|
|
|
|
89
|
|
|
// CreateNewMem |
|
90
|
|
|
func CreateNewMem(c *fiber.Ctx) error { |
|
91
|
|
|
db := database.DBConn // DB Conn |
|
92
|
|
|
|
|
93
|
|
|
mem := new(models.Mem) |
|
94
|
|
|
|
|
95
|
|
|
if err := c.BodyParser(&mem); err != nil { |
|
96
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
|
97
|
|
|
Success: false, |
|
98
|
|
|
Message: err.Error(), |
|
99
|
|
|
Data: nil, |
|
100
|
|
|
Count: 0, |
|
101
|
|
|
}) |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
db.Preload("User").Preload("Card").Create(mem) |
|
105
|
|
|
|
|
106
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
|
107
|
|
|
Success: true, |
|
108
|
|
|
Message: "Success register a new mem", |
|
109
|
|
|
Data: *mem, |
|
110
|
|
|
Count: 1, |
|
111
|
|
|
}) |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// PUT |
|
115
|
|
|
|
|
116
|
|
|
// UpdateMemByID |
|
117
|
|
|
func UpdateMemByID(c *fiber.Ctx) error { |
|
118
|
|
|
db := database.DBConn // DB Conn |
|
119
|
|
|
|
|
120
|
|
|
// Params |
|
121
|
|
|
id := c.Params("id") |
|
122
|
|
|
|
|
123
|
|
|
mem := new(models.Mem) |
|
124
|
|
|
|
|
125
|
|
|
if err := db.First(&mem, id).Error; err != nil { |
|
126
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
127
|
|
|
Success: false, |
|
128
|
|
|
Message: err.Error(), |
|
129
|
|
|
Data: nil, |
|
130
|
|
|
Count: 0, |
|
131
|
|
|
}) |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if err := UpdateMem(c, mem); err != nil { |
|
135
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
|
136
|
|
|
Success: false, |
|
137
|
|
|
Message: "Couldn't update the mem", |
|
138
|
|
|
Data: nil, |
|
139
|
|
|
Count: 0, |
|
140
|
|
|
}) |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
|
144
|
|
|
Success: true, |
|
145
|
|
|
Message: "Success update mem by Id.", |
|
146
|
|
|
Data: *mem, |
|
147
|
|
|
Count: 1, |
|
148
|
|
|
}) |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// UpdateMem |
|
152
|
|
|
func UpdateMem(c *fiber.Ctx, m *models.Mem) error { |
|
153
|
|
|
db := database.DBConn // DB Conn |
|
154
|
|
|
|
|
155
|
|
|
if err := c.BodyParser(&m); err != nil { |
|
156
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
|
157
|
|
|
Success: false, |
|
158
|
|
|
Message: err.Error(), |
|
159
|
|
|
Data: nil, |
|
160
|
|
|
Count: 0, |
|
161
|
|
|
}) |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
db.Save(m) |
|
165
|
|
|
|
|
166
|
|
|
return nil |
|
167
|
|
|
} |
|
168
|
|
|
|