mirror of
https://github.com/knadh/listmonk.git
synced 2024-11-10 09:02:36 +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.
87 lines
3 KiB
Go
87 lines
3 KiB
Go
package core
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/knadh/listmonk/models"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// GetTemplates retrieves all templates.
|
|
func (c *Core) GetTemplates(noBody bool) ([]models.Template, error) {
|
|
out := []models.Template{}
|
|
if err := c.q.GetTemplates.Select(&out, 0, noBody); err != nil {
|
|
return nil, echo.NewHTTPError(http.StatusInternalServerError,
|
|
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.templates}", "error", pqErrMsg(err)))
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
// GetTemplate retrieves a given template.
|
|
func (c *Core) GetTemplate(id int, noBody bool) (models.Template, error) {
|
|
var out []models.Template
|
|
if err := c.q.GetTemplates.Select(&out, id, noBody); err != nil {
|
|
return models.Template{}, echo.NewHTTPError(http.StatusInternalServerError,
|
|
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.templates}", "error", pqErrMsg(err)))
|
|
}
|
|
|
|
if len(out) == 0 {
|
|
return models.Template{}, echo.NewHTTPError(http.StatusBadRequest,
|
|
c.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.template}"))
|
|
}
|
|
|
|
return out[0], nil
|
|
}
|
|
|
|
// CreateTemplate creates a new template.
|
|
func (c *Core) CreateTemplate(name string, body []byte) (models.Template, error) {
|
|
var newID int
|
|
if err := c.q.CreateTemplate.Get(&newID, name, body); err != nil {
|
|
return models.Template{}, echo.NewHTTPError(http.StatusInternalServerError,
|
|
c.i18n.Ts("globals.messages.errorCreating", "name", "{globals.terms.template}", "error", pqErrMsg(err)))
|
|
}
|
|
|
|
return c.GetTemplate(newID, false)
|
|
}
|
|
|
|
// UpdateTemplate updates a given template.
|
|
func (c *Core) UpdateTemplate(id int, name string, body []byte) (models.Template, error) {
|
|
res, err := c.q.UpdateTemplate.Exec(id, name, body)
|
|
if err != nil {
|
|
return models.Template{}, echo.NewHTTPError(http.StatusInternalServerError,
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.template}", "error", pqErrMsg(err)))
|
|
}
|
|
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
|
return models.Template{}, echo.NewHTTPError(http.StatusBadRequest,
|
|
c.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.template}"))
|
|
}
|
|
|
|
return c.GetTemplate(id, false)
|
|
}
|
|
|
|
// SetDefaultTemplate sets a template as default.
|
|
func (c *Core) SetDefaultTemplate(id int) error {
|
|
if _, err := c.q.SetDefaultTemplate.Exec(id); err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.template}", "error", pqErrMsg(err)))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeleteTemplate deletes a given template.
|
|
func (c *Core) DeleteTemplate(id int) error {
|
|
var delID int
|
|
if err := c.q.DeleteTemplate.Get(&delID, id); err != nil {
|
|
// TODO: Fix this. Deletes but always throws a "no result set" error.
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.template}", "error", pqErrMsg(err)))
|
|
}
|
|
if delID == 0 {
|
|
return echo.NewHTTPError(http.StatusBadRequest, c.i18n.T("templates.cantDeleteDefault"))
|
|
}
|
|
|
|
return nil
|
|
}
|