mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-11 05:06:52 +00:00
83 lines
2 KiB
Go
83 lines
2 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/gommon/log"
|
|
|
|
"github.com/rjNemo/rentease/constants"
|
|
"github.com/rjNemo/rentease/internal/domains/booking"
|
|
"github.com/rjNemo/rentease/internal/views"
|
|
)
|
|
|
|
func (s Server) handleHomePage() echo.HandlerFunc {
|
|
return func(ctx echo.Context) error {
|
|
component := views.Index()
|
|
return s.renderTempl(ctx, http.StatusOK, component)
|
|
}
|
|
}
|
|
|
|
func (s Server) handleNewBookingPage() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
component := views.NewBooking(constants.Platforms)
|
|
return s.renderTempl(c, http.StatusOK, component)
|
|
}
|
|
}
|
|
|
|
func (s Server) handleCreateBooking() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
type NewBooking struct {
|
|
Name string `form:"name"`
|
|
PhoneNumber string `form:"phone_number"`
|
|
CustomerNumber string `form:"customer_number"`
|
|
Email string `form:"email"`
|
|
From time.Time `json:"from"`
|
|
To time.Time `from:"to"`
|
|
Platform string `form:"platform"`
|
|
PlatformFees string `form:"platform_fees"`
|
|
}
|
|
nb := new(NewBooking)
|
|
err := c.Bind(nb)
|
|
if err != nil {
|
|
log.Warn(err)
|
|
return err
|
|
}
|
|
|
|
ts, err := parseTime(c.FormValue("from"))
|
|
nb.From = ts
|
|
ts, err = parseTime(c.FormValue("to"))
|
|
nb.To = ts
|
|
|
|
b := &booking.Booking{
|
|
Name: nb.Name,
|
|
PhoneNumber: nb.PhoneNumber,
|
|
CustomerNumber: nb.CustomerNumber,
|
|
Email: nb.Email,
|
|
From: nb.From,
|
|
To: nb.To,
|
|
Platform: nb.Platform,
|
|
PlatformFees: nb.PlatformFees,
|
|
}
|
|
_ = s.db.Create(b)
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("%s/%d", constants.RouteBooking, b.Id))
|
|
}
|
|
}
|
|
|
|
func (s Server) handleBookingPage() echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
component := views.BookingById()
|
|
return s.renderTempl(c, http.StatusOK, component)
|
|
|
|
}
|
|
}
|
|
|
|
func parseTime(src string) (time.Time, error) {
|
|
ts, err := time.Parse("2006-01-02", src)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
return ts, nil
|
|
}
|