diff --git a/i18n/en.json b/i18n/en.json index 863825be2..fe4b2616d 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -502,7 +502,7 @@ "subscribers.status.unconfirmed": "Unconfirmed", "subscribers.status.unsubscribed": "Unsubscribed", "subscribers.subscribersDeleted": "{num} subscriber(s) deleted", - "templates.cantDeleteDefault": "Cannot delete default template", + "templates.cantDeleteDefault": "Cannot delete non-existent or default template", "templates.default": "Default", "templates.dummyName": "Dummy campaign", "templates.dummySubject": "Dummy campaign subject", diff --git a/internal/core/templates.go b/internal/core/templates.go index 42379ca72..307f8d7ba 100644 --- a/internal/core/templates.go +++ b/internal/core/templates.go @@ -1,6 +1,7 @@ package core import ( + "database/sql" "net/http" "github.com/knadh/listmonk/models" @@ -74,8 +75,7 @@ func (c *Core) SetDefaultTemplate(id int) error { // DeleteTemplate deletes a given template. func (c *Core) DeleteTemplate(id int) error { var delID int - if err := c.q.DeleteTemplate.Get(&delID, id); err != nil { - // TODO: Fix this. Deletes but always throws a "no result set" error. + if err := c.q.DeleteTemplate.Get(&delID, id); err != nil && err != sql.ErrNoRows { return echo.NewHTTPError(http.StatusInternalServerError, c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.template}", "error", pqErrMsg(err))) } diff --git a/queries.sql b/queries.sql index 8fec76a28..0015c539f 100644 --- a/queries.sql +++ b/queries.sql @@ -776,10 +776,12 @@ WITH tpl AS ( DELETE FROM templates WHERE id = $1 AND (SELECT COUNT(id) FROM templates) > 1 AND is_default = false RETURNING id ), def AS ( - SELECT id FROM templates WHERE is_default = true LIMIT 1 + SELECT id FROM templates WHERE is_default = true AND type='campaign' LIMIT 1 +), +up AS ( + UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1 ) -UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1 - RETURNING (SELECT id FROM tpl); +SELECT id FROM tpl; -- media