From e59e6a2a420c20fa82e843f89c5a7c69ce2a1de3 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Mon, 7 Aug 2023 10:51:57 +0200 Subject: [PATCH] Move the locales parameter inside Company struct. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I realized that locales should be company-dependent: we could have two companies that show pages in a different subset of the application locales. It is not the case now, because despite being a “multicompany application”, it is intended for a single customer, but still makes sense to include it in Company, even if the subset is the same set as the application’s. --- pkg/app/app.go | 4 ++-- pkg/app/public.go | 9 ++++----- pkg/auth/company.go | 10 +++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/app/app.go b/pkg/app/app.go index 0a3fbf8..94bbb41 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -76,7 +76,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { panic(err) } - company, err := auth.CompanyByHost(r.Context(), conn, r.Host) + company, err := auth.CompanyByHost(r.Context(), conn, r.Host, h.locales) if database.ErrorIsNotFound(err) { http.NotFound(w, r) return @@ -116,7 +116,7 @@ func (h *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } user.Locale = urlLocale - h.public.Handler(user, company, h.locales, conn).ServeHTTP(w, r) + h.public.Handler(user, company, conn).ServeHTTP(w, r) } } } diff --git a/pkg/app/public.go b/pkg/app/public.go index c8d8fa8..c58dbd9 100644 --- a/pkg/app/public.go +++ b/pkg/app/public.go @@ -12,7 +12,6 @@ import ( "dev.tandem.ws/tandem/camper/pkg/auth" "dev.tandem.ws/tandem/camper/pkg/database" httplib "dev.tandem.ws/tandem/camper/pkg/http" - "dev.tandem.ws/tandem/camper/pkg/locale" "dev.tandem.ws/tandem/camper/pkg/template" ) @@ -22,14 +21,14 @@ func newPublicHandler() *publicHandler { return &publicHandler{} } -func (h *publicHandler) Handler(user *auth.User, company *auth.Company, locales locale.Locales, conn *database.Conn) http.Handler { +func (h *publicHandler) 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 "": page := newHomePage() - page.MustRender(w, r, user, company, locales) + page.MustRender(w, r, user, company) default: http.NotFound(w, r) } @@ -55,11 +54,11 @@ func newPublicPage(template string) *PublicPage { } } -func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company, locales locale.Locales) { +func (p *PublicPage) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) { schema := httplib.Protocol(r) authority := httplib.Host(r) _, path := httplib.ShiftPath(r.RequestURI) - for _, l := range locales { + for _, l := range company.Locales { p.LocalizedAlternates = append(p.LocalizedAlternates, &LocalizedAlternate{ Lang: l.Language.String(), Endonym: l.Endonym, diff --git a/pkg/auth/company.go b/pkg/auth/company.go index fc40a06..755641c 100644 --- a/pkg/auth/company.go +++ b/pkg/auth/company.go @@ -9,14 +9,18 @@ import ( "context" "dev.tandem.ws/tandem/camper/pkg/database" + "dev.tandem.ws/tandem/camper/pkg/locale" ) type Company struct { - ID int + ID int + Locales locale.Locales } -func CompanyByHost(ctx context.Context, conn *database.Conn, host string) (*Company, error) { - company := &Company{} +func CompanyByHost(ctx context.Context, conn *database.Conn, host string, allLocales locale.Locales) (*Company, error) { + company := &Company{ + Locales: allLocales, + } if err := conn.QueryRow(ctx, ` select company_id from company_host