2023-12-21 20:17:04 +00:00
|
|
|
package location
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
|
|
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/auth"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/form"
|
|
|
|
httplib "dev.tandem.ws/tandem/camper/pkg/http"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/locale"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AdminHandler struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAdminHandler() *AdminHandler {
|
|
|
|
return &AdminHandler{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *AdminHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var head string
|
|
|
|
head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
|
|
|
|
|
|
|
|
switch head {
|
|
|
|
case "":
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
2024-01-12 18:51:12 +00:00
|
|
|
f := newLocationForm(company)
|
2023-12-21 20:17:04 +00:00
|
|
|
if err := f.FillFromDatabase(r.Context(), company, conn); err != nil {
|
|
|
|
if !database.ErrorIsNotFound(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.MustRender(w, r, user, company)
|
|
|
|
case http.MethodPut:
|
|
|
|
updateLocationSettings(w, r, user, company, conn)
|
|
|
|
default:
|
|
|
|
httplib.MethodNotAllowed(w, r, http.MethodGet)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLocationSettings(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, conn *database.Conn) {
|
2024-01-12 18:51:12 +00:00
|
|
|
f := newLocationForm(company)
|
2023-12-21 20:17:04 +00:00
|
|
|
if ok, err := form.Handle(f, w, r, user); err != nil {
|
|
|
|
return
|
|
|
|
} else if !ok {
|
|
|
|
f.MustRender(w, r, user, company)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tx := conn.MustBegin(r.Context())
|
|
|
|
defer tx.Rollback(r.Context())
|
2024-01-12 18:51:12 +00:00
|
|
|
if err := tx.SetupLocation(r.Context(), company.ID, f.Directions[f.DefaultLang].Val, f.MapEmbed.Val, f.OpeningDates[f.DefaultLang].Val); err != nil {
|
2023-12-21 20:17:04 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for lang := range company.Locales {
|
2024-01-12 18:51:12 +00:00
|
|
|
l := lang.String()
|
|
|
|
if l == f.DefaultLang {
|
2023-12-21 20:17:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-01-12 18:51:12 +00:00
|
|
|
if err := tx.TranslateLocation(r.Context(), company.ID, lang, f.Directions[l].Val, f.OpeningDates[l].Val); err != nil {
|
2023-12-21 20:17:04 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.MustCommit(r.Context())
|
|
|
|
httplib.Redirect(w, r, "/admin/location", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
type locationForm struct {
|
2024-01-12 18:51:12 +00:00
|
|
|
DefaultLang string
|
2023-12-21 20:17:04 +00:00
|
|
|
Directions form.I18nInput
|
|
|
|
MapEmbed *form.Input
|
|
|
|
OpeningDates form.I18nInput
|
|
|
|
}
|
|
|
|
|
2024-01-12 18:51:12 +00:00
|
|
|
func newLocationForm(company *auth.Company) *locationForm {
|
2023-12-21 20:17:04 +00:00
|
|
|
return &locationForm{
|
2024-01-12 18:51:12 +00:00
|
|
|
DefaultLang: company.DefaultLanguage.String(),
|
|
|
|
Directions: form.NewI18nInput(company.Locales, "directions"),
|
2023-12-21 20:17:04 +00:00
|
|
|
MapEmbed: &form.Input{
|
|
|
|
Name: "map_embed",
|
|
|
|
},
|
2024-01-12 18:51:12 +00:00
|
|
|
OpeningDates: form.NewI18nInput(company.Locales, "opening_dates"),
|
2023-12-21 20:17:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *locationForm) FillFromDatabase(ctx context.Context, company *auth.Company, conn *database.Conn) error {
|
|
|
|
var directions database.RecordArray
|
|
|
|
var openingDates database.RecordArray
|
|
|
|
err := conn.QueryRow(ctx, `
|
|
|
|
select location.directions::text
|
|
|
|
, location.map_embed::text
|
|
|
|
, location.opening_dates::text
|
|
|
|
, array_agg((lang_tag, i18n.directions::text))
|
|
|
|
, array_agg((lang_tag, i18n.opening_dates::text))
|
|
|
|
from location
|
|
|
|
left join location_i18n as i18n using (company_id)
|
|
|
|
where company_id = $1
|
|
|
|
group by location.directions::text
|
|
|
|
, location.map_embed::text
|
|
|
|
, location.opening_dates::text
|
|
|
|
`, pgx.QueryResultFormats{pgx.BinaryFormatCode}, company.ID).Scan(
|
2024-01-12 18:51:12 +00:00
|
|
|
&f.Directions[f.DefaultLang].Val,
|
2023-12-21 20:17:04 +00:00
|
|
|
&f.MapEmbed.Val,
|
2024-01-12 18:51:12 +00:00
|
|
|
&f.OpeningDates[f.DefaultLang].Val,
|
2023-12-21 20:17:04 +00:00
|
|
|
&directions,
|
|
|
|
&openingDates,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-22 01:23:18 +00:00
|
|
|
if err := f.Directions.FillArray(directions); err != nil {
|
2023-12-21 20:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-12-22 01:23:18 +00:00
|
|
|
if err := f.OpeningDates.FillArray(openingDates); err != nil {
|
2023-12-21 20:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *locationForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
|
|
|
|
template.MustRenderAdmin(w, r, user, company, "location.gohtml", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *locationForm) Parse(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Directions.FillValue(r)
|
|
|
|
f.MapEmbed.FillValue(r)
|
|
|
|
f.OpeningDates.FillValue(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *locationForm) Valid(l *locale.Locale) bool {
|
|
|
|
v := form.NewValidator(l)
|
|
|
|
return v.AllOK
|
|
|
|
}
|