Added a button to quickly pay off a debt
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
<th>Debtor</th>
|
||||
<th>Creditor</th>
|
||||
<th>Sum</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
{{ range $exp.Debts }}
|
||||
{{ $debt := . }}
|
||||
@@ -25,6 +26,13 @@
|
||||
<td>{{ $debt.Debtor.UserName }}</td>
|
||||
<td>{{ $debt.Creditor.UserName }}</td>
|
||||
<td>{{ formatPrice $debt.Sum }}</td>
|
||||
<td>
|
||||
<a hx-post="/payments/add"
|
||||
hx-swap="none"
|
||||
hx-vals='{"ExpID":"{{ $exp.ExpID }}",
|
||||
"UserID":"{{ $debt.Debtor.UserID }}",
|
||||
"Sum": "{{ $debt.Sum }}"}'>PAY</a>
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
|
||||
@@ -2,8 +2,7 @@ package expenses
|
||||
|
||||
import (
|
||||
mod "expenses/expenses/models"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
dec "github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
// TODO
|
||||
@@ -19,7 +18,7 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payed, err := decimal.NewFromString(values[k])
|
||||
payed, err := dec.NewFromString(values[k])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -46,7 +45,7 @@ func ParseFormPayments(userIDs []string, paymentsIDs []string,
|
||||
return payments, nil
|
||||
}
|
||||
|
||||
// insertPayment allows us to insert a share manually
|
||||
// insertPayment allows us to insert a payment manually
|
||||
// for now its private, need to figure out if it needs to be public
|
||||
func (a *ExpensesApp) insertPayment(payment mod.ExpensePayment, eIdx int64) error {
|
||||
tx, err := a.DB.Begin()
|
||||
@@ -81,3 +80,19 @@ func (a *ExpensesApp) DeletePayment(id int64) error {
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (a *ExpensesApp) AddPayment(expID int64, userID int64, sum dec.Decimal) error {
|
||||
payment := mod.ExpensePayment{
|
||||
User: mod.User{
|
||||
UserID: userID,
|
||||
},
|
||||
PayedAmount: sum,
|
||||
}
|
||||
|
||||
err := a.insertPayment(payment, expID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
+22
-2
@@ -4,9 +4,9 @@ import (
|
||||
exp "expenses/expenses"
|
||||
experr "expenses/expenses/errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
dec "github.com/shopspring/decimal"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func deletePayment(c *gin.Context) {
|
||||
@@ -32,6 +32,26 @@ func deletePayment(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func addPayment(c *gin.Context) {
|
||||
expID, err := exp.ParseID(c.PostForm("ExpID"))
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
userID, err := exp.ParseID(c.PostForm("UserID"))
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
sum, err := dec.NewFromString(c.PostForm("Sum"))
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
err = exp.App.AddPayment(expID, userID, sum)
|
||||
if err != nil {
|
||||
}
|
||||
}
|
||||
|
||||
func RoutePayments(router *gin.Engine) {
|
||||
router.DELETE("/payments/:id", deletePayment)
|
||||
|
||||
router.POST("/payments/add", addPayment)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user