separate profile model from account model

This commit is contained in:
dhax 2017-10-13 00:11:57 +02:00
parent f729160209
commit c3271d7dcf
15 changed files with 152 additions and 83 deletions

View file

@ -11,7 +11,6 @@ import (
validation "github.com/go-ozzo/ozzo-validation"
"github.com/dhax/go-base/auth"
"github.com/dhax/go-base/models"
)
// AccountStore defines database operations for account.
@ -21,7 +20,6 @@ type AccountStore interface {
Delete(*auth.Account) error
UpdateToken(*auth.Token) error
DeleteToken(*auth.Token) error
UpdateProfile(*models.Profile) error
}
// AccountResource implements account managment handler.
@ -46,7 +44,6 @@ func (rs *AccountResource) router() *chi.Mux {
r.Put("/", rs.updateToken)
r.Delete("/", rs.deleteToken)
})
r.Put("/profile", rs.updateProfile)
return r
}
@ -57,8 +54,7 @@ func (rs *AccountResource) accountCtx(next http.Handler) http.Handler {
account, err := rs.Store.Get(claims.ID)
if err != nil {
// account deleted while access token still valid
log(r).WithField("account", claims.Sub).Warn(err)
render.Render(w, r, ErrNotFound)
render.Render(w, r, ErrUnauthorized)
return
}
ctx := context.WithValue(r.Context(), ctxAccount, account)
@ -68,7 +64,8 @@ func (rs *AccountResource) accountCtx(next http.Handler) http.Handler {
type accountRequest struct {
*auth.Account
// not really neccessary here as we limit updated database columns in store
// override protected data here, although not really neccessary here
// as we limit updated database columns in store as well
ProtectedID int `json:"id"`
ProtectedActive bool `json:"active"`
ProtectedRoles []string `json:"roles"`
@ -174,44 +171,3 @@ func (rs *AccountResource) deleteToken(w http.ResponseWriter, r *http.Request) {
}
render.Respond(w, r, http.NoBody)
}
type profileRequest struct {
*models.Profile
ProtectedID int `json:"id"`
}
func (d *profileRequest) Bind(r *http.Request) error {
// d.ProtectedActive = true
// d.ProtectedRoles = []string{}
return nil
}
type profileResponse struct {
*models.Profile
}
func newProfileResponse(p *models.Profile) *profileResponse {
return &profileResponse{
Profile: p,
}
}
func (rs *AccountResource) updateProfile(w http.ResponseWriter, r *http.Request) {
acc := r.Context().Value(ctxAccount).(*auth.Account)
data := &profileRequest{Profile: acc.Profile}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
}
p := data.Profile
if err := rs.Store.UpdateProfile(p); err != nil {
switch err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(err))
return
}
render.Render(w, r, ErrRender(err))
return
}
render.Respond(w, r, newProfileResponse(p))
}

View file

@ -15,11 +15,13 @@ type ctxKey int
const (
ctxAccount ctxKey = iota
ctxProfile
)
// API provides application resources and handlers.
type API struct {
Account *AccountResource
Profile *ProfileResource
}
// NewAPI configures and returns application API.
@ -27,8 +29,12 @@ func NewAPI(db *pg.DB) (*API, error) {
accountStore := database.NewAccountStore(db)
account := NewAccountResource(accountStore)
profileStore := database.NewProfileStore(db)
profile := NewProfileResource(profileStore)
api := &API{
Account: account,
Profile: profile,
}
return api, nil
}
@ -38,6 +44,7 @@ func (a *API) Router() *chi.Mux {
r := chi.NewRouter()
r.Mount("/account", a.Account.router())
r.Mount("/profile", a.Profile.router())
return r
}

View file

@ -70,9 +70,12 @@ func ErrRender(err error) render.Renderer {
}
var (
// ErrBadRequest return status 400 Bad Request for malformed request body.
// ErrBadRequest returns status 400 Bad Request for malformed request body.
ErrBadRequest = &ErrResponse{HTTPStatusCode: http.StatusBadRequest, StatusText: http.StatusText(http.StatusBadRequest)}
// ErrUnauthorized returns 401 Unauthorized.
ErrUnauthorized = &ErrResponse{HTTPStatusCode: http.StatusUnauthorized, StatusText: http.StatusText(http.StatusUnauthorized)}
// ErrNotFound returns status 404 Not Found for invalid resource request.
ErrNotFound = &ErrResponse{HTTPStatusCode: http.StatusNotFound, StatusText: http.StatusText(http.StatusNotFound)}

95
api/app/profile.go Normal file
View file

@ -0,0 +1,95 @@
package app
import (
"context"
"net/http"
"github.com/dhax/go-base/auth"
"github.com/dhax/go-base/models"
"github.com/go-chi/chi"
"github.com/go-chi/render"
validation "github.com/go-ozzo/ozzo-validation"
)
// ProfileStore defines database operations for a profile.
type ProfileStore interface {
Get(accountID int) (*models.Profile, error)
Update(p *models.Profile) error
}
// ProfileResource implements profile management handler.
type ProfileResource struct {
Store ProfileStore
}
// NewProfileResource creates and returns a profile resource.
func NewProfileResource(store ProfileStore) *ProfileResource {
return &ProfileResource{
Store: store,
}
}
func (rs *ProfileResource) router() *chi.Mux {
r := chi.NewRouter()
r.Use(rs.profileCtx)
r.Get("/", rs.get)
r.Put("/", rs.update)
return r
}
func (rs *ProfileResource) profileCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := auth.ClaimsFromCtx(r.Context())
p, err := rs.Store.Get(claims.ID)
if err != nil {
log(r).WithField("profileCtx", claims.Sub).Error(err)
render.Render(w, r, ErrInternalServerError)
return
}
ctx := context.WithValue(r.Context(), ctxProfile, p)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
type profileRequest struct {
*models.Profile
ProtectedID int `json:"id"`
}
func (d *profileRequest) Bind(r *http.Request) error {
return nil
}
type profileResponse struct {
*models.Profile
}
func newProfileResponse(p *models.Profile) *profileResponse {
return &profileResponse{
Profile: p,
}
}
func (rs *ProfileResource) get(w http.ResponseWriter, r *http.Request) {
p := r.Context().Value(ctxProfile).(*models.Profile)
render.Respond(w, r, newProfileResponse(p))
}
func (rs *ProfileResource) update(w http.ResponseWriter, r *http.Request) {
p := r.Context().Value(ctxProfile).(*models.Profile)
data := &profileRequest{Profile: p}
if err := render.Bind(r, data); err != nil {
render.Render(w, r, ErrInvalidRequest(err))
}
if err := rs.Store.Update(p); err != nil {
switch err.(type) {
case validation.Errors:
render.Render(w, r, ErrValidation(err))
return
}
render.Render(w, r, ErrRender(err))
return
}
render.Respond(w, r, newProfileResponse(p))
}