update line item

This commit is contained in:
Ruidy 2024-03-22 21:45:57 +01:00
parent b89a760aa3
commit 28866ac5fb
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
5 changed files with 59 additions and 14 deletions

View file

@ -98,6 +98,18 @@ func (bs Service) OneItem(id int) *Item {
return i
}
func (bs Service) UpdateItem(id int, item string, qty int, price float64, paymentMethod, paymentStatus string) *Item {
i := new(Item)
bs.db.Model(i).Clauses(clause.Returning{}).Where("id = ?", id).Updates(map[string]any{
"item": item,
"payment_method": paymentMethod,
"payment_status": paymentStatus,
"quantity": qty,
"price": price,
})
return i
}
type Report struct {
Lines []*Line
Total float64

View file

@ -243,3 +243,35 @@ func handlePayItem(bs *booking.Service) echo.HandlerFunc {
}))
}
}
func handleUpdateItem(bs *booking.Service) echo.HandlerFunc {
return func(c echo.Context) error {
type updateItem struct {
Item string `form:"item"`
PaymentMethod string `form:"paymentMethod"`
PaymentStatus string `form:"paymentStatus"`
Id int `param:"id"`
Quantity int `form:"quantity"`
Price float64 `form:"price"`
}
ui := new(updateItem)
if err := c.Bind(ui); err != nil {
log.Warn(err)
return err
}
i := bs.UpdateItem(ui.Id, ui.Item, ui.Quantity, ui.Price, ui.PaymentMethod, ui.PaymentStatus)
return renderTempl(c, http.StatusCreated, view.LineItem(&view.ItemViewModel{
Id: strconv.Itoa(ui.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),
}))
}
}

View file

@ -12,6 +12,7 @@ func (s Server) MountHandlers() {
s.Router.PUT("/bookings/:id", handleUpdateBooking(s.bs, s.hc))
s.Router.POST("/bookings/:id/items", handleCreateItem(s.bs))
s.Router.POST("/items/:id", handlePayItem(s.bs))
s.Router.PUT("/items/:id", handleUpdateItem(s.bs))
s.Router.GET("/items/:id", handleLineItemForm(s.bs))
s.Router.GET("/bookings/pdf/:id", handleCreateInvoicePdf(s.bs, s.ps, s.hc))
s.Router.GET("/reports", handleReportsPage())

View file

@ -19,15 +19,15 @@ templ LineItem(item *ItemViewModel) {
templ LineItemForm(item *ItemViewModel) {
<tr>
<form hx-put={ item.ItemUrl }>
<td><input value={ item.Item } name="item"/></td>
<td><input value={ item.Quantity } name="quantity"/></td>
<td><input value={ item.Price } name="price"/></td>
<td><input value={ item.PaymentMethod } name="paymentMethod"/></td>
<td><input value={ item.PaymentStatus } name="PaymentStatus"/></td>
<form hx-put={ item.ItemUrl } id="edit-item" hx-target="closest tr" hx-swap="outerHTML">
<td><input value={ item.Item } name="item" form="edit-item"/></td>
<td><input value={ item.Quantity } name="quantity" form="edit-item"/></td>
<td><input value={ item.Price } name="price" form="edit-item"/></td>
<td><input value={ item.PaymentMethod } name="paymentMethod" form="edit-item"/></td>
<td><input value={ item.PaymentStatus } name="PaymentStatus" form="edit-item"/></td>
<td>{ item.SubTotal }</td>
<td>
<button class="secondary">Save</button>
<button class="secondary" type="submit" form="edit-item">Save</button>
</td>
</form>
</tr>

View file

@ -159,7 +159,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\"><td><input value=\"")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" id=\"edit-item\" hx-target=\"closest tr\" hx-swap=\"outerHTML\"><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -167,7 +167,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"item\"></td><td><input value=\"")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"item\" form=\"edit-item\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -175,7 +175,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"quantity\"></td><td><input value=\"")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"quantity\" form=\"edit-item\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -183,7 +183,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"price\"></td><td><input value=\"")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"price\" form=\"edit-item\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -191,7 +191,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"paymentMethod\"></td><td><input value=\"")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"paymentMethod\" form=\"edit-item\"></td><td><input value=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -199,7 +199,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"PaymentStatus\"></td><td>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" name=\"PaymentStatus\" form=\"edit-item\"></td><td>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@ -212,7 +212,7 @@ func LineItemForm(item *ItemViewModel) templ.Component {
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td><button class=\"secondary\">Save</button></td></form></tr>")
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</td><td><button class=\"secondary\" type=\"submit\" form=\"edit-item\">Save</button></td></form></tr>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}