diff --git a/internal/server/handle_public.go b/internal/server/handle_public.go index af3b32b..8f0df5d 100644 --- a/internal/server/handle_public.go +++ b/internal/server/handle_public.go @@ -1,24 +1,41 @@ package server import ( + "errors" + "fmt" "net/http" + "time" "github.com/labstack/echo/v4" - "github.com/rjNemo/rentease/config" "github.com/rjNemo/rentease/internal/view" + myTime "github.com/rjNemo/rentease/pkg/time" ) -func handleHomePage(hc *config.Host) echo.HandlerFunc { +func handleHomePage() echo.HandlerFunc { return func(ctx echo.Context) error { - return renderTempl(ctx, http.StatusOK, view.Index(&view.HomePageViewModel{ - Name: hc.Name, - Address: hc.Address, - ZipCode: hc.ZipCode, - City: hc.City, - PhoneNumber: hc.PhoneNumber, - Email: hc.Email, - Items: hc.Items, - })) + return renderTempl(ctx, http.StatusOK, view.Index()) + } +} + +func handleRequestBooking() echo.HandlerFunc { + return func(c echo.Context) error { + // validate the form request + // no time travelling + fromStr := c.FormValue("from") + toStr := c.FormValue("to") + from, fErr := myTime.ParseFromForm(fromStr) + to, tErr := myTime.ParseFromForm(toStr) + if fErr != nil || tErr != nil { + return fmt.Errorf("error parsing booking request time: %q %q", fErr, tErr) + } + + errs := make([]error, 0) + if to.Sub(from) < time.Duration(0) { + errs = append(errs, errors.New("invalid_time_range")) + } + // a communication mean + // + return renderTempl(c, http.StatusOK, view.RequestBookingForm(errs)) } } diff --git a/internal/server/routes.go b/internal/server/routes.go index 5c8ac9b..fef3475 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -1,7 +1,10 @@ package server func (s Server) MountHandlers() { - s.Router.GET("/", handleHomePage(s.hc)) + // public + s.Router.GET("/", handleHomePage()) + s.Router.POST("/request-booking", handleRequestBooking()) + // admin s.Router.GET("/bookings", handleListBookingPage(s.bs, s.hc)) s.Router.GET("/bookings/new", handleNewBookingPage()) s.Router.POST("/bookings/new", handleCreateBooking(s.bs)) diff --git a/internal/view/index.templ b/internal/view/index.templ index 3a0e8db..e8e5338 100644 --- a/internal/view/index.templ +++ b/internal/view/index.templ @@ -1,63 +1,82 @@ package view -import ( - "github.com/rjNemo/rentease/config" -) +import "github.com/rjNemo/rentease/config" -type HomePageViewModel struct { - Name string - Address string - ZipCode string - City string - PhoneNumber string - Email string - Items []config.HostItem +func makeItems() []string { + host := config.NewHost() + items := make([]string, 0, 2) + for _, i := range host.Items { + if i.Name == "T2" || i.Name == "T3" { + items = append(items, i.Name) + } + } + return items } -templ Index(host *HomePageViewModel) { +var items = makeItems() + +func invalidTimeRange(errs []error) bool { + for _, e := range errs { + if e.Error() == "invalid_time_range" { + return true + } + } + return false +} + +templ RequestBookingForm(errors []error) { +
+
+ + + +
+
+ + + +
+ + +
+} + +templ Index() { @PublicLayout() {

Reserver votre sejour des maintenant

-
-
- - - -
-
- - - -
- - -
+ @RequestBookingForm(nil)
} diff --git a/internal/view/index_templ.go b/internal/view/index_templ.go index 5494e0e..5f00bbf 100644 --- a/internal/view/index_templ.go +++ b/internal/view/index_templ.go @@ -10,21 +10,31 @@ import "context" import "io" import "bytes" -import ( - "github.com/rjNemo/rentease/config" -) +import "github.com/rjNemo/rentease/config" -type HomePageViewModel struct { - Name string - Address string - ZipCode string - City string - PhoneNumber string - Email string - Items []config.HostItem +func makeItems() []string { + host := config.NewHost() + items := make([]string, 0, 2) + for _, i := range host.Items { + if i.Name == "T2" || i.Name == "T3" { + items = append(items, i.Name) + } + } + return items } -func Index(host *HomePageViewModel) templ.Component { +var items = makeItems() + +func invalidTimeRange(errs []error) bool { + for _, e := range errs { + if e.Error() == "invalid_time_range" { + return true + } + } + return false +} + +func RequestBookingForm(errors []error) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) if !templ_7745c5c3_IsBuffer { @@ -37,53 +47,100 @@ func Index(host *HomePageViewModel) templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Var2 := templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) - if !templ_7745c5c3_IsBuffer { - templ_7745c5c3_Buffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) - } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 1) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - for _, i := range host.Items { - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 2) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(i.Name)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 3) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - var templ_7745c5c3_Var3 string - templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(i.Name) - if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/index.templ`, Line: 27, Col: 42} - } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 4) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 5) - if templ_7745c5c3_Err != nil { - return templ_7745c5c3_Err - } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = io.Copy(templ_7745c5c3_W, templ_7745c5c3_Buffer) - } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) + } + return templ_7745c5c3_Err + }) +} + +func Index() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var3 := templ.GetChildren(ctx) + if templ_7745c5c3_Var3 == nil { + templ_7745c5c3_Var3 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + templ_7745c5c3_Var4 := templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) + if !templ_7745c5c3_IsBuffer { + templ_7745c5c3_Buffer = templ.GetBuffer() + defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Reserver votre sejour des maintenant

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = RequestBookingForm(nil).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if !templ_7745c5c3_IsBuffer { + _, templ_7745c5c3_Err = io.Copy(templ_7745c5c3_W, templ_7745c5c3_Buffer) + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = PublicLayout().Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/internal/view/public.templ b/internal/view/public.templ index da12b3d..6b9858e 100644 --- a/internal/view/public.templ +++ b/internal/view/public.templ @@ -72,7 +72,9 @@ templ PublicLayout() {
{ children... }
- + } diff --git a/internal/view/public_templ.go b/internal/view/public_templ.go index 18f739d..6ac4fb2 100644 --- a/internal/view/public_templ.go +++ b/internal/view/public_templ.go @@ -59,7 +59,7 @@ func PublicLayout() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 1) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -72,12 +72,12 @@ func PublicLayout() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 2) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" | Locations de vacances au Gosier en Guadeloupe
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -112,20 +112,20 @@ func PublicLayout() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 7) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }