/
post.go
45 lines (35 loc) · 916 Bytes
/
post.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
package gocruddy
import (
"github.com/ao-concepts/storage"
"github.com/gofiber/fiber/v2"
)
func Post(c Container, config CrudConfig) fiber.Handler {
log := c.GetLogger()
db := c.GetDatabase()
crudRepo := c.GetCrudRepo()
return func(ctx *fiber.Ctx) error {
if err := db.UseTransaction(func(tx *storage.Transaction) (err error) {
mapped, err := config.MapPostEntry(config.GetEntityEntry(), ctx, tx)
if err != nil {
if crudError, ok := err.(Error); ok {
log.ErrWarn(crudError)
if crudError.respond {
ctx.SendString(crudError.Error())
}
return ctx.SendStatus(crudError.responseCode)
}
return err
}
if err := crudRepo.Insert(tx, mapped); err != nil {
return err
}
return ctx.JSON(fiber.Map{
"id": mapped.GetID(),
})
}); err != nil {
log.ErrError(err)
return ctx.SendStatus(fiber.StatusInternalServerError)
}
return nil
}
}