/
logs.go
238 lines (184 loc) · 6.62 KB
/
logs.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package handlers
import (
"errors"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/italia/developers-italia-api/internal/common"
"github.com/italia/developers-italia-api/internal/handlers/general"
"github.com/italia/developers-italia-api/internal/models"
"github.com/pilagod/gorm-cursor-paginator/v2/paginator"
"gorm.io/gorm"
)
type LogInterface interface {
GetLogs(ctx *fiber.Ctx) error
GetLog(ctx *fiber.Ctx) error
PostLog(ctx *fiber.Ctx) error
PatchLog(ctx *fiber.Ctx) error
DeleteLog(ctx *fiber.Ctx) error
GetSoftwareLogs(ctx *fiber.Ctx) error
PostSoftwareLog(ctx *fiber.Ctx) error
}
type Log struct {
db *gorm.DB
}
func NewLog(db *gorm.DB) *Log {
return &Log{db: db}
}
// GetLogs gets the list of all logs and returns any error encountered.
func (p *Log) GetLogs(ctx *fiber.Ctx) error {
var logs []models.Log
stmt, err := general.Clauses(ctx, p.db, "message")
if err != nil {
return common.Error(
fiber.StatusUnprocessableEntity,
"can't get Logs",
err.Error(),
)
}
// Logs are returned in descending order, last first
paginator := general.NewPaginatorWithConfig(ctx, &paginator.Config{Order: paginator.DESC})
result, cursor, err := paginator.Paginate(stmt, &logs)
if err != nil {
return common.Error(
fiber.StatusUnprocessableEntity,
"can't get Logs",
"wrong cursor format in page[after] or page[before]",
)
}
if result.Error != nil {
return common.Error(
fiber.StatusInternalServerError,
"can't get Logs",
fiber.ErrInternalServerError.Message,
)
}
return ctx.JSON(fiber.Map{"data": &logs, "links": general.PaginationLinks(cursor)})
}
// GetLog gets the log with the given ID and returns any error encountered.
func (p *Log) GetLog(ctx *fiber.Ctx) error {
log := models.Log{}
if err := p.db.First(&log, "id = ?", ctx.Params("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return common.Error(fiber.StatusNotFound, "can't get Log", "Log was not found")
}
return common.Error(fiber.StatusInternalServerError, "can't get Log", "internal server error")
}
return ctx.JSON(&log)
}
// PostLog creates a new log.
func (p *Log) PostLog(ctx *fiber.Ctx) error {
logReq := new(common.Log)
if err := ctx.BodyParser(&logReq); err != nil {
return common.Error(fiber.StatusBadRequest, "can't create Log", "invalid json")
}
if err := common.ValidateStruct(*logReq); err != nil {
return common.ErrorWithValidationErrors(fiber.StatusUnprocessableEntity, "can't create Log", "invalid format", err)
}
log := models.Log{ID: utils.UUIDv4(), Message: logReq.Message}
if err := p.db.Create(&log).Error; err != nil {
return common.Error(fiber.StatusInternalServerError, "can't create Log", "db error")
}
return ctx.JSON(&log)
}
// PatchLog updates the log with the given ID.
func (p *Log) PatchLog(ctx *fiber.Ctx) error {
logReq := new(common.Log)
if err := ctx.BodyParser(logReq); err != nil {
return common.Error(fiber.StatusBadRequest, "can't update Log", "invalid json")
}
if err := common.ValidateStruct(*logReq); err != nil {
return common.ErrorWithValidationErrors(fiber.StatusUnprocessableEntity, "can't update Log", "invalid format", err)
}
log := models.Log{}
if err := p.db.First(&log, "id = ?", ctx.Params("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return common.Error(fiber.StatusNotFound, "can't update Log", "Log was not found")
}
return common.Error(fiber.StatusInternalServerError, "can't update Log", "internal server error")
}
log.Message = logReq.Message
if err := p.db.Updates(&log).Error; err != nil {
return common.Error(fiber.StatusInternalServerError, "can't update Log", "db error")
}
return ctx.JSON(&log)
}
// DeleteLog deletes the log with the given ID.
func (p *Log) DeleteLog(ctx *fiber.Ctx) error {
var log models.Log
result := p.db.Delete(&log, "id = ?", ctx.Params("id"))
if result.Error != nil {
return common.Error(fiber.StatusInternalServerError, "can't delete Log", "db error")
}
if result.RowsAffected == 0 {
return common.Error(fiber.StatusNotFound, "can't delete Log", "Log was not found")
}
return ctx.SendStatus(fiber.StatusNoContent)
}
// GetSoftwareLogs gets the logs associated to a Software with the given ID and returns any error encountered.
func (p *Log) GetSoftwareLogs(ctx *fiber.Ctx) error {
var logs []models.Log
software := models.Software{}
if err := p.db.First(&software, "id = ?", ctx.Params("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return common.Error(fiber.StatusNotFound, "can't get Software", "Software was not found")
}
return common.Error(
fiber.StatusInternalServerError,
"can't get Software",
fiber.ErrInternalServerError.Message,
)
}
stmt := p.db.
Where(map[string]interface{}{"entity_type": models.Software{}.TableName()}).
Where("entity_id = ?", software.ID)
// Logs are returned in descending order, last first
paginator := general.NewPaginatorWithConfig(ctx, &paginator.Config{Order: paginator.DESC})
result, cursor, err := paginator.Paginate(stmt, &logs)
if err != nil {
return common.Error(
fiber.StatusUnprocessableEntity,
"can't get Software",
"wrong cursor format in page[after] or page[before]",
)
}
if result.Error != nil {
return common.Error(
fiber.StatusInternalServerError,
"can't get Software",
fiber.ErrInternalServerError.Message,
)
}
return ctx.JSON(fiber.Map{"data": &logs, "links": general.PaginationLinks(cursor)})
}
// PostSoftwareLog creates a new log associated to a Software with the given ID and returns any error encountered.
func (p *Log) PostSoftwareLog(ctx *fiber.Ctx) error {
logReq := new(common.Log)
software := models.Software{}
if err := p.db.First(&software, "id = ?", ctx.Params("id")).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return common.Error(fiber.StatusNotFound, "can't create Log", "Software was not found")
}
return common.Error(
fiber.StatusInternalServerError,
"can't get Software",
fiber.ErrInternalServerError.Message,
)
}
if err := ctx.BodyParser(&logReq); err != nil {
return common.Error(fiber.StatusBadRequest, "can't create Log", "invalid json")
}
if err := common.ValidateStruct(*logReq); err != nil {
return common.ErrorWithValidationErrors(fiber.StatusUnprocessableEntity, "can't create Log", "invalid format", err)
}
table := models.Software{}.TableName()
log := models.Log{
ID: utils.UUIDv4(),
Message: logReq.Message,
EntityID: &software.ID,
EntityType: &table,
}
if err := p.db.Create(&log).Error; err != nil {
return common.Error(fiber.StatusInternalServerError, "can't create Log", "db error")
}
return ctx.JSON(&log)
}