mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-20 06:05:08 +01:00
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/getAlby/lndhub.go/db/models"
|
|
"github.com/getAlby/lndhub.go/lib"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// PayInvoiceController : Pay invoice controller struct
|
|
type PayInvoiceController struct{}
|
|
|
|
// PayInvoice : Pay invoice Controller
|
|
func (PayInvoiceController) PayInvoice(c echo.Context) error {
|
|
ctx := c.(*lib.LndhubContext)
|
|
var reqBody struct {
|
|
Invoice string `json:"invoice" validate:"required"`
|
|
Amount int `json:"amount" validate:"omitempty,gte=0"`
|
|
}
|
|
|
|
if err := c.Bind(&reqBody); err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{
|
|
"message": "failed to bind json",
|
|
})
|
|
}
|
|
|
|
if err := c.Validate(&reqBody); err != nil {
|
|
return c.JSON(http.StatusBadRequest, echo.Map{
|
|
"message": "invalid request",
|
|
})
|
|
}
|
|
|
|
db := ctx.DB
|
|
debitAccount := models.Account{}
|
|
creditAccount := models.Account{}
|
|
if err := db.NewSelect().Model(&debitAccount).Where("user_id = ? AND type= ?", ctx.User.ID, "current").Limit(1).Scan(context.TODO()); err != nil {
|
|
return err
|
|
}
|
|
if err := db.NewSelect().Model(&creditAccount).Where("user_id = ? AND type= ?", ctx.User.ID, "outgoing").Limit(1).Scan(context.TODO()); err != nil {
|
|
return err
|
|
}
|
|
entry := models.TransactionEntry{
|
|
UserID: ctx.User.ID,
|
|
CreditAccountID: creditAccount.ID,
|
|
DebitAccountID: debitAccount.ID,
|
|
Amount: 1000,
|
|
}
|
|
if _, err := db.NewInsert().Model(&entry).Exec(context.TODO()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|