package server import ( "fmt" "net/http" "strconv" "github.com/labstack/echo/v4" u "github.com/rjNemo/underscore" "github.com/rjNemo/rentease/internal/constant" "github.com/rjNemo/rentease/internal/service/booking" "github.com/rjNemo/rentease/internal/view" ) func handleCreatePayment(bs *booking.Service) echo.HandlerFunc { type CreatePaymentInput struct { Amount float64 `form:"amount"` PaymentMethod string `form:"paymentMethod"` } return func(c echo.Context) error { idStr := c.Param("id") id, err := strconv.Atoi(idStr) if err != nil { return err } np := new(CreatePaymentInput) c.Bind(np) b := bs.One(id) _, err = bs.CreatePayment(b.Id, np.Amount, np.PaymentMethod) if err != nil { return err } nb := bs.One(id) return renderTempl(c, http.StatusOK, view.ItemList(view.ItemListViewModel{ Items: u.Map(nb.Items, func(i booking.Item) view.ItemViewModel { return view.ItemViewModel{ Id: strconv.Itoa(i.Id), 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), PaymentStatus: i.PaymentStatus, ItemUrl: fmt.Sprintf("%s/%d", constant.RouteItem, i.Id), Payments: u.Map(b.Payments, func(p booking.Payment) view.Payment { return view.Payment{ Amount: strconv.FormatFloat(p.Amount, 'f', 2, 64), PaymentMethod: p.PaymentMethod, PaymentUrl: fmt.Sprintf("%s/%d", constant.RoutePayment, p.ID), } }), } }), })) } } type Payment struct { BookingID int Amount float64 PaymentMethod string }