listmonk/cmd/lists.go

160 lines
3.3 KiB
Go
Raw Normal View History

2018-10-25 13:51:47 +00:00
package main
import (
"net/http"
"strconv"
"strings"
2018-10-25 13:51:47 +00:00
"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
2018-10-25 13:51:47 +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)
pg = app.paginator.NewFromURL(c.Request().URL.Query())
2018-10-25 13:51:47 +00:00
query = strings.TrimSpace(c.FormValue("query"))
orderBy = c.FormValue("order_by")
order = c.FormValue("order")
minimal, _ = strconv.ParseBool(c.FormValue("minimal"))
listID, _ = strconv.Atoi(c.Param("id"))
out models.PageResults
2018-10-25 13:51:47 +00:00
)
// Fetch one list.
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})
}
// Minimal query simply returns the list of all lists without JOIN subscriber counts. This is fast.
if !single && minimal {
res, err := app.core.GetLists("")
if err != nil {
return err
}
if len(res) == 0 {
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
}
// Meta.
out.Results = res
out.Total = len(res)
out.Page = 1
out.PerPage = out.Total
return c.JSON(http.StatusOK, okResp{out})
}
// Full list query.
res, total, err := app.core.QueryLists(query, orderBy, order, pg.Offset, pg.Limit)
if err != nil {
return err
}
if single && len(res) == 0 {
2020-12-19 10:55:52 +00:00
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.list}"))
}
2018-10-25 13:51:47 +00:00
if single {
return c.JSON(http.StatusOK, okResp{res[0]})
2018-10-25 13:51:47 +00:00
}
out.Query = query
out.Results = res
out.Total = total
2019-05-14 11:11:05 +00:00
out.Page = pg.Page
out.PerPage = pg.PerPage
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)
l = models.List{}
2018-10-25 13:51:47 +00:00
)
if err := c.Bind(&l); err != nil {
2018-10-25 13:51:47 +00:00
return err
}
// Validate.
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
}
out, err := app.core.CreateList(l)
2020-03-07 15:07:48 +00:00
if err != nil {
return err
2018-10-25 13:51:47 +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.
var l models.List
if err := c.Bind(&l); err != nil {
2018-10-25 13:51:47 +00:00
return err
}
// 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
}
out, err := app.core.UpdateList(id, l)
if err != nil {
return err
2018-10-25 13:51:47 +00:00
}
return c.JSON(http.StatusOK, okResp{out})
2018-10-25 13:51:47 +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)
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 {
ids = append(ids, int(id))
2018-10-25 13:51:47 +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})
}