stubbed mock shop creation route

This commit is contained in:
2026-02-03 08:28:07 -07:00
parent e74854bf6e
commit a8dda86882
8 changed files with 381 additions and 153 deletions
+50
View File
@@ -0,0 +1,50 @@
package param
import (
"encoding"
"ruben/inventory2/internal/domains/accounts"
"strconv"
)
// encoding.TextUnmarshaler instances for Spec
func Text(dst *string) encoding.TextUnmarshaler {
return (*rawText)(dst)
}
func Int(dst *int) encoding.TextUnmarshaler {
return (*intText)(dst)
}
func Platform(dst *accounts.Platform) encoding.TextUnmarshaler {
return (*platformText)(dst)
}
type (
rawText string
intText int
platformText accounts.Platform
)
func (t *rawText) UnmarshalText(text []byte) error {
*t = rawText(text)
return nil
}
func (n *intText) UnmarshalText(text []byte) error {
i, err := strconv.Atoi(string(text))
if err != nil {
return err
}
*n = intText(i)
return nil
}
func (p *platformText) UnmarshalText(text []byte) error {
v, err := accounts.NewPlatform(string(text))
if err != nil {
return err
}
*p = platformText(v)
return nil
}
+76
View File
@@ -0,0 +1,76 @@
package param
import (
"encoding"
"github.com/gin-gonic/gin"
"ruben/inventory2/internal/server/response"
)
// Spec is the entry point of the package.
// It's typically constructed via Path() or Form().
// But it's zero value is valid.
//
// It's methods support a builder pattern to minimize API bloat.
//
// To complete gin parameter parsing, call Unmarshal().
type Spec struct {
path map[string]encoding.TextUnmarshaler
form map[string]encoding.TextUnmarshaler
}
func Path(k string, dst encoding.TextUnmarshaler) Spec {
return Spec{
path: map[string]encoding.TextUnmarshaler{
k: dst,
},
}
}
func Form(k string, dst encoding.TextUnmarshaler) Spec {
return Spec{
form: map[string]encoding.TextUnmarshaler{
k: dst,
},
}
}
func (s Spec) Path(k string, dst encoding.TextUnmarshaler) Spec {
if s.path == nil {
s.path = make(map[string]encoding.TextUnmarshaler, 1)
}
s.path[k] = dst
return s
}
func (s Spec) Form(k string, dst encoding.TextUnmarshaler) Spec {
if s.form == nil {
s.form = make(map[string]encoding.TextUnmarshaler, 1)
}
s.form[k] = dst
return s
}
func (s Spec) Unmarshal(c *gin.Context) error {
for k, dst := range s.path {
v := c.Param(k)
if v == "" {
return response.NotFound().Msgf("no %s provided", k)
}
if err := dst.UnmarshalText([]byte(v)); err != nil {
return response.NotFound().Wrap(err).Msgf("invalid %s", k)
}
}
for k, dst := range s.form {
v, ok := c.GetPostForm(k)
if !ok || v == "" {
return response.BadRequest().Msgf("no %s provided", k)
}
if err := dst.UnmarshalText([]byte(v)); err != nil {
return response.BadRequest().Wrap(err).Msgf("invalid %s provided", k)
}
}
return nil
}