-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_vault.go
82 lines (71 loc) · 1.6 KB
/
update_vault.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
package vaults
import (
"fmt"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/liobrdev/simplepasswords_vaults/models"
"github.com/liobrdev/simplepasswords_vaults/utils"
)
type UpdateVaultRequestBody struct {
Title string `json:"vault_title"`
}
func (h handler) UpdateVault(c *fiber.Ctx) error {
body := UpdateVaultRequestBody{}
if err := c.BodyParser(&body); err != nil {
return utils.RespondWithError(
c,
fiber.StatusBadRequest,
utils.UpdateVault,
string(utils.ErrorParse),
err.Error(),
)
}
if body.Title == "" {
return utils.RespondWithError(
c,
fiber.StatusBadRequest,
utils.UpdateVault,
string(utils.ErrorVaultTitle),
"",
)
}
if len(body.Title) > 255 {
return utils.RespondWithError(
c,
fiber.StatusBadRequest,
utils.UpdateVault,
string(utils.ErrorVaultTitle),
fmt.Sprintf("Too long (%d > 255)", len(body.Title)),
)
}
slug := c.Params("slug")
if result := h.DB.Model(&models.Vault{}).Where("slug = ?", slug).Update(
"title",
body.Title,
); result.Error != nil {
return utils.RespondWithError(
c,
fiber.StatusConflict,
utils.UpdateVault,
string(utils.ErrorFailedDB),
result.Error.Error(),
)
} else if n := result.RowsAffected; n == 0 {
return utils.RespondWithError(
c,
fiber.StatusNotFound,
utils.UpdateVault,
string(utils.ErrorNoRowsAffected),
"Likely that slug was not found.",
)
} else if n > 1 {
return utils.RespondWithError(
c,
fiber.StatusConflict,
utils.UpdateVault,
"result.RowsAffected > 1",
strconv.FormatInt(n, 10),
)
}
return c.SendStatus(fiber.StatusNoContent)
}