diff --git a/internal/booking/service.go b/internal/booking/service.go index 95ba3c6..7f8f368 100644 --- a/internal/booking/service.go +++ b/internal/booking/service.go @@ -97,7 +97,7 @@ func (bs Service) Update(id int, From time.Time, To time.Time, Name string, Phon return b } -func (bs Service) CreateItem(bookingId int, item config.HostItem, quantity int, paymentMethod string, customerNumber int) *Item { +func (bs Service) CreateItem(bookingId int, item config.HostItem, quantity int, paymentMethod string, customerNumber int) (items []*Item) { i := &Item{ BookingId: bookingId, Item: item.Name, @@ -106,7 +106,7 @@ func (bs Service) CreateItem(bookingId int, item config.HostItem, quantity int, PaymentMethod: paymentMethod, } _ = bs.db.Create(i) - + items = append(items, i) if item.Taxes != 0.0 { ti := &Item{ BookingId: bookingId, @@ -116,9 +116,10 @@ func (bs Service) CreateItem(bookingId int, item config.HostItem, quantity int, PaymentMethod: "Cash", } _ = bs.db.Create(ti) + items = append(items, ti) } - return i + return items } func (bs Service) PayItem(id int) *Item { diff --git a/internal/server/handle_bookings.go b/internal/server/handle_bookings.go index 9d49ad2..13f2f82 100644 --- a/internal/server/handle_bookings.go +++ b/internal/server/handle_bookings.go @@ -257,7 +257,7 @@ func handleCreateItem(bs *booking.Service, cs *calendar.Service, hc *config.Host return fmt.Errorf("invalid item name %q", ni.Item) } - i := bs.CreateItem(b.Id, itm, ni.Quantity, ni.PaymentMethod, b.CustomerNumber) + newItems := bs.CreateItem(b.Id, itm, ni.Quantity, ni.PaymentMethod, b.CustomerNumber) if err = cs.Create( itm.CalendarId, @@ -269,16 +269,20 @@ func handleCreateItem(bs *booking.Service, cs *calendar.Service, hc *config.Host captureError(c, err) } - return renderTempl(c, http.StatusCreated, view.LineItem(&view.ItemViewModel{ - Id: strconv.Itoa(i.Id), - Item: i.Item, - Quantity: strconv.Itoa(i.Quantity), - Price: strconv.FormatFloat(i.Price, 'f', 2, 64), - PaymentMethod: i.PaymentMethod, - PaymentStatus: i.PaymentStatus, - SubTotal: strconv.FormatFloat(i.Price*float64(i.Quantity), 'f', 2, 64), - ItemUrl: fmt.Sprintf("%s/%d", constant.RouteItem, i.Id), - })) + for _, i := range newItems { + renderTempl(c, http.StatusCreated, view.LineItem(&view.ItemViewModel{ + Id: strconv.Itoa(i.Id), + Item: i.Item, + Quantity: strconv.Itoa(i.Quantity), + Price: strconv.FormatFloat(i.Price, 'f', 2, 64), + PaymentMethod: i.PaymentMethod, + PaymentStatus: i.PaymentStatus, + SubTotal: strconv.FormatFloat(i.Price*float64(i.Quantity), 'f', 2, 64), + ItemUrl: fmt.Sprintf("%s/%d", constant.RouteItem, i.Id), + })) + } + + return nil } }