2018-10-25 21:51:47 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2020-03-07 23:07:48 +08:00
|
|
|
"github.com/gofrs/uuid"
|
2018-10-25 21:51:47 +08:00
|
|
|
"github.com/knadh/listmonk/models"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2019-05-14 19:11:05 +08:00
|
|
|
type listsWrap struct {
|
|
|
|
Results []models.List `json:"results"`
|
|
|
|
|
|
|
|
Total int `json:"total"`
|
|
|
|
PerPage int `json:"per_page"`
|
|
|
|
Page int `json:"page"`
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:30:29 +08:00
|
|
|
var (
|
|
|
|
listQuerySortFields = []string{"name", "type", "subscriber_count", "created_at", "updated_at"}
|
|
|
|
)
|
|
|
|
|
2018-10-25 21:51:47 +08:00
|
|
|
// handleGetLists handles retrieval of lists.
|
|
|
|
func handleGetLists(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2019-05-14 19:11:05 +08:00
|
|
|
out listsWrap
|
2018-10-25 21:51:47 +08:00
|
|
|
|
2021-06-05 14:31:33 +08:00
|
|
|
pg = getPagination(c.QueryParams(), 20)
|
2020-10-24 22:30:29 +08:00
|
|
|
orderBy = c.FormValue("order_by")
|
|
|
|
order = c.FormValue("order")
|
2018-10-25 21:51:47 +08:00
|
|
|
listID, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
single = false
|
|
|
|
)
|
|
|
|
|
|
|
|
// Fetch one list.
|
|
|
|
if listID > 0 {
|
|
|
|
single = true
|
|
|
|
}
|
|
|
|
|
2020-10-24 22:30:29 +08:00
|
|
|
// Sort params.
|
|
|
|
if !strSliceContains(orderBy, listQuerySortFields) {
|
|
|
|
orderBy = "created_at"
|
|
|
|
}
|
|
|
|
if order != sortAsc && order != sortDesc {
|
|
|
|
order = sortAsc
|
|
|
|
}
|
|
|
|
|
2021-01-31 18:49:39 +08:00
|
|
|
if err := db.Select(&out.Results, fmt.Sprintf(app.queries.QueryLists, orderBy, order), listID, pg.Offset, pg.Limit); err != nil {
|
2020-03-08 02:33:22 +08:00
|
|
|
app.log.Printf("error fetching lists: %v", err)
|
2018-10-25 21:51:47 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorFetching",
|
2021-01-23 21:18:10 +08:00
|
|
|
"name", "{globals.terms.lists}", "error", pqErrMsg(err)))
|
2019-10-25 13:41:47 +08:00
|
|
|
}
|
|
|
|
if single && len(out.Results) == 0 {
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.list}"))
|
2019-10-25 13:41:47 +08:00
|
|
|
}
|
|
|
|
if len(out.Results) == 0 {
|
2018-10-25 21:51:47 +08:00
|
|
|
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace null tags.
|
2019-05-14 19:11:05 +08:00
|
|
|
for i, v := range out.Results {
|
2018-10-25 21:51:47 +08:00
|
|
|
if v.Tags == nil {
|
2019-05-14 19:11:05 +08:00
|
|
|
out.Results[i].Tags = make(pq.StringArray, 0)
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if single {
|
2019-05-14 19:11:05 +08:00
|
|
|
return c.JSON(http.StatusOK, okResp{out.Results[0]})
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
2019-05-14 19:11:05 +08:00
|
|
|
// Meta.
|
|
|
|
out.Total = out.Results[0].Total
|
|
|
|
out.Page = pg.Page
|
|
|
|
out.PerPage = pg.PerPage
|
2021-09-18 21:44:58 +08:00
|
|
|
if out.PerPage == 0 {
|
|
|
|
out.PerPage = out.Total
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:51:47 +08:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCreateList handles list creation.
|
|
|
|
func handleCreateList(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
o = models.List{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := c.Bind(&o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate.
|
2020-03-08 15:33:38 +08:00
|
|
|
if !strHasLen(o.Name, 1, stdInputMaxLen) {
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("lists.invalidName"))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
2020-03-07 23:07:48 +08:00
|
|
|
uu, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
2020-03-08 02:33:22 +08:00
|
|
|
app.log.Printf("error generating UUID: %v", err)
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorUUID", "error", err.Error()))
|
2020-03-07 23:07:48 +08:00
|
|
|
}
|
|
|
|
|
2018-10-25 21:51:47 +08:00
|
|
|
// Insert and read ID.
|
|
|
|
var newID int
|
2020-03-07 23:07:48 +08:00
|
|
|
o.UUID = uu.String()
|
2020-03-08 02:33:22 +08:00
|
|
|
if err := app.queries.CreateList.Get(&newID,
|
2018-10-25 21:51:47 +08:00
|
|
|
o.UUID,
|
|
|
|
o.Name,
|
|
|
|
o.Type,
|
2019-12-01 20:18:36 +08:00
|
|
|
o.Optin,
|
2018-10-25 21:51:47 +08:00
|
|
|
pq.StringArray(normalizeTags(o.Tags))); err != nil {
|
2020-03-08 02:33:22 +08:00
|
|
|
app.log.Printf("error creating list: %v", err)
|
2018-10-25 21:51:47 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorCreating",
|
2020-12-19 18:55:52 +08:00
|
|
|
"name", "{globals.terms.list}", "error", pqErrMsg(err)))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hand over to the GET handler to return the last insertion.
|
2020-11-08 21:06:25 +08:00
|
|
|
return handleGetLists(copyEchoCtx(c, map[string]string{
|
|
|
|
"id": fmt.Sprintf("%d", newID),
|
|
|
|
}))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleUpdateList handles list modification.
|
|
|
|
func handleUpdateList(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.Atoi(c.Param("id"))
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 {
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Incoming params.
|
|
|
|
var o models.List
|
|
|
|
if err := c.Bind(&o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-08 02:33:22 +08:00
|
|
|
res, err := app.queries.UpdateList.Exec(id,
|
2020-03-08 01:30:55 +08:00
|
|
|
o.Name, o.Type, o.Optin, pq.StringArray(normalizeTags(o.Tags)))
|
2018-10-25 21:51:47 +08:00
|
|
|
if err != nil {
|
2020-03-08 02:33:22 +08:00
|
|
|
app.log.Printf("error updating list: %v", err)
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorUpdating",
|
2020-12-19 18:55:52 +08:00
|
|
|
"name", "{globals.terms.list}", "error", pqErrMsg(err)))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if n, _ := res.RowsAffected(); n == 0 {
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.list}"))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return handleGetLists(c)
|
|
|
|
}
|
|
|
|
|
2021-05-25 01:11:48 +08:00
|
|
|
// handleDeleteLists handles list deletion, either a single one (ID in the URI), or a list.
|
2018-10-25 21:51:47 +08:00
|
|
|
func handleDeleteLists(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
ids pq.Int64Array
|
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 && len(ids) == 0 {
|
2020-12-19 18:55:52 +08:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if id > 0 {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
|
2020-03-08 02:33:22 +08:00
|
|
|
if _, err := app.queries.DeleteLists.Exec(ids); err != nil {
|
|
|
|
app.log.Printf("error deleting lists: %v", err)
|
2018-10-25 21:51:47 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 21:24:33 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorDeleting",
|
2020-12-19 18:55:52 +08:00
|
|
|
"name", "{globals.terms.list}", "error", pqErrMsg(err)))
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|