diff --git a/internal/booking/service.go b/internal/booking/service.go index 1b27061..e21478c 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -55,6 +55,24 @@ func (bs Service) One(id int) *Booking { return b } +func (bs Service) Update(id int, From time.Time, To time.Time, Name string, PhoneNumber string, Email string, Platform string, + CustomerNumber int, PlatformFees float64, +) *Booking { + b := &Booking{ + Id: id, + Name: Name, + PhoneNumber: PhoneNumber, + CustomerNumber: CustomerNumber, + Email: Email, + From: From, + To: To, + Platform: Platform, + PlatformFees: PlatformFees, + } + bs.db.Save(b) + return b +} + func (bs Service) CreateItem(bid int, item string, qty int, price float64, method string) *Item { i := &Item{ BookingId: bid, diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 7047598..b0e6c58 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -94,7 +94,7 @@ func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { 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", constant.RouteBooking, b.Id)), + Url: templ.EscapeString(fmt.Sprintf("%s/%d", constant.RouteBooking, b.Id)), PdfUrl: templ.SafeURL(fmt.Sprintf("%s/pdf/%d", constant.RouteBooking, b.Id)), Items: u.Map(b.Items, func(i booking.Item) view.ItemViewModel { return view.ItemViewModel{ @@ -118,6 +118,53 @@ func handleBookingPage(bs *booking.Service, hc *config.Host) echo.HandlerFunc { } } +func handleUpdateBooking(bs *booking.Service, hc *config.Host) echo.HandlerFunc { + return func(c echo.Context) error { + type UpdateBooking struct { + From time.Time `json:"from"` + To time.Time `json:"to"` + Id int `param:"id"` + 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(UpdateBooking) + 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.Update(nb.Id, nb.From, nb.To, nb.Name, nb.PhoneNumber, nb.Email, nb.Platform, nb.CustomerNumber, nb.PlatformFees) + + form := view.BookingForm(view.BookingViewModel{ + Id: b.InvoiceNumber(hc), + 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, + Platforms: hc.Platforms, + PlatformFees: strconv.FormatFloat(b.PlatformFees, 'f', 2, 64), + PaymentMethods: hc.PaymentMethods, + Url: templ.EscapeString(fmt.Sprintf("%s/%d", constant.RouteBooking, b.Id)), + PdfUrl: templ.SafeURL(fmt.Sprintf("%s/pdf/%d", constant.RouteBooking, b.Id)), + }) + + return renderTempl(c, http.StatusOK, form) + } +} + func handleCreateItem(bs *booking.Service) echo.HandlerFunc { return func(c echo.Context) error { bookingIdStr := c.Param("id") diff --git a/internal/server/routes.go b/internal/server/routes.go index 3d81869..1c58707 100644 --- a/internal/server/routes.go +++ b/internal/server/routes.go @@ -9,6 +9,7 @@ func (s Server) MountHandlers() { s.Router.GET("/bookings/new", handleNewBookingPage(s.hc)) s.Router.POST("/bookings/new", handleCreateBooking(s.bs)) s.Router.GET("/bookings/:id", handleBookingPage(s.bs, s.hc)) + s.Router.PUT("/bookings/:id", handleUpdateBooking(s.bs, s.hc)) s.Router.POST("/bookings/:id/items", handleCreateItem(s.bs)) s.Router.GET("/bookings/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps, s.hc)) s.Router.GET("/reports", handleReportsPage()) diff --git a/internal/view/booking_by_id.templ b/internal/view/booking_by_id.templ index 0bfcbbb..035b91d 100644 --- a/internal/view/booking_by_id.templ +++ b/internal/view/booking_by_id.templ @@ -1,5 +1,9 @@ package view +import ( + "fmt" +) + type BookingViewModel struct { Id string Name string @@ -42,7 +46,7 @@ templ BookingById(booking *BookingViewModel) {
-
+
Add line - +