1
|
|
|
package handlers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"memnixrest/core" |
5
|
|
|
"memnixrest/database" |
6
|
|
|
"memnixrest/models" |
7
|
|
|
"net/http" |
8
|
|
|
|
9
|
|
|
"github.com/gofiber/fiber/v2" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// GET |
13
|
|
|
|
14
|
|
|
// GetAllRevisions |
15
|
|
|
func GetAllRevisions(c *fiber.Ctx) error { |
16
|
|
|
db := database.DBConn // DB Conn |
17
|
|
|
|
18
|
|
|
var revisions []models.Revision |
19
|
|
|
|
20
|
|
|
if res := db.Joins("User").Joins("Card").Find(&revisions); res.Error != nil { |
21
|
|
|
|
22
|
|
|
return c.JSON(models.ResponseHTTP{ |
23
|
|
|
Success: false, |
24
|
|
|
Message: "Failed to get all revisions", |
25
|
|
|
Data: nil, |
26
|
|
|
Count: 0, |
27
|
|
|
}) |
28
|
|
|
} |
29
|
|
|
return c.JSON(models.ResponseHTTP{ |
30
|
|
|
Success: true, |
31
|
|
|
Message: "Get all revisions", |
32
|
|
|
Data: revisions, |
33
|
|
|
Count: len(revisions), |
34
|
|
|
}) |
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// GetRevisionByID |
39
|
|
|
func GetRevisionByID(c *fiber.Ctx) error { |
40
|
|
|
db := database.DBConn // Db Conn |
41
|
|
|
|
42
|
|
|
// Params |
43
|
|
|
id := c.Params("id") |
44
|
|
|
|
45
|
|
|
revision := new(models.Revision) |
46
|
|
|
|
47
|
|
|
if err := db.Joins("User").Joins("Card").First(&revision, id).Error; err != nil { |
48
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
49
|
|
|
Success: false, |
50
|
|
|
Message: err.Error(), |
51
|
|
|
Data: nil, |
52
|
|
|
Count: 0, |
53
|
|
|
}) |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return c.JSON(models.ResponseHTTP{ |
57
|
|
|
Success: true, |
58
|
|
|
Message: "Success get revision by ID.", |
59
|
|
|
Data: *revision, |
60
|
|
|
Count: 1, |
61
|
|
|
}) |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// GetRevisionByUserID |
65
|
|
|
func GetRevisionByUserID(c *fiber.Ctx) error { |
66
|
|
|
db := database.DBConn // DB Conn |
67
|
|
|
|
68
|
|
|
// Params |
69
|
|
|
id := c.Params("userID") |
70
|
|
|
|
71
|
|
|
revision := new(models.Revision) |
72
|
|
|
|
73
|
|
|
if err := db.Joins("User").Joins("Card").Where("revisions.user_id = ?", id).First(&revision).Error; err != nil { |
74
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
75
|
|
|
Success: false, |
76
|
|
|
Message: err.Error(), |
77
|
|
|
Data: nil, |
78
|
|
|
Count: 0, |
79
|
|
|
}) |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return c.JSON(models.ResponseHTTP{ |
83
|
|
|
Success: true, |
84
|
|
|
Message: "Success get revision by UserID.", |
85
|
|
|
Data: *revision, |
86
|
|
|
Count: 1, |
87
|
|
|
}) |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// POST |
91
|
|
|
|
92
|
|
|
// CreateNewRevision |
93
|
|
|
func CreateNewRevision(c *fiber.Ctx) error { |
94
|
|
|
db := database.DBConn // Db Conn |
95
|
|
|
|
96
|
|
|
revision := new(models.Revision) |
97
|
|
|
|
98
|
|
|
if err := c.BodyParser(&revision); err != nil { |
99
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
100
|
|
|
Success: false, |
101
|
|
|
Message: err.Error(), |
102
|
|
|
Data: nil, |
103
|
|
|
Count: 0, |
104
|
|
|
}) |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
db.Preload("User").Preload("Card").Create(revision) |
108
|
|
|
|
109
|
|
|
mem := core.GetMemByCardAndUser(c, revision.UserID, revision.CardID) |
110
|
|
|
core.UpdateMem(c, revision, &mem) |
111
|
|
|
|
112
|
|
|
return c.JSON(models.ResponseHTTP{ |
113
|
|
|
Success: true, |
114
|
|
|
Message: "Success register a revision", |
115
|
|
|
Data: *revision, |
116
|
|
|
Count: 1, |
117
|
|
|
}) |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|