2022-04-03 15:24:40 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2022-07-02 11:25:42 +00:00
|
|
|
"database/sql"
|
2022-04-03 15:24:40 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/knadh/listmonk/models"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetTemplates retrieves all templates.
|
2022-07-02 10:00:17 +00:00
|
|
|
func (c *Core) GetTemplates(status string, noBody bool) ([]models.Template, error) {
|
2022-04-03 15:24:40 +00:00
|
|
|
out := []models.Template{}
|
2022-07-02 10:00:17 +00:00
|
|
|
if err := c.q.GetTemplates.Select(&out, 0, noBody, status); err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
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
|
2022-07-02 10:00:17 +00:00
|
|
|
if err := c.q.GetTemplates.Select(&out, id, noBody, ""); err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
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.
|
2022-07-02 10:00:17 +00:00
|
|
|
func (c *Core) CreateTemplate(name, typ, subject string, body []byte) (models.Template, error) {
|
2022-04-03 15:24:40 +00:00
|
|
|
var newID int
|
2022-07-02 10:00:17 +00:00
|
|
|
if err := c.q.CreateTemplate.Get(&newID, name, typ, subject, body); err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
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.
|
2022-07-02 10:00:17 +00:00
|
|
|
func (c *Core) UpdateTemplate(id int, name, typ, subject string, body []byte) (models.Template, error) {
|
|
|
|
res, err := c.q.UpdateTemplate.Exec(id, name, typ, subject, body)
|
2022-04-03 15:24:40 +00:00
|
|
|
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
|
2022-07-02 11:25:42 +00:00
|
|
|
if err := c.q.DeleteTemplate.Get(&delID, id); err != nil && err != sql.ErrNoRows {
|
2022-04-03 15:24:40 +00:00
|
|
|
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
|
|
|
|
}
|