2019-06-24 17:36:42 -08:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/pkg/errors"
|
2019-06-25 22:31:54 -08:00
|
|
|
"gopkg.in/go-playground/validator.v9"
|
2019-06-24 17:36:42 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Account represents the Account API method handler set.
|
|
|
|
type Account struct {
|
|
|
|
MasterDB *sqlx.DB
|
|
|
|
|
|
|
|
// ADD OTHER STATE LIKE THE LOGGER AND CONFIG HERE.
|
|
|
|
}
|
|
|
|
|
2019-06-24 22:41:21 -08:00
|
|
|
// Read godoc
|
2019-06-25 22:31:54 -08:00
|
|
|
// @Summary Get account by ID
|
|
|
|
// @Description Read returns the specified account from the system.
|
2019-06-25 02:40:29 -08:00
|
|
|
// @Tags account
|
2019-06-24 22:41:21 -08:00
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-06-25 06:25:55 -08:00
|
|
|
// @Security OAuth2Password
|
|
|
|
// @Param id path string true "Account ID"
|
2019-06-25 22:31:54 -08:00
|
|
|
// @Success 200 {object} account.AccountResponse
|
|
|
|
// @Failure 400 {object} web.ErrorResponse
|
|
|
|
// @Failure 404 {object} web.ErrorResponse
|
|
|
|
// @Failure 500 {object} web.ErrorResponse
|
2019-06-24 22:41:21 -08:00
|
|
|
// @Router /accounts/{id} [get]
|
2019-06-24 17:36:42 -08:00
|
|
|
func (a *Account) Read(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
|
|
|
claims, ok := ctx.Value(auth.Key).(auth.Claims)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("claims missing from context")
|
|
|
|
}
|
|
|
|
|
2019-06-26 20:21:00 -08:00
|
|
|
// Handle included-archived query value if set.
|
2019-06-24 17:36:42 -08:00
|
|
|
var includeArchived bool
|
2019-06-26 20:21:00 -08:00
|
|
|
if v := r.URL.Query().Get("included-archived"); v != "" {
|
|
|
|
b, err := strconv.ParseBool(v)
|
2019-06-24 17:36:42 -08:00
|
|
|
if err != nil {
|
2019-06-26 20:21:00 -08:00
|
|
|
err = errors.WithMessagef(err, "unable to parse %s as boolean for included-archived param", v)
|
|
|
|
return web.RespondJsonError(ctx, w, web.NewRequestError(err, http.StatusBadRequest))
|
2019-06-24 17:36:42 -08:00
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
includeArchived = b
|
2019-06-24 17:36:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := account.Read(ctx, claims, a.MasterDB, params["id"], includeArchived)
|
|
|
|
if err != nil {
|
2019-06-26 20:21:00 -08:00
|
|
|
cause := errors.Cause(err)
|
|
|
|
switch cause {
|
2019-06-24 17:36:42 -08:00
|
|
|
case account.ErrNotFound:
|
2019-06-26 20:21:00 -08:00
|
|
|
return web.RespondJsonError(ctx, w, web.NewRequestError(err, http.StatusNotFound))
|
2019-06-24 17:36:42 -08:00
|
|
|
default:
|
|
|
|
return errors.Wrapf(err, "ID: %s", params["id"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 22:31:54 -08:00
|
|
|
return web.RespondJson(ctx, w, res.Response(ctx), http.StatusOK)
|
2019-06-24 17:36:42 -08:00
|
|
|
}
|
|
|
|
|
2019-06-25 22:31:54 -08:00
|
|
|
// Read godoc
|
|
|
|
// @Summary Update account by ID
|
|
|
|
// @Description Update updates the specified account in the system.
|
|
|
|
// @Tags account
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security OAuth2Password
|
|
|
|
// @Param data body account.AccountUpdateRequest true "Update fields"
|
2019-06-26 20:21:00 -08:00
|
|
|
// @Success 204
|
2019-06-25 22:31:54 -08:00
|
|
|
// @Failure 400 {object} web.ErrorResponse
|
|
|
|
// @Failure 403 {object} web.ErrorResponse
|
|
|
|
// @Failure 500 {object} web.ErrorResponse
|
|
|
|
// @Router /accounts [patch]
|
2019-06-24 17:36:42 -08:00
|
|
|
func (a *Account) Update(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, ok := ctx.Value(auth.Key).(auth.Claims)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("claims missing from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
var req account.AccountUpdateRequest
|
|
|
|
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-24 17:36:42 -08:00
|
|
|
}
|
2019-06-26 20:21:00 -08:00
|
|
|
return web.RespondJsonError(ctx, w, err)
|
2019-06-24 17:36:42 -08:00
|
|
|
}
|
|
|
|
|
2019-06-25 22:31:54 -08:00
|
|
|
err := account.Update(ctx, claims, a.MasterDB, req, v.Now)
|
2019-06-24 17:36:42 -08:00
|
|
|
if err != nil {
|
2019-06-26 20:21:00 -08:00
|
|
|
cause := errors.Cause(err)
|
|
|
|
switch cause {
|
2019-06-24 17:36:42 -08:00
|
|
|
case account.ErrForbidden:
|
2019-06-26 20:21:00 -08:00
|
|
|
return web.RespondJsonError(ctx, w, web.NewRequestError(err, http.StatusForbidden))
|
2019-06-24 17:36:42 -08:00
|
|
|
default:
|
2019-06-26 20:21:00 -08:00
|
|
|
_, ok := cause.(validator.ValidationErrors)
|
2019-06-25 22:31:54 -08:00
|
|
|
if ok {
|
2019-06-26 20:21:00 -08:00
|
|
|
return web.RespondJsonError(ctx, w, web.NewRequestError(err, http.StatusBadRequest))
|
2019-06-25 22:31:54 -08:00
|
|
|
}
|
2019-06-24 17:36:42 -08:00
|
|
|
|
2019-06-26 20:21:00 -08:00
|
|
|
return errors.Wrapf(err, "Id: %s Account: %+v", req.ID, &req)
|
2019-06-24 17:36:42 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return web.RespondJson(ctx, w, nil, http.StatusNoContent)
|
|
|
|
}
|