-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtemplates.go
More file actions
108 lines (88 loc) · 3.93 KB
/
templates.go
File metadata and controls
108 lines (88 loc) · 3.93 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package core
import (
"database/sql"
"net/http"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
null "gopkg.in/volatiletech/null.v6"
)
// GetTemplates retrieves all templates.
func (c *Core) GetTemplates(status string, noBody bool) ([]models.Template, error) {
out := []models.Template{}
if err := c.q.GetTemplates.Select(&out, 0, noBody, status); err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.templates}", "error", pqErrMsg(err)))
}
return out, nil
}
// QueryTemplates retrieves paginated templates optionally filtering them by the given
// search string. It also returns the total number of records in the DB.
func (c *Core) QueryTemplates(searchStr, typ, orderBy, order string, noBody bool, offset, limit int) (models.Templates, int, error) {
queryStr, stmt := makeSearchQuery(searchStr, orderBy, order, c.q.QueryTemplates, templateQuerySortFields)
var out models.Templates
if err := c.db.Select(&out, stmt, noBody, typ, queryStr, offset, limit); err != nil {
c.log.Printf("error fetching templates: %v", err)
return nil, 0, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.templates}", "error", pqErrMsg(err)))
}
total := 0
if len(out) > 0 {
total = out[0].Total
}
return out, total, 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, typ, subject string, body []byte, bodySource null.String) (models.Template, error) {
var newID int
if err := c.q.CreateTemplate.Get(&newID, name, typ, subject, body, bodySource); 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, subject string, body []byte, bodySource null.String) (models.Template, error) {
res, err := c.q.UpdateTemplate.Exec(id, name, subject, body, bodySource)
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 && err != sql.ErrNoRows {
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
}