Refactor get-campaign to accept uuid + get body

This commit is contained in:
Kailash Nadh 2020-04-26 15:51:26 +05:30
parent 6bba55f0eb
commit 3a9a2ef4ec
3 changed files with 9 additions and 5 deletions

View file

@ -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.")
}

View file

@ -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
}

View file

@ -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.