create item in service

This commit is contained in:
Ruidy 2024-02-16 18:20:15 +01:00
parent 92f8085537
commit 3dd36da114
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 16 additions and 15 deletions

View file

@ -43,6 +43,18 @@ func (bs Service) One(id int) *Booking {
return b
}
func (bs Service) CreateItem(bid int, item string, qty int, price float64, method string) *Item {
i := &Item{
BookingId: bid,
Item: item,
Quantity: qty,
Price: price,
PaymentMethod: method,
}
_ = bs.db.Create(i)
return i
}
type Line struct {
From time.Time
To time.Time

View file

@ -115,7 +115,7 @@ func handleBookingPage(bs *booking.Service) echo.HandlerFunc {
}
}
func (s Server) handleCreateItem() echo.HandlerFunc {
func handleCreateItem(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
bookingIdStr := c.Param("id")
bid, err := strconv.Atoi(bookingIdStr)
@ -134,15 +134,7 @@ func (s Server) handleCreateItem() echo.HandlerFunc {
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))
i := bs.CreateItem(bid, ni.Item, ni.Quantity, ni.Price, ni.PaymentMethod)
return renderTempl(c, http.StatusCreated, views.LineItem(i))
}
}

View file

@ -1,13 +1,10 @@
package server
import (
"fmt"
"os"
"strings"
"github.com/labstack/echo/v4/middleware"
"github.com/rjNemo/rentease/constants"
)
func (s Server) MountHandlers() {
@ -29,7 +26,7 @@ func (s Server) MountHandlers() {
s.Router.GET("/bookings/new", handleNewBookingPage())
s.Router.POST("/bookings/new", handleCreateBooking(s.bs))
s.Router.GET("/bookings/:id", handleBookingPage(s.bs))
s.Router.POST(fmt.Sprintf("%s/:id/items", constants.RouteBooking), s.handleCreateItem())
s.Router.POST("bookings/:id/items", handleCreateItem(s.bs))
s.Router.GET("/reports", handleReportsPage())
s.Router.GET("/reports/do", handleComputeReport(s.bs))
s.Router.GET("/pdf", handleCreateInvoicePdf(s.ps))