2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2021-12-09 16:04:38 +00:00
|
|
|
"strings"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
"github.com/knadh/listmonk/models"
|
2021-12-09 15:21:07 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
2021-09-18 14:45:24 +00:00
|
|
|
// handleGetLists retrieves lists with additional metadata like subscriber counts. This may be slow.
|
2018-10-25 13:51:47 +00:00
|
|
|
func handleGetLists(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2022-11-08 18:05:31 +00:00
|
|
|
pg = app.paginator.NewFromURL(c.Request().URL.Query())
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2021-12-09 16:04:38 +00:00
|
|
|
query = strings.TrimSpace(c.FormValue("query"))
|
2021-09-18 14:45:24 +00:00
|
|
|
orderBy = c.FormValue("order_by")
|
|
|
|
order = c.FormValue("order")
|
|
|
|
minimal, _ = strconv.ParseBool(c.FormValue("minimal"))
|
|
|
|
listID, _ = strconv.Atoi(c.Param("id"))
|
2022-11-08 18:05:31 +00:00
|
|
|
|
|
|
|
out models.PageResults
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Fetch one list.
|
2021-12-09 16:04:38 +00:00
|
|
|
single := false
|
2018-10-25 13:51:47 +00:00
|
|
|
if listID > 0 {
|
|
|
|
single = true
|
|
|
|
}
|
|
|
|
|
2022-11-22 17:41:05 +00:00
|
|
|
if single {
|
|
|
|
out, err := app.core.GetList(listID, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
2021-12-09 16:04:38 +00:00
|
|
|
// Minimal query simply returns the list of all lists without JOIN subscriber counts. This is fast.
|
2021-09-18 14:45:24 +00:00
|
|
|
if !single && minimal {
|
2022-04-03 15:24:40 +00:00
|
|
|
res, err := app.core.GetLists("")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-09-18 14:45:24 +00:00
|
|
|
}
|
2022-04-03 15:24:40 +00:00
|
|
|
if len(res) == 0 {
|
2021-09-18 14:45:24 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meta.
|
2022-04-03 15:24:40 +00:00
|
|
|
out.Results = res
|
|
|
|
out.Total = len(res)
|
2021-09-18 14:45:24 +00:00
|
|
|
out.Page = 1
|
|
|
|
out.PerPage = out.Total
|
2022-04-03 15:24:40 +00:00
|
|
|
|
2021-09-18 14:45:24 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// Full list query.
|
2022-05-01 09:42:31 +00:00
|
|
|
res, total, err := app.core.QueryLists(query, orderBy, order, pg.Offset, pg.Limit)
|
2022-04-03 15:24:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-25 05:41:47 +00:00
|
|
|
}
|
2022-04-03 15:24:40 +00:00
|
|
|
|
|
|
|
if single && len(res) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.list}"))
|
2019-10-25 05:41:47 +00:00
|
|
|
}
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
if single {
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{res[0]})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 09:42:31 +00:00
|
|
|
out.Query = query
|
2022-04-03 15:24:40 +00:00
|
|
|
out.Results = res
|
2022-05-01 09:42:31 +00:00
|
|
|
out.Total = total
|
2019-05-14 11:11:05 +00:00
|
|
|
out.Page = pg.Page
|
|
|
|
out.PerPage = pg.PerPage
|
2021-09-18 13:44:58 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCreateList handles list creation.
|
|
|
|
func handleCreateList(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2022-04-03 15:24:40 +00:00
|
|
|
l = models.List{}
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
if err := c.Bind(&l); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate.
|
2022-04-03 15:24:40 +00:00
|
|
|
if !strHasLen(l.Name, 1, stdInputMaxLen) {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("lists.invalidName"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.CreateList(l)
|
2020-03-07 15:07:48 +00:00
|
|
|
if err != nil {
|
2022-04-03 15:24:40 +00:00
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00: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 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Incoming params.
|
2022-04-03 15:24:40 +00:00
|
|
|
var l models.List
|
|
|
|
if err := c.Bind(&l); err != nil {
|
2018-10-25 13:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
// Validate.
|
|
|
|
if !strHasLen(l.Name, 1, stdInputMaxLen) {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("lists.invalidName"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
out, err := app.core.UpdateList(id, l)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 17:11:48 +00:00
|
|
|
// handleDeleteLists handles list deletion, either a single one (ID in the URI), or a list.
|
2018-10-25 13:51:47 +00:00
|
|
|
func handleDeleteLists(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
|
2022-04-03 15:24:40 +00:00
|
|
|
ids []int
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if id < 1 && len(ids) == 0 {
|
2020-12-19 10:55:52 +00:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if id > 0 {
|
2022-04-03 15:24:40 +00:00
|
|
|
ids = append(ids, int(id))
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
if err := app.core.DeleteLists(ids); err != nil {
|
|
|
|
return err
|
2018-10-25 13:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|