From 8d3dd589aa0bc9cc99d2742d7185895567345efb Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Thu, 21 Dec 2023 16:43:46 +0100 Subject: [PATCH] =?UTF-8?q?Allow=20to=20=E2=80=9Creplace=E2=80=9D=20carous?= =?UTF-8?q?el=20slides?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We realized that it was impossible to update the image of the home and services carousels: since i am using the add_{type}_carousel() function, it actually added a new slide instead of replacing the old one. We need this one for tomorrow, so i made a workaround creating the new slide, moving the relevant data from the old slide to the new, and then removing the old slide. Yuck. --- pkg/carousel/admin.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/carousel/admin.go b/pkg/carousel/admin.go index 687e1d2..23982a5 100644 --- a/pkg/carousel/admin.go +++ b/pkg/carousel/admin.go @@ -239,12 +239,23 @@ func CollectSlideEntries(ctx context.Context, company *auth.Company, conn *datab func addSlide(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, carouselName string) { f := newSlideForm(carouselName) - editSlide(w, r, user, company, conn, f) + f.process(w, r, user, company, conn, func(ctx context.Context) { + conn.MustExec(ctx, fmt.Sprintf("select add_%s_carousel_slide($1, $2)", f.CarouselName), f.Media, f.Caption) + }) } func editSlide(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *slideForm) { f.process(w, r, user, company, conn, func(ctx context.Context) { - conn.MustExec(ctx, fmt.Sprintf("select add_%s_carousel_slide($1, $2)", f.CarouselName), f.Media, f.Caption) + if f.ID == f.Media.Int() { + conn.MustExec(ctx, fmt.Sprintf("select add_%s_carousel_slide($1, $2)", f.CarouselName), f.Media, f.Caption) + } else { + tx := conn.MustBegin(ctx) + tx.MustExec(ctx, fmt.Sprintf("select add_%s_carousel_slide($1, $2)", f.CarouselName), f.Media, f.Caption) + tx.MustExec(ctx, fmt.Sprintf("update %[1]s_carousel set position = (select position from %[1]s_carousel where media_id = $2) where media_id = $1", f.CarouselName), f.Media, f.ID) + tx.MustExec(ctx, fmt.Sprintf("update %s_carousel_i18n set media_id = $1 where media_id = $2", f.CarouselName), f.Media, f.ID) + tx.MustExec(ctx, fmt.Sprintf("select remove_%s_carousel_slide($1)", f.CarouselName), f.ID) + tx.MustCommit(ctx) + } }) }