Uses uuid to avoid easy inferral of user ids
This commit is contained in:
parent
800c839b53
commit
932edb8da7
5 changed files with 23 additions and 12 deletions
2
go.mod
2
go.mod
|
|
@ -31,7 +31,7 @@ require (
|
||||||
github.com/godror/knownpb v0.1.1 // indirect
|
github.com/godror/knownpb v0.1.1 // indirect
|
||||||
github.com/gofiber/fiber/v2 v2.52.4 // indirect
|
github.com/gofiber/fiber/v2 v2.52.4 // indirect
|
||||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
|
||||||
github.com/google/uuid v1.5.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
github.com/huandu/xstrings v1.4.0 // indirect
|
github.com/huandu/xstrings v1.4.0 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -74,6 +74,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
|
||||||
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||||
|
|
|
||||||
|
|
@ -4,27 +4,36 @@ import (
|
||||||
"freed/internal/model"
|
"freed/internal/model"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/log"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var defaultUserError = fiber.NewError(fiber.ErrInternalServerError.Code, "Could not create user")
|
||||||
|
|
||||||
func (h *Handler) createUser(c *fiber.Ctx) error {
|
func (h *Handler) createUser(c *fiber.Ctx) error {
|
||||||
user := new(model.User)
|
user := new(model.User)
|
||||||
|
|
||||||
if err := c.BodyParser(user); err != nil {
|
userId, idErr := uuid.NewRandom()
|
||||||
return err
|
|
||||||
|
if idErr != nil {
|
||||||
|
log.Warn(idErr)
|
||||||
|
return defaultUserError
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := h.db.Exec("INSERT INTO user (first_name, email) VALUES (?, ?)", user.FirstName, user.Email)
|
user.ID = userId.String()
|
||||||
|
|
||||||
if err != nil {
|
if parseErr := c.BodyParser(user); parseErr != nil {
|
||||||
return err
|
log.Warn(parseErr)
|
||||||
|
return defaultUserError
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := result.LastInsertId()
|
_, insertErr := h.db.Exec("INSERT INTO user (id, first_name, email) VALUES (?, ?, ?)", user.ID, user.FirstName, user.Email)
|
||||||
|
|
||||||
if err != nil {
|
if insertErr != nil {
|
||||||
return err
|
log.Warn(insertErr)
|
||||||
|
return defaultUserError
|
||||||
}
|
}
|
||||||
|
|
||||||
c.SendStatus(201)
|
c.SendStatus(201)
|
||||||
return c.JSON(&fiber.Map{"userId": id})
|
return c.JSON(&fiber.Map{"userId": userId})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
-- +migrate Up
|
-- +migrate Up
|
||||||
CREATE table user (
|
CREATE table user (
|
||||||
id INTEGER PRIMARY KEY,
|
id text PRIMARY KEY,
|
||||||
first_name text NOT NULL,
|
first_name text NOT NULL,
|
||||||
email text NOT NULL
|
email text NOT NULL
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID uint32
|
ID string `json: id`
|
||||||
FirstName string `json: "firstName"`
|
FirstName string `json: "firstName"`
|
||||||
Email string `json: "email"`
|
Email string `json: "email"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue