-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
50 lines (41 loc) · 1.35 KB
/
settings.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
package core
import (
"encoding/json"
"net/http"
"github.com/ghostdevv/listmonk-tweaked/models"
"github.com/jmoiron/sqlx/types"
"github.com/labstack/echo/v4"
)
// GetSettings returns settings from the DB.
func (c *Core) GetSettings() (models.Settings, error) {
var (
b types.JSONText
out models.Settings
)
if err := c.q.GetSettings.Get(&b); err != nil {
return out, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching",
"name", "{globals.terms.settings}", "error", pqErrMsg(err)))
}
// Unmarshal the settings and filter out sensitive fields.
if err := json.Unmarshal([]byte(b), &out); err != nil {
return out, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("settings.errorEncoding", "error", err.Error()))
}
return out, nil
}
// UpdateSettings updates settings.
func (c *Core) UpdateSettings(s models.Settings) error {
// Marshal settings.
b, err := json.Marshal(s)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("settings.errorEncoding", "error", err.Error()))
}
// Update the settings in the DB.
if _, err := c.q.UpdateSettings.Exec(b); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.settings}", "error", pqErrMsg(err)))
}
return nil
}