rentease/internal/server/handle_public.go

46 lines
1.1 KiB
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"))
}
phoneNumber := c.FormValue("phone")
email := c.FormValue("email")
if phoneNumber == "" && email == "" {
errs = append(errs, errors.New("missing_communication_method"))
}
return renderTempl(c, http.StatusOK, view.RequestBookingForm(errs))
}
}