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