package server import ( "bytes" "encoding/json" "fmt" "net/http" "os" "strconv" "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, _ := parseTime(c.FormValue("from")) nb.From = ts ts, _ = 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 { idStr := c.Param("id") id, err := strconv.Atoi(idStr) if err != nil { return err } b := &booking.Booking{Id: id} s.db.Preload("Items").First(b) component := views.BookingById(b, constants.Items, constants.Platforms, constants.PaymentMethods) 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"` Quantity int `form:"quantity"` Price string `form:"price"` PaymentMethod string `form:"method"` } 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)) } } 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 }