From 3a9a2ef4eca3a381d82021027a374e90d30db495 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sun, 26 Apr 2020 15:51:26 +0530 Subject: [PATCH] Refactor `get-campaign` to accept uuid + get body --- campaigns.go | 6 +++--- manager_db.go | 2 +- queries.sql | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/campaigns.go b/campaigns.go index 5c31c63..f62f2f3 100644 --- a/campaigns.go +++ b/campaigns.go @@ -274,7 +274,7 @@ func handleUpdateCampaign(c echo.Context) error { } var cm models.Campaign - if err := app.queries.GetCampaign.Get(&cm, id); err != nil { + if err := app.queries.GetCampaign.Get(&cm, id, nil); err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.") } @@ -337,7 +337,7 @@ func handleUpdateCampaignStatus(c echo.Context) error { } var cm models.Campaign - if err := app.queries.GetCampaign.Get(&cm, id); err != nil { + if err := app.queries.GetCampaign.Get(&cm, id, nil); err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.") } @@ -412,7 +412,7 @@ func handleDeleteCampaign(c echo.Context) error { } var cm models.Campaign - if err := app.queries.GetCampaign.Get(&cm, id); err != nil { + if err := app.queries.GetCampaign.Get(&cm, id, nil); err != nil { if err == sql.ErrNoRows { return echo.NewHTTPError(http.StatusBadRequest, "Campaign not found.") } diff --git a/manager_db.go b/manager_db.go index 30eeb74..7ed7d96 100644 --- a/manager_db.go +++ b/manager_db.go @@ -38,7 +38,7 @@ func (r *runnerDB) NextSubscribers(campID, limit int) ([]models.Subscriber, erro // GetCampaign fetches a campaign from the database. func (r *runnerDB) GetCampaign(campID int) (*models.Campaign, error) { var out = &models.Campaign{} - err := r.queries.GetCampaign.Get(out, campID) + err := r.queries.GetCampaign.Get(out, campID, nil) return out, err } diff --git a/queries.sql b/queries.sql index f71bf55..cfc44c0 100644 --- a/queries.sql +++ b/queries.sql @@ -382,7 +382,11 @@ WHERE ($1 = 0 OR id = $1) ORDER BY created_at DESC OFFSET $4 LIMIT $5; -- name: get-campaign -SELECT * FROM campaigns WHERE id = $1; +SELECT campaigns.*, + COALESCE(templates.body, (SELECT body FROM templates WHERE is_default = true LIMIT 1)) AS template_body + FROM campaigns + LEFT JOIN templates ON (templates.id = campaigns.template_id) + WHERE CASE WHEN $1 > 0 THEN campaigns.id = $1 ELSE uuid = $2 END; -- name: get-campaign-stats -- This query is used to lazy load campaign stats (views, counts, list of lists) given a list of campaign IDs.