listmonk/lists.go

176 lines
4 KiB
Go
Raw Normal View History

2018-10-25 13:51:47 +00:00
package main
import (
"fmt"
"net/http"
"strconv"
2020-03-07 15:07:48 +00:00
"github.com/gofrs/uuid"
2018-10-25 13:51:47 +00:00
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
"github.com/asaskevich/govalidator"
"github.com/labstack/echo"
)
2019-05-14 11:11:05 +00:00
type listsWrap struct {
Results []models.List `json:"results"`
Total int `json:"total"`
PerPage int `json:"per_page"`
Page int `json:"page"`
}
2018-10-25 13:51:47 +00:00
// handleGetLists handles retrieval of lists.
func handleGetLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
2019-05-14 11:11:05 +00:00
out listsWrap
2018-10-25 13:51:47 +00:00
2019-05-14 11:11:05 +00:00
pg = getPagination(c.QueryParams())
2018-10-25 13:51:47 +00:00
listID, _ = strconv.Atoi(c.Param("id"))
single = false
)
// Fetch one list.
if listID > 0 {
single = true
}
2019-05-14 11:11:05 +00:00
err := app.Queries.GetLists.Select(&out.Results, listID, pg.Offset, pg.Limit)
2018-10-25 13:51:47 +00:00
if err != nil {
2020-03-07 17:30:55 +00:00
app.Logger.Printf("error fetching lists: %v", err)
2018-10-25 13:51:47 +00:00
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error fetching lists: %s", pqErrMsg(err)))
}
if single && len(out.Results) == 0 {
2018-10-25 13:51:47 +00:00
return echo.NewHTTPError(http.StatusBadRequest, "List not found.")
}
if len(out.Results) == 0 {
2018-10-25 13:51:47 +00:00
return c.JSON(http.StatusOK, okResp{[]struct{}{}})
}
// Replace null tags.
2019-05-14 11:11:05 +00:00
for i, v := range out.Results {
2018-10-25 13:51:47 +00:00
if v.Tags == nil {
2019-05-14 11:11:05 +00:00
out.Results[i].Tags = make(pq.StringArray, 0)
2018-10-25 13:51:47 +00:00
}
}
if single {
2019-05-14 11:11:05 +00:00
return c.JSON(http.StatusOK, okResp{out.Results[0]})
2018-10-25 13:51:47 +00:00
}
2019-05-14 11:11:05 +00:00
// Meta.
out.Total = out.Results[0].Total
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)
o = models.List{}
)
if err := c.Bind(&o); err != nil {
return err
}
// Validate.
if !govalidator.IsByteLength(o.Name, 1, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest,
"Invalid length for the name field.")
}
2020-03-07 15:07:48 +00:00
uu, err := uuid.NewV4()
if err != nil {
2020-03-07 17:30:55 +00:00
app.Logger.Printf("error generating UUID: %v", err)
2020-03-07 15:07:48 +00:00
return echo.NewHTTPError(http.StatusInternalServerError, "Error generating UUID")
}
2018-10-25 13:51:47 +00:00
// Insert and read ID.
var newID int
2020-03-07 15:07:48 +00:00
o.UUID = uu.String()
2018-10-25 13:51:47 +00:00
if err := app.Queries.CreateList.Get(&newID,
o.UUID,
o.Name,
o.Type,
o.Optin,
2018-10-25 13:51:47 +00:00
pq.StringArray(normalizeTags(o.Tags))); err != nil {
2020-03-07 17:30:55 +00:00
app.Logger.Printf("error creating list: %v", err)
2018-10-25 13:51:47 +00:00
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error creating list: %s", pqErrMsg(err)))
}
// Hand over to the GET handler to return the last insertion.
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", newID))
return c.JSON(http.StatusOK, handleGetLists(c))
}
// 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 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}
// Incoming params.
var o models.List
if err := c.Bind(&o); err != nil {
return err
}
2020-03-07 17:30:55 +00:00
res, err := app.Queries.UpdateList.Exec(id,
o.Name, o.Type, o.Optin, pq.StringArray(normalizeTags(o.Tags)))
2018-10-25 13:51:47 +00:00
if err != nil {
2020-03-07 17:30:55 +00:00
app.Logger.Printf("error updating list: %v", err)
2018-10-25 13:51:47 +00:00
return echo.NewHTTPError(http.StatusBadRequest,
fmt.Sprintf("Error updating list: %s", pqErrMsg(err)))
}
if n, _ := res.RowsAffected(); n == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "List not found.")
}
return handleGetLists(c)
}
// handleDeleteLists handles deletion deletion,
// either a single one (ID in the URI), or a list.
func handleDeleteLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
ids pq.Int64Array
)
// Read the list IDs if they were sent in the body.
if err := c.Bind(&ids); err != nil {
return err
}
2018-10-25 13:51:47 +00:00
if id < 1 && len(ids) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}
if id > 0 {
ids = append(ids, id)
}
if _, err := app.Queries.DeleteLists.Exec(ids); err != nil {
2020-03-07 17:30:55 +00:00
app.Logger.Printf("error deleting lists: %v", err)
2018-10-25 13:51:47 +00:00
return echo.NewHTTPError(http.StatusInternalServerError,
2020-03-07 17:30:55 +00:00
fmt.Sprintf("Error deleting: %v", err))
2018-10-25 13:51:47 +00:00
}
return c.JSON(http.StatusOK, okResp{true})
}