2019-06-24 17:36:42 -08:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-08-17 11:03:48 +07:00
|
|
|
"time"
|
2019-06-24 17:36:42 -08:00
|
|
|
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/account"
|
2019-08-17 11:03:48 +07:00
|
|
|
accountref "geeks-accelerator/oss/saas-starter-kit/internal/account/account_preference"
|
2019-07-13 12:16:28 -08:00
|
|
|
"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-08-17 11:03:48 +07:00
|
|
|
|
2019-06-24 17:36:42 -08:00
|
|
|
"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.
|
2019-08-17 11:03:48 +07:00
|
|
|
type Accounts struct {
|
|
|
|
Repository AccountRepository
|
2019-06-24 17:36:42 -08:00
|
|
|
|
|
|
|
// ADD OTHER STATE LIKE THE LOGGER AND CONFIG HERE.
|
|
|
|
}
|
|
|
|
|
2019-08-17 11:03:48 +07:00
|
|
|
type AccountRepository interface {
|
|
|
|
//CanReadAccount(ctx context.Context, claims auth.Claims, dbConn *sqlx.DB, accountID string) error
|
|
|
|
Find(ctx context.Context, claims auth.Claims, req account.AccountFindRequest) (account.Accounts, error)
|
|
|
|
Create(ctx context.Context, claims auth.Claims, req account.AccountCreateRequest, now time.Time) (*account.Account, error)
|
|
|
|
ReadByID(ctx context.Context, claims auth.Claims, id string) (*account.Account, error)
|
|
|
|
Read(ctx context.Context, claims auth.Claims, req account.AccountReadRequest) (*account.Account, error)
|
|
|
|
Update(ctx context.Context, claims auth.Claims, req account.AccountUpdateRequest, now time.Time) error
|
|
|
|
Archive(ctx context.Context, claims auth.Claims, req account.AccountArchiveRequest, now time.Time) error
|
|
|
|
Delete(ctx context.Context, claims auth.Claims, req account.AccountDeleteRequest) error
|
|
|
|
}
|
|
|
|
type AccountPrefRepository interface {
|
|
|
|
Find(ctx context.Context, claims auth.Claims, req accountref.AccountPreferenceFindRequest) ([]*accountref.AccountPreference, error)
|
|
|
|
FindByAccountID(ctx context.Context, claims auth.Claims, req accountref.AccountPreferenceFindByAccountIDRequest) ([]*accountref.AccountPreference, error)
|
|
|
|
Read(ctx context.Context, claims auth.Claims, req accountref.AccountPreferenceReadRequest) (*accountref.AccountPreference, error)
|
|
|
|
Set(ctx context.Context, claims auth.Claims, req accountref.AccountPreferenceSetRequest, now time.Time) error
|
|
|
|
Archive(ctx context.Context, claims auth.Claims, req accountref.AccountPreferenceArchiveRequest, now time.Time) error
|
|
|
|
Delete(ctx context.Context, claims auth.Claims, req accountref.AccountPreferenceDeleteRequest) error
|
|
|
|
}
|
|
|
|
|
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
|
2019-08-05 19:49:30 -08:00
|
|
|
// @Failure 400 {object} weberror.ErrorResponse
|
|
|
|
// @Failure 404 {object} weberror.ErrorResponse
|
|
|
|
// @Failure 500 {object} weberror.ErrorResponse
|
2019-06-24 22:41:21 -08:00
|
|
|
// @Router /accounts/{id} [get]
|
2019-08-17 11:03:48 +07:00
|
|
|
func (h *Accounts) Read(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error {
|
2019-06-24 17:36:42 -08:00
|
|
|
claims, ok := ctx.Value(auth.Key).(auth.Claims)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("claims missing from context")
|
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
// Handle include-archived query value if set.
|
2019-06-24 17:36:42 -08:00
|
|
|
var includeArchived bool
|
2019-08-04 14:48:43 -08:00
|
|
|
if v := r.URL.Query().Get("include-archived"); v != "" {
|
2019-06-26 20:21:00 -08:00
|
|
|
b, err := strconv.ParseBool(v)
|
2019-06-24 17:36:42 -08:00
|
|
|
if err != nil {
|
2019-08-04 14:48:43 -08:00
|
|
|
err = errors.WithMessagef(err, "unable to parse %s as boolean for include-archived param", v)
|
2019-08-01 16:17:47 -08:00
|
|
|
return web.RespondJsonError(ctx, w, weberror.NewError(ctx, 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
|
|
|
}
|
|
|
|
|
2019-08-14 12:53:40 -08:00
|
|
|
res, err := h.Repository.Read(ctx, claims, account.AccountReadRequest{
|
2019-08-04 14:48:43 -08:00
|
|
|
ID: params["id"],
|
|
|
|
IncludeArchived: includeArchived,
|
|
|
|
})
|
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.ErrNotFound:
|
2019-08-01 16:17:47 -08:00
|
|
|
return web.RespondJsonError(ctx, w, weberror.NewError(ctx, 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-08-05 19:49:30 -08:00
|
|
|
// @Failure 400 {object} weberror.ErrorResponse
|
|
|
|
// @Failure 403 {object} weberror.ErrorResponse
|
|
|
|
// @Failure 500 {object} weberror.ErrorResponse
|
2019-06-25 22:31:54 -08:00
|
|
|
// @Router /accounts [patch]
|
2019-08-17 11:03:48 +07:00
|
|
|
func (h *Accounts) Update(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-24 17:36:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
claims, ok := ctx.Value(auth.Key).(auth.Claims)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("claims missing from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
var req account.AccountUpdateRequest
|
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-24 17:36:42 -08:00
|
|
|
}
|
2019-06-27 04:48:18 -08:00
|
|
|
return web.RespondJsonError(ctx, w, err)
|
2019-06-24 17:36:42 -08:00
|
|
|
}
|
|
|
|
|
2019-08-14 12:53:40 -08:00
|
|
|
err = h.Repository.Update(ctx, claims, 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-08-01 16:17:47 -08:00
|
|
|
return web.RespondJsonError(ctx, w, weberror.NewError(ctx, 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-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
|
|
|
}
|
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)
|
|
|
|
}
|