validate time range

This commit is contained in:
Ruidy 2024-03-10 15:30:49 +01:00
parent 430ec69ae6
commit 3cbddfa47d
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
6 changed files with 235 additions and 137 deletions

View file

@ -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))
}
}

View file

@ -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))

View file

@ -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) {
<form id="booking-request-form" hx-post="/request-booking">
<fieldset class="grid">
<label for="item">
Logement
<select name="item" id="item" required>
for _,i := range items {
<option value={ i }>{ i }</option>
}
</select>
</label>
<label for="from">
Du
<input type="date" id="from" name="from" required/>
</label>
<label for="to">
Au
if (invalidTimeRange(errors)) {
<input type="date" id="to" name="to" required aria-invalid="true"/>
<small>La date de depart doit etre apres celle d'arrivee</small>
} else {
<input type="date" id="to" name="to" required/>
}
</label>
</fieldset>
<fieldset class="grid">
<label for="name">
Nom *
<input type="text" id="name" name="name" required/>
</label>
<label for="phone">
Telephone
<input type="tel" id="phone" name="phone"/>
</label>
<label for="email">
Email
<input type="email" id="email" name="email"/>
</label>
</fieldset>
<label for="message">
Message
<textarea name="message" id="message"></textarea>
</label>
<button type="submit">Book</button>
</form>
}
templ Index() {
@PublicLayout() {
<section>
<h1>Reserver votre sejour des maintenant</h1>
<article>
<form>
<fieldset class="grid">
<label for="item">
Logement
<select name="item" id="item" required>
for _,i := range host.Items {
<option value={ i.Name }>{ i.Name }</option>
}
</select>
</label>
<label for="from">
Du
<input type="date" id="from" name="from" required/>
</label>
<label for="to">
Au
<input type="date" id="to" name="to" required/>
</label>
</fieldset>
<fieldset class="grid">
<label for="name">
Nom *
<input type="text" id="name" name="name" required/>
</label>
<label for="phone">
Telephone
<input type="tel" id="phone" name="phone"/>
</label>
<label for="email">
Email
<input type="email" id="email" name="email"/>
</label>
</fieldset>
<label for="message">
Message
<textarea name="message" id="message"></textarea>
</label>
<button type="submit">Book</button>
</form>
@RequestBookingForm(nil)
</article>
</section>
}

View file

@ -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("<form id=\"booking-request-form\" hx-post=\"/request-booking\"><fieldset class=\"grid\"><label for=\"item\">Logement <select name=\"item\" id=\"item\" required>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
})
templ_7745c5c3_Err = PublicLayout().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer)
}
for _, i := range items {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<option value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(i))
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
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(i)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/index.templ`, Line: 33, Col: 29}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</option>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</select></label> <label for=\"from\">Du <input type=\"date\" id=\"from\" name=\"from\" required></label> <label for=\"to\">Au ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if invalidTimeRange(errors) {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"date\" id=\"to\" name=\"to\" required aria-invalid=\"true\"> <small>La date de depart doit etre apres celle d'arrivee</small>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<input type=\"date\" id=\"to\" name=\"to\" required>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</label></fieldset><fieldset class=\"grid\"><label for=\"name\">Nom * <input type=\"text\" id=\"name\" name=\"name\" required></label> <label for=\"phone\">Telephone <input type=\"tel\" id=\"phone\" name=\"phone\"></label> <label for=\"email\">Email <input type=\"email\" id=\"email\" name=\"email\"></label></fieldset><label for=\"message\">Message <textarea name=\"message\" id=\"message\"></textarea></label> <button type=\"submit\">Book</button></form>")
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("<section><h1>Reserver votre sejour des maintenant</h1><article>")
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("</article></section>")
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
}

View file

@ -72,7 +72,9 @@ templ PublicLayout() {
<main class="container">
{ children... }
</main>
<footer class="container-fluid">{ hvm.Name } &copy 2024 Telephone : <a href={ hvm.PhoneUrl }>{ hvm.PhoneNumber }</a> Email : <a href={ hvm.EmailUrl }>{ hvm.Email }</a> </footer>
<footer class="container">
<small>{ hvm.Name } &copy 2024 Telephone : <a href={ hvm.PhoneUrl }>{ hvm.PhoneNumber }</a> Email : <a href={ hvm.EmailUrl }>{ hvm.Email }</a> </small>
</footer>
</body>
</html>
}

View file

@ -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("<!doctype html><html lang=\"fr\" data-theme=\"light\"><head><title>")
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</title><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta name=\"description\" content=\"Locations de vacances au Gosier en Guadeloupe\"><link rel=\"icon\" href=\"/static/img/favicon.svg\"><link rel=\"stylesheet\" href=\"/static/css/pico.min.css\"><script src=\"/static/js/htmx.js\" defer></script></head><body hx-boost=\"true\"><nav class=\"container-fluid\"><ul><li><a href=\"/\"><img src=\"/static/img/logo.png\" alt=\"logo de villafleurie\" width=\"50px\"></a></li></ul><ul><li><details class=\"dropdown\"><summary>Logements</summary><ul dir=\"rtl\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, l := range hvm.Items {
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 3)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<li><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -86,7 +86,7 @@ func PublicLayout() templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 4)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -99,12 +99,12 @@ func PublicLayout() templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 5)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a></li>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 6)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</ul></details></li><li><a href=\"/book\" role=\"button\">Book Now</a></li></ul></nav><main class=\"container\">")
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("</main><footer class=\"container\"><small>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var5 string
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(hvm.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/public.templ`, Line: 74, Col: 45}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/public.templ`, Line: 75, Col: 21}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 8)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" &copy 2024 Telephone : <a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -134,20 +134,20 @@ func PublicLayout() templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 9)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var7 string
templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(hvm.PhoneNumber)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/public.templ`, Line: 74, Col: 117}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/public.templ`, Line: 75, Col: 93}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 10)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a> Email : <a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -156,20 +156,20 @@ func PublicLayout() templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 11)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var9 string
templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(hvm.Email)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/public.templ`, Line: 74, Col: 172}
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/view/public.templ`, Line: 75, Col: 148}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = templ.WriteWatchModeString(templ_7745c5c3_Buffer, 12)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a></small></footer></body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}