Allow to “replace” carousel slides

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.
This commit is contained in:
jordi fita mas 2023-12-21 16:43:46 +01:00
parent a7e62dbe70
commit 8d3dd589aa
1 changed files with 13 additions and 2 deletions

View File

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