From d784291a0426402ca15bd74213b133bfe212746a Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Fri, 13 Oct 2023 19:05:17 +0200 Subject: [PATCH] Create Go functions for PostgreSQL functions for campsite type option I am going to base these to create the function for campsite type feature, and since i intend to --- pkg/campsite/types/l10n.go | 4 +++- pkg/campsite/types/option.go | 6 +++--- pkg/database/funcs.go | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/campsite/types/l10n.go b/pkg/campsite/types/l10n.go index 1ad7c81..023262f 100644 --- a/pkg/campsite/types/l10n.go +++ b/pkg/campsite/types/l10n.go @@ -125,7 +125,9 @@ func editOptionL10n(w http.ResponseWriter, r *http.Request, user *auth.User, com l10n.MustRender(w, r, user, company) return } - conn.MustExec(r.Context(), "select translate_campsite_type_option($1, $2, $3)", l10n.ID, l10n.Locale.Language, l10n.Name) + if err := conn.TranslateCampsiteTypeOption(r.Context(), l10n.ID, l10n.Locale.Language, l10n.Name.Val); err != nil { + panic(err) + } httplib.Redirect(w, r, "/admin/campsites/types/"+l10n.TypeSlug+"/options", http.StatusSeeOther) } diff --git a/pkg/campsite/types/option.go b/pkg/campsite/types/option.go index 00a3984..886e352 100644 --- a/pkg/campsite/types/option.go +++ b/pkg/campsite/types/option.go @@ -181,7 +181,7 @@ func addOption(w http.ResponseWriter, r *http.Request, user *auth.User, company panic(err) } processOptionForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error { - id, err := tx.GetInt(ctx, "select add_campsite_type_option($1, $2, $3, $4)", typeSlug, f.Name, f.Min, f.Max) + id, err := tx.AddCampsiteTypeOption(ctx, typeSlug, f.Name.Val, f.Min.Int(), f.Max.Int()) if err != nil { return err } @@ -191,7 +191,7 @@ func addOption(w http.ResponseWriter, r *http.Request, user *auth.User, company func editOption(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *optionForm) { processOptionForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error { - if _, err := conn.Exec(ctx, "select edit_campsite_type_option($1, $2, $3, $4)", f.ID, f.Name, f.Min, f.Max); err != nil { + if _, err := tx.EditCampsiteTypeOption(ctx, f.ID, f.Name.Val, f.Min.Int(), f.Max.Int()); err != nil { return err } return setOptionPrices(ctx, tx, f.ID, f.Prices) @@ -222,7 +222,7 @@ func processOptionForm(w http.ResponseWriter, r *http.Request, user *auth.User, func setOptionPrices(ctx context.Context, tx *database.Tx, id int, prices map[int]*optionPriceForm) error { for seasonID, p := range prices { - if _, err := tx.Exec(ctx, "select set_campsite_type_option_cost($1, $2, $3)", id, seasonID, p.PricePerNight); err != nil { + if err := tx.SetCampsiteTypeOptionCost(ctx, id, seasonID, p.PricePerNight.Val); err != nil { return err } } diff --git a/pkg/database/funcs.go b/pkg/database/funcs.go index cfdde75..afb8580 100644 --- a/pkg/database/funcs.go +++ b/pkg/database/funcs.go @@ -27,3 +27,21 @@ func (tx *Tx) SetCampsiteTypeCost(ctx context.Context, slug string, seasonID int _, err := tx.Exec(ctx, "select set_campsite_type_cost($1, $2, $3, $4)", slug, seasonID, minNights, costPerNight) return err } + +func (tx *Tx) AddCampsiteTypeOption(ctx context.Context, typeSlug string, name string, min int, max int) (int, error) { + return tx.GetInt(ctx, "select add_campsite_type_option($1, $2, $3, $4)", typeSlug, name, min, max) +} + +func (tx *Tx) EditCampsiteTypeOption(ctx context.Context, id int, name string, min int, max int) (int, error) { + return tx.GetInt(ctx, "select edit_campsite_type_option($1, $2, $3, $4)", id, name, min, max) +} + +func (tx *Tx) SetCampsiteTypeOptionCost(ctx context.Context, id int, seasonID int, pricePerNight string) error { + _, err := tx.Exec(ctx, "select set_campsite_type_option_cost($1, $2, $3)", id, seasonID, pricePerNight) + return err +} + +func (c *Conn) TranslateCampsiteTypeOption(ctx context.Context, id int, langTag language.Tag, name string) error { + _, err := c.Exec(ctx, "select translate_campsite_type_option($1, $2, $3)", id, langTag, name) + return err +}