Add Go functions for campsite_type PostgreSQL functions
I want these because when there are changes in the signature i then have to find where it is used, and it is easier to do when the compiler tells you. For relations it is less necessary because GoLand knows how to validate SQL strings for them, but it seems to not work with functions, apparently due to the lack of the “FROM” keyword. Besides, it tx.FunctionName(ctx, params...) is shorter than tx.Exec("select functions_name($1, $2…)", params...).
This commit is contained in:
parent
0ebbf9613d
commit
59fe8dd131
|
@ -197,7 +197,7 @@ func addType(w http.ResponseWriter, r *http.Request, user *auth.User, company *a
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
processTypeForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
processTypeForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
||||||
slug, err := tx.GetText(ctx, "select add_campsite_type($1, $2, $3, $4, $5, $6, $7)", company.ID, f.Media, f.Name, f.Spiel, f.Description, f.MaxCampers, f.DogsAllowed)
|
slug, err := tx.AddCampsiteType(ctx, company.ID, f.Media.Int(), f.Name.Val, f.Spiel.Val, f.Description.Val, f.MaxCampers.Int(), f.DogsAllowed.Checked)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -207,7 +207,7 @@ func addType(w http.ResponseWriter, r *http.Request, user *auth.User, company *a
|
||||||
|
|
||||||
func setTypePrices(ctx context.Context, tx *database.Tx, slug string, prices map[int]*typePriceForm) error {
|
func setTypePrices(ctx context.Context, tx *database.Tx, slug string, prices map[int]*typePriceForm) error {
|
||||||
for seasonID, p := range prices {
|
for seasonID, p := range prices {
|
||||||
if _, err := tx.Exec(ctx, "select set_campsite_type_cost($1, $2, $3, $4)", slug, seasonID, p.MinNights, p.PricePerNight); err != nil {
|
if err := tx.SetCampsiteTypeCost(ctx, slug, seasonID, p.MinNights.Int(), p.PricePerNight.Val); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -216,7 +216,7 @@ func setTypePrices(ctx context.Context, tx *database.Tx, slug string, prices map
|
||||||
|
|
||||||
func editType(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *typeForm) {
|
func editType(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn, f *typeForm) {
|
||||||
processTypeForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
processTypeForm(w, r, user, company, conn, f, func(ctx context.Context, tx *database.Tx) error {
|
||||||
if _, err := conn.Exec(ctx, "select edit_campsite_type($1, $2, $3, $4, $5, $6, $7, $8)", f.Slug, f.Media, f.Name, f.Spiel, f.Description, f.MaxCampers, f.DogsAllowed, f.Active); err != nil {
|
if _, err := tx.EditCampsiteType(ctx, f.Slug, f.Media.Int(), f.Name.Val, f.Spiel.Val, f.Description.Val, f.MaxCampers.Int(), f.DogsAllowed.Checked, f.Active.Checked); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return setTypePrices(ctx, tx, f.Slug, f.Prices)
|
return setTypePrices(ctx, tx, f.Slug, f.Prices)
|
||||||
|
|
|
@ -58,7 +58,9 @@ func editTypeL10n(w http.ResponseWriter, r *http.Request, user *auth.User, compa
|
||||||
l10n.MustRender(w, r, user, company)
|
l10n.MustRender(w, r, user, company)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.MustExec(r.Context(), "select translate_campsite_type($1, $2, $3, $4, $5)", l10n.Slug, l10n.Locale.Language, l10n.Name, l10n.Spiel, l10n.Description)
|
if err := conn.TranslateCampsiteType(r.Context(), l10n.Slug, l10n.Locale.Language, l10n.Name.Val, l10n.Spiel.Val, l10n.Description.Val); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
httplib.Redirect(w, r, "/admin/campsites/types", http.StatusSeeOther)
|
httplib.Redirect(w, r, "/admin/campsites/types", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (tx *Tx) AddCampsiteType(ctx context.Context, companyID int, mediaID int, name string, spiel string, description string, maxCampers int, dogsAllowed bool) (string, error) {
|
||||||
|
return tx.GetText(ctx, "select add_campsite_type($1, $2, $3, $4, $5, $6, $7)", companyID, mediaID, name, spiel, description, maxCampers, dogsAllowed)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) EditCampsiteType(ctx context.Context, slug string, mediaID int, name string, spiel string, description string, maxCampers int, dogsAllowed bool, active bool) (string, error) {
|
||||||
|
return tx.GetText(ctx, "select edit_campsite_type($1, $2, $3, $4, $5, $6, $7, $8)", slug, mediaID, name, spiel, description, maxCampers, dogsAllowed, active)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) TranslateCampsiteType(ctx context.Context, slug string, langTag language.Tag, name string, spiel string, description string) error {
|
||||||
|
_, err := c.Exec(ctx, "select translate_campsite_type($1, $2, $3, $4, $5)", slug, langTag, name, spiel, description)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
func (tx *Tx) SetCampsiteTypeCost(ctx context.Context, slug string, seasonID int, minNights int, costPerNight string) error {
|
||||||
|
_, err := tx.Exec(ctx, "select set_campsite_type_cost($1, $2, $3, $4)", slug, seasonID, minNights, costPerNight)
|
||||||
|
return err
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ package form
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,6 +29,14 @@ func (input *Input) Value() (driver.Value, error) {
|
||||||
return input.Val, nil
|
return input.Val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (input *Input) Int() int {
|
||||||
|
i, err := strconv.Atoi(input.Val)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
func (input *Input) L10nInput() *L10nInput {
|
func (input *Input) L10nInput() *L10nInput {
|
||||||
return &L10nInput{
|
return &L10nInput{
|
||||||
Input: Input{
|
Input: Input{
|
||||||
|
|
Loading…
Reference in New Issue