Implement testing endpoint for new transaction
This commit is contained in:
parent
d9c03e231e
commit
d5904a7c4a
@ -1,15 +1,67 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type NewTransactionPayload struct {
|
||||||
|
Date JSONDate `json:"date"`
|
||||||
|
Payee struct {
|
||||||
|
ID uuid.NullUUID
|
||||||
|
Name string
|
||||||
|
} `json:"payee"`
|
||||||
|
Category struct {
|
||||||
|
ID uuid.NullUUID
|
||||||
|
Name string
|
||||||
|
} `json:"category"`
|
||||||
|
Memo string `json:"memo"`
|
||||||
|
Amount string `json:"amount"`
|
||||||
|
BudgetID uuid.UUID `json:"budget_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONDate time.Time
|
||||||
|
|
||||||
|
// Implement Marshaler and Unmarshaler interface
|
||||||
|
func (j *JSONDate) UnmarshalJSON(b []byte) error {
|
||||||
|
s := strings.Trim(string(b), "\"")
|
||||||
|
t, err := time.Parse("2006-01-02", s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*j = JSONDate(t)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (j JSONDate) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(time.Time(j))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maybe a Format function for printing your date
|
||||||
|
func (j JSONDate) Format(s string) string {
|
||||||
|
t := time.Time(j)
|
||||||
|
return t.Format(s)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) newTransaction(c *gin.Context) {
|
func (h *Handler) newTransaction(c *gin.Context) {
|
||||||
|
var payload NewTransactionPayload
|
||||||
|
err := c.BindJSON(&payload)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%v\n", payload)
|
||||||
|
/* c.JSON(http.StatusOK, payload)*/
|
||||||
|
return
|
||||||
|
|
||||||
transactionMemo, _ := c.GetPostForm("memo")
|
transactionMemo, _ := c.GetPostForm("memo")
|
||||||
transactionAccountID, err := getUUID(c, "account_id")
|
transactionAccountID, err := getUUID(c, "account_id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user