mirror of
https://github.com/knadh/listmonk.git
synced 2024-11-15 12:05:50 +08:00
b5cd9498b1
This is a long pending refactor. All the DB, query, CRUD, and related logic scattered across HTTP handlers are now moved into a central `core` package with clean, abstracted methods, decoupling HTTP handlers from executing direct DB queries and other business logic. eg: `core.CreateList()`, `core.GetLists()` etc. - Remove obsolete subscriber methods. - Move optin hook queries to core. - Move campaign methods to `core`. - Move all campaign methods to `core`. - Move public page functions to `core`. - Move all template functions to `core`. - Move media and settings function to `core`. - Move handler middleware functions to `core`. - Move all bounce functions to `core`. - Move all dashboard functions to `core`. - Fix GetLists() not honouring type - Fix unwrapped JSON responses. - Clean up obsolete pre-core util function. - Replace SQL array null check with cardinality check. - Fix missing validations in `core` queries. - Remove superfluous deps on internal `subimporter`. - Add dashboard functions to `core`. - Fix broken domain ban check. - Fix broken subscriber check middleware. - Remove redundant error handling. - Remove obsolete functions. - Remove obsolete structs. - Remove obsolete queries and DB functions. - Document the `core` package.
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package core
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/jmoiron/sqlx/types"
|
|
"github.com/knadh/listmonk/models"
|
|
"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
|
|
}
|