rentease/internal/server/handle_public.go
2024-03-10 15:30:49 +01:00

41 lines
968 B
Go

package server
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/rjNemo/rentease/internal/view"
myTime "github.com/rjNemo/rentease/pkg/time"
)
func handleHomePage() echo.HandlerFunc {
return func(ctx echo.Context) error {
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))
}
}