rentease/internal/server/handler_booking.go
2024-02-16 18:08:24 +01:00

148 lines
4.3 KiB
Go

package server
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/a-h/templ"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
u "github.com/rjNemo/underscore"
"github.com/rjNemo/rentease/constants"
"github.com/rjNemo/rentease/internal/booking"
"github.com/rjNemo/rentease/internal/views"
myTime "github.com/rjNemo/rentease/pkg/time"
)
func handleListBookingPage(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
bookings := bs.All()
bvm := u.Map(bookings, func(b *booking.Booking) *views.ListBookingsViewModel {
return &views.ListBookingsViewModel{
Id: strconv.Itoa(b.Id),
Url: templ.SafeURL(fmt.Sprintf("%s/%d", constants.RouteBooking, b.Id)),
From: b.From.Format("2006-01-02"),
To: b.To.Format("2006-01-02"),
Platform: b.Platform,
Name: b.Name,
}
})
component := views.ListBookings(bvm)
return renderTempl(c, http.StatusOK, component)
}
}
func handleNewBookingPage() echo.HandlerFunc {
return func(c echo.Context) error {
return renderTempl(c, http.StatusOK, views.NewBooking(constants.Platforms))
}
}
func handleCreateBooking(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
type NewBooking struct {
From time.Time `json:"from"`
To time.Time `json:"to"`
Name string `form:"name"`
PhoneNumber string `form:"phone_number"`
Email string `form:"email"`
Platform string `form:"platform"`
CustomerNumber int `form:"customer_number"`
PlatformFees float64 `form:"platform_fees"`
}
nb := new(NewBooking)
err := c.Bind(nb)
if err != nil {
log.Warn(err)
return err
}
ts, _ := myTime.ParseFromForm(c.FormValue("from"))
nb.From = ts
ts, _ = myTime.ParseFromForm(c.FormValue("to"))
nb.To = ts
b := bs.Create(nb.From, nb.To, nb.Name, nb.PhoneNumber, nb.Email, nb.Platform, nb.CustomerNumber, nb.PlatformFees)
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s/%d", constants.RouteBooking, b.Id))
}
}
func handleBookingPage(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
return err
}
b := &booking.Booking{Id: id}
bs.db.Preload("Items").First(b)
bvm := &views.BookingViewModel{
Id: fmt.Sprintf("%04s", strconv.Itoa(b.Id)),
Name: b.Name,
PhoneNumber: b.PhoneNumber,
CustomerNumber: strconv.Itoa(b.CustomerNumber),
Email: b.Email,
From: b.From.Format("2006-01-02"),
To: b.To.Format("2006-01-02"),
Platform: b.Platform,
PlatformFees: strconv.FormatFloat(b.PlatformFees, 'f', 2, 64),
Url: templ.EscapeString(fmt.Sprintf("%s/%d/items", constants.RouteBooking, b.Id)),
Items: u.Map(b.Items, func(i booking.Item) views.ItemViewModel {
return views.ItemViewModel{
Item: i.Item,
Quantity: strconv.Itoa(i.Quantity),
Price: strconv.FormatFloat(i.Price, 'f', 2, 64),
SubTotal: strconv.FormatFloat(i.Price*float64(i.Quantity), 'f', 2, 64),
PaymentMethod: i.PaymentMethod,
PaymentStatus: i.PaymentStatus,
}
}),
Total: strconv.FormatFloat(u.Reduce(b.Items, func(i booking.Item, sum float64) float64 {
return sum + i.Price*float64(i.Quantity)
}, 0.0), 'f', 2, 64),
Platforms: constants.Platforms,
ItemList: constants.Items,
PaymentMethods: constants.PaymentMethods,
}
component := views.BookingById(bvm)
return s.renderTempl(c, http.StatusOK, component)
}
}
func (s Server) handleCreateItem() echo.HandlerFunc {
return func(c echo.Context) error {
bookingIdStr := c.Param("id")
bid, err := strconv.Atoi(bookingIdStr)
if err != nil {
return err
}
type NewItem struct {
Item string `form:"item"`
PaymentMethod string `form:"method"`
Quantity int `form:"quantity"`
Price float64 `form:"price"`
}
ni := new(NewItem)
if err := c.Bind(ni); err != nil {
log.Warn(err)
return err
}
i := &booking.Item{
BookingId: bid,
Item: ni.Item,
Quantity: ni.Quantity,
Price: ni.Price,
PaymentMethod: ni.PaymentMethod,
}
_ = s.db.Create(i)
return s.renderTempl(c, http.StatusCreated, views.LineItem(i))
}
}