rentease/internal/server/handle_public.go
2024-03-10 20:34:29 +01:00

61 lines
1.5 KiB
Go

package server
import (
"fmt"
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/rjNemo/rentease/internal/booking"
"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(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
itemStr := c.FormValue("item")
fromStr := c.FormValue("from")
toStr := c.FormValue("to")
name := c.FormValue("item")
phoneNumber := c.FormValue("phone")
email := c.FormValue("email")
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([]string, 0)
if to.Sub(from) < time.Duration(0) {
errs = append(errs, "invalid_time_range")
}
if phoneNumber == "" && email == "" {
errs = append(errs, "missing_communication_method")
}
if len(errs) > 0 {
return renderTempl(c, http.StatusOK, view.RequestBookingForm(&view.RequestBookingViewModel{
Item: itemStr,
From: fromStr,
To: toStr,
Name: name,
PhoneNumber: phoneNumber,
Email: email,
Errors: errs,
}))
}
bs.CreateRequest(from, to, name, phoneNumber, email, itemStr, 1)
return renderTempl(c, http.StatusSeeOther, view.BaseLayout())
}
}