From f24ca85b1c0604cc1263d5254d50777ccf70209f Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 12 Jan 2022 17:42:19 +0200 Subject: [PATCH 1/4] Update notes.md --- notes.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/notes.md b/notes.md index 0248735..cdeb6b8 100644 --- a/notes.md +++ b/notes.md @@ -71,6 +71,18 @@ Get all transactions + created_at (datetime) ``` +#### tokens + +``` ++ id (primary key) ++ user_id (foreign_key) ++ name (string - optional name of the application) ++ access_token (string auto generated on /auth call) ++ refresh_token (string auto generated on /auth call) ++ expires_at (datetime / created_at + 2 weeks) ++ created_at (datetime) + + #### invoices ``` From d3672520d2c1cdb5f6113f8f5200247eb4ab3bb6 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 12 Jan 2022 17:42:42 +0200 Subject: [PATCH 2/4] Update notes.md --- notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notes.md b/notes.md index cdeb6b8..af3c69f 100644 --- a/notes.md +++ b/notes.md @@ -81,7 +81,7 @@ Get all transactions + refresh_token (string auto generated on /auth call) + expires_at (datetime / created_at + 2 weeks) + created_at (datetime) - +``` #### invoices From 324aa78d3e0a3eecdde4a9ae557de912cc98e650 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Wed, 12 Jan 2022 18:16:31 +0100 Subject: [PATCH 3/4] Add payinvoice endpoint --- cmd/main.go | 1 + pkg/controllers/payinvoice.ctrl.go | 32 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 pkg/controllers/payinvoice.ctrl.go diff --git a/cmd/main.go b/cmd/main.go index 4b671f5..e09da84 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -48,6 +48,7 @@ func main() { e.POST("/auth", controllers.AuthController{}.Auth) e.POST("/create", controllers.CreateUserController{}.CreateUser) e.POST("/addinvoice", controllers.AddInvoiceController{}.AddInvoice, middleware.JWT([]byte("secret"))) + e.POST("/payinvoice", controllers.PayInvoiceController{}.PayInvoice, middleware.JWT([]byte("secret"))) // Start server go func() { diff --git a/pkg/controllers/payinvoice.ctrl.go b/pkg/controllers/payinvoice.ctrl.go new file mode 100644 index 0000000..169c881 --- /dev/null +++ b/pkg/controllers/payinvoice.ctrl.go @@ -0,0 +1,32 @@ +package controllers + +import ( + "github.com/labstack/echo/v4" + "net/http" +) + +// PayInvoiceController : Pay invoice controller struct +type PayInvoiceController struct{} + +// PayInvoice : Pay invoice Controller +func (PayInvoiceController) PayInvoice(c echo.Context) error { + var body struct { + ID uint `json:"id"` + Invoice string `json:"invoice" validate:"required"` + Amount int `json:"amount" validate:"gt=0"` + } + + if err := c.Bind(&body); err != nil { + return c.JSON(http.StatusBadRequest, echo.Map{ + "message": "failed to bind json", + }) + } + + if err := c.Validate(&body); err != nil { + return c.JSON(http.StatusBadRequest, echo.Map{ + "message": "invalid request", + }) + } + + return nil +} From 182873b28675dc45dfa393e34a45b83f8e92d200 Mon Sep 17 00:00:00 2001 From: Viktor Patchev Date: Thu, 13 Jan 2022 10:08:47 +0100 Subject: [PATCH 4/4] Fix request body --- pkg/controllers/payinvoice.ctrl.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/controllers/payinvoice.ctrl.go b/pkg/controllers/payinvoice.ctrl.go index 169c881..bc3508c 100644 --- a/pkg/controllers/payinvoice.ctrl.go +++ b/pkg/controllers/payinvoice.ctrl.go @@ -10,19 +10,18 @@ type PayInvoiceController struct{} // PayInvoice : Pay invoice Controller func (PayInvoiceController) PayInvoice(c echo.Context) error { - var body struct { - ID uint `json:"id"` + var reqBody struct { Invoice string `json:"invoice" validate:"required"` Amount int `json:"amount" validate:"gt=0"` } - if err := c.Bind(&body); err != nil { + if err := c.Bind(&reqBody); err != nil { return c.JSON(http.StatusBadRequest, echo.Map{ "message": "failed to bind json", }) } - if err := c.Validate(&body); err != nil { + if err := c.Validate(&reqBody); err != nil { return c.JSON(http.StatusBadRequest, echo.Map{ "message": "invalid request", })