Skip to content

Commit

Permalink
Merge big refactor with the 'core' branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed May 8, 2022
2 parents b4f0c7e + 959541f commit a3fd461
Show file tree
Hide file tree
Showing 31 changed files with 2,205 additions and 1,379 deletions.
15 changes: 6 additions & 9 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"syscall"
"time"

"github.com/jmoiron/sqlx/types"
"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -61,12 +60,11 @@ func handleGetServerConfig(c echo.Context) error {
func handleGetDashboardCharts(c echo.Context) error {
var (
app = c.Get("app").(*App)
out types.JSONText
)

if err := app.queries.GetDashboardCharts.Get(&out); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorFetching", "name", "dashboard charts", "error", pqErrMsg(err)))
out, err := app.core.GetDashboardCharts()
if err != nil {
return err
}

return c.JSON(http.StatusOK, okResp{out})
Expand All @@ -76,12 +74,11 @@ func handleGetDashboardCharts(c echo.Context) error {
func handleGetDashboardCounts(c echo.Context) error {
var (
app = c.Get("app").(*App)
out types.JSONText
)

if err := app.queries.GetDashboardCounts.Get(&out); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorFetching", "name", "dashboard stats", "error", pqErrMsg(err)))
out, err := app.core.GetDashboardCounts()
if err != nil {
return err
}

return c.JSON(http.StatusOK, okResp{out})
Expand Down
75 changes: 24 additions & 51 deletions cmd/bounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,20 @@ package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"

"github.com/knadh/listmonk/models"
"github.com/labstack/echo/v4"
"github.com/lib/pq"
)

type bouncesWrap struct {
Results []models.Bounce `json:"results"`

Total int `json:"total"`
PerPage int `json:"per_page"`
Page int `json:"page"`
}

// handleGetBounces handles retrieval of bounce records.
func handleGetBounces(c echo.Context) error {
var (
app = c.Get("app").(*App)
pg = getPagination(c.QueryParams(), 50)
out bouncesWrap

id, _ = strconv.Atoi(c.Param("id"))
campID, _ = strconv.Atoi(c.QueryParam("campaign_id"))
Expand All @@ -35,38 +24,30 @@ func handleGetBounces(c echo.Context) error {
order = c.FormValue("order")
)

// Fetch one list.
single := false
// Fetch one bounce.
if id > 0 {
single = true
out, err := app.core.GetBounce(id)
if err != nil {
return err
}
return c.JSON(http.StatusOK, okResp{out})
}

// Sort params.
if !strSliceContains(orderBy, bounceQuerySortFields) {
orderBy = "created_at"
}
if order != sortAsc && order != sortDesc {
order = sortDesc
res, total, err := app.core.QueryBounces(campID, 0, source, orderBy, order, pg.Offset, pg.Limit)
if err != nil {
return err
}

stmt := fmt.Sprintf(app.queries.QueryBounces, orderBy, order)
if err := db.Select(&out.Results, stmt, id, campID, 0, source, pg.Offset, pg.Limit); err != nil {
app.log.Printf("error fetching bounces: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorFetching",
"name", "{globals.terms.bounce}", "error", pqErrMsg(err)))
}
if len(out.Results) == 0 {
// No results.
var out models.PageResults
if len(res) == 0 {
out.Results = []models.Bounce{}
return c.JSON(http.StatusOK, okResp{out})
}

if single {
return c.JSON(http.StatusOK, okResp{out.Results[0]})
}

// Meta.
out.Total = out.Results[0].Total
out.Results = res
out.Total = total
out.Page = pg.Page
out.PerPage = pg.PerPage

Expand All @@ -76,22 +57,17 @@ func handleGetBounces(c echo.Context) error {
// handleGetSubscriberBounces retrieves a subscriber's bounce records.
func handleGetSubscriberBounces(c echo.Context) error {
var (
app = c.Get("app").(*App)
subID = c.Param("id")
app = c.Get("app").(*App)
subID, _ = strconv.Atoi(c.Param("id"))
)

id, _ := strconv.ParseInt(subID, 10, 64)
if id < 1 {
if subID < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}

out := []models.Bounce{}
stmt := fmt.Sprintf(app.queries.QueryBounces, "created_at", "ASC")
if err := db.Select(&out, stmt, 0, 0, subID, "", 0, 1000); err != nil {
app.log.Printf("error fetching bounces: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorFetching",
"name", "{globals.terms.bounce}", "error", pqErrMsg(err)))
out, _, err := app.core.QueryBounces(0, subID, "", "", "", 0, 1000)
if err != nil {
return err
}

return c.JSON(http.StatusOK, okResp{out})
Expand All @@ -103,12 +79,12 @@ func handleDeleteBounces(c echo.Context) error {
app = c.Get("app").(*App)
pID = c.Param("id")
all, _ = strconv.ParseBool(c.QueryParam("all"))
IDs = pq.Int64Array{}
IDs = []int{}
)

// Is it an /:id call?
if pID != "" {
id, _ := strconv.ParseInt(pID, 10, 64)
id, _ := strconv.Atoi(pID)
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
Expand All @@ -128,11 +104,8 @@ func handleDeleteBounces(c echo.Context) error {
IDs = i
}

if _, err := app.queries.DeleteBounces.Exec(IDs); err != nil {
app.log.Printf("error deleting bounces: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("globals.messages.errorDeleting",
"name", "{globals.terms.bounce}", "error", pqErrMsg(err)))
if err := app.core.DeleteBounces(IDs); err != nil {
return err
}

return c.JSON(http.StatusOK, okResp{true})
Expand Down
Loading

0 comments on commit a3fd461

Please sign in to comment.