Extract util.go
This commit is contained in:
parent
1a4267186a
commit
53dd31fa35
@ -7,57 +7,8 @@ import (
|
|||||||
|
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getUUID(c *gin.Context, name string) (uuid.UUID, error) {
|
|
||||||
value, succ := c.GetPostForm(name)
|
|
||||||
if !succ {
|
|
||||||
return uuid.UUID{}, fmt.Errorf("not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := uuid.Parse(value)
|
|
||||||
if err != nil {
|
|
||||||
return uuid.UUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getNullUUIDFromParam(c *gin.Context, name string) (uuid.NullUUID, error) {
|
|
||||||
value := c.Param(name)
|
|
||||||
if value == "" {
|
|
||||||
return uuid.NullUUID{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := uuid.Parse(value)
|
|
||||||
if err != nil {
|
|
||||||
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return uuid.NullUUID{
|
|
||||||
UUID: id,
|
|
||||||
Valid: true,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getNullUUIDFromForm(c *gin.Context, name string) (uuid.NullUUID, error) {
|
|
||||||
value, succ := c.GetPostForm(name)
|
|
||||||
if !succ || value == "" {
|
|
||||||
return uuid.NullUUID{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := uuid.Parse(value)
|
|
||||||
if err != nil {
|
|
||||||
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return uuid.NullUUID{
|
|
||||||
UUID: id,
|
|
||||||
Valid: true,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) newTransaction(c *gin.Context) {
|
func (h *Handler) newTransaction(c *gin.Context) {
|
||||||
transactionMemo, _ := c.GetPostForm("memo")
|
transactionMemo, _ := c.GetPostForm("memo")
|
||||||
transactionAccountID, err := getUUID(c, "account_id")
|
transactionAccountID, err := getUUID(c, "account_id")
|
||||||
|
56
http/util.go
Normal file
56
http/util.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getUUID(c *gin.Context, name string) (uuid.UUID, error) {
|
||||||
|
value, succ := c.GetPostForm(name)
|
||||||
|
if !succ {
|
||||||
|
return uuid.UUID{}, fmt.Errorf("not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := uuid.Parse(value)
|
||||||
|
if err != nil {
|
||||||
|
return uuid.UUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNullUUIDFromParam(c *gin.Context, name string) (uuid.NullUUID, error) {
|
||||||
|
value := c.Param(name)
|
||||||
|
if value == "" {
|
||||||
|
return uuid.NullUUID{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := uuid.Parse(value)
|
||||||
|
if err != nil {
|
||||||
|
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uuid.NullUUID{
|
||||||
|
UUID: id,
|
||||||
|
Valid: true,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNullUUIDFromForm(c *gin.Context, name string) (uuid.NullUUID, error) {
|
||||||
|
value, succ := c.GetPostForm(name)
|
||||||
|
if !succ || value == "" {
|
||||||
|
return uuid.NullUUID{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := uuid.Parse(value)
|
||||||
|
if err != nil {
|
||||||
|
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uuid.NullUUID{
|
||||||
|
UUID: id,
|
||||||
|
Valid: true,
|
||||||
|
}, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user