2019-06-25 02:40:29 -08:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-25 22:31:54 -08:00
|
|
|
"net/http"
|
|
|
|
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/auth"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
2019-08-04 14:48:43 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/webcontext"
|
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web/weberror"
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/signup"
|
2019-06-25 02:40:29 -08:00
|
|
|
"github.com/pkg/errors"
|
2019-06-25 22:31:54 -08:00
|
|
|
"gopkg.in/go-playground/validator.v9"
|
2019-06-25 02:40:29 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Signup represents the Signup API method handler set.
|
|
|
|
type Signup struct {
|
2019-08-14 12:53:40 -08:00
|
|
|
*signup.Repository
|
2019-06-25 02:40:29 -08:00
|
|
|
|
|
|
|
// 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
|
2019-08-05 19:49:30 -08:00
|
|
|
// @Failure 400 {object} weberror.ErrorResponse
|
|
|
|
// @Failure 500 {object} weberror.ErrorResponse
|
2019-06-25 02:40:29 -08:00
|
|
|
// @Router /signup [post]
|
2019-08-14 12:53:40 -08:00
|
|
|
func (h *Signup) Signup(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-08-01 16:17:47 -08:00
|
|
|
v, err := webcontext.ContextValues(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-25 02:40:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Claims are optional as authentication is not required ATM for this method.
|
2019-08-01 16:17:47 -08:00
|
|
|
claims, _ := auth.ClaimsFromContext(ctx)
|
2019-08-07 23:00:12 -08:00
|
|
|
|
2019-06-25 02:40:29 -08:00
|
|
|
var req signup.SignupRequest
|
2019-08-01 16:17:47 -08:00
|
|
|
if err := web.Decode(ctx, r, &req); err != nil {
|
|
|
|
if _, ok := errors.Cause(err).(*weberror.Error); !ok {
|
|
|
|
err = weberror.NewError(ctx, err, http.StatusBadRequest)
|
2019-06-25 22:31:54 -08:00
|
|
|
}
|
2019-06-27 04:48:18 -08:00
|
|
|
return web.RespondJsonError(ctx, w, err)
|
2019-06-25 02:40:29 -08:00
|
|
|
}
|
|
|
|
|
2019-08-14 12:53:40 -08:00
|
|
|
res, err := h.Repository.Signup(ctx, claims, req, v.Now)
|
2019-06-25 02:40:29 -08:00
|
|
|
if err != nil {
|
2019-06-26 20:21:00 -08:00
|
|
|
switch errors.Cause(err) {
|
2019-06-25 02:40:29 -08:00
|
|
|
case account.ErrForbidden:
|
2019-08-01 16:17:47 -08:00
|
|
|
return web.RespondJsonError(ctx, w, weberror.NewError(ctx, err, http.StatusForbidden))
|
2019-06-25 02:40:29 -08:00
|
|
|
default:
|
2019-06-25 22:31:54 -08:00
|
|
|
_, ok := err.(validator.ValidationErrors)
|
|
|
|
if ok {
|
2019-08-01 16:17:47 -08:00
|
|
|
return web.RespondJsonError(ctx, w, weberror.NewError(ctx, err, http.StatusBadRequest))
|
2019-06-25 22:31:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrapf(err, "Signup: %+v", &req)
|
2019-06-25 02:40:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:21:00 -08:00
|
|
|
return web.RespondJson(ctx, w, res.Response(ctx), http.StatusCreated)
|
2019-06-25 02:40:29 -08:00
|
|
|
}
|