1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-06 23:46:29 +02:00

68 lines
2.1 KiB
Go
Raw Normal View History

package handlers
import (
"context"
"net/http"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/account"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/auth"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/web"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/signup"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"gopkg.in/go-playground/validator.v9"
)
// Signup represents the Signup API method handler set.
type Signup struct {
MasterDB *sqlx.DB
// ADD OTHER STATE LIKE THE LOGGER AND CONFIG HERE.
}
// Signup godoc
// @Summary Signup handles new account creation.
// @Description Signup creates a new account and user in the system.
// @Tags signup
// @Accept json
// @Produce json
// @Param data body signup.SignupRequest true "Signup details"
2019-06-26 20:21:00 -08:00
// @Success 201 {object} signup.SignupResponse
// @Failure 400 {object} web.ErrorResponse
// @Failure 500 {object} web.ErrorResponse
// @Router /signup [post]
func (c *Signup) Signup(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
v, ok := ctx.Value(web.KeyValues).(*web.Values)
if !ok {
return web.NewShutdownError("web value missing from context")
}
// Claims are optional as authentication is not required ATM for this method.
claims, _ := ctx.Value(auth.Key).(auth.Claims)
var req signup.SignupRequest
if err := web.Decode(r, &req); err != nil {
2019-06-26 20:21:00 -08:00
if _, ok := errors.Cause(err).(*web.Error); !ok {
err = web.NewRequestError(err, http.StatusBadRequest)
}
2019-06-26 20:21:00 -08:00
return web.RespondJsonError(ctx, w, err)
}
res, err := signup.Signup(ctx, claims, c.MasterDB, req, v.Now)
if err != nil {
2019-06-26 20:21:00 -08:00
switch errors.Cause(err) {
case account.ErrForbidden:
2019-06-26 20:21:00 -08:00
return web.RespondJsonError(ctx, w, web.NewRequestError(err, http.StatusForbidden))
default:
_, ok := err.(validator.ValidationErrors)
if ok {
2019-06-26 20:21:00 -08:00
return web.RespondJsonError(ctx, w, web.NewRequestError(err, http.StatusBadRequest))
}
return errors.Wrapf(err, "Signup: %+v", &req)
}
}
2019-06-26 20:21:00 -08:00
return web.RespondJson(ctx, w, res.Response(ctx), http.StatusCreated)
}