separate profile model from account model
This commit is contained in:
parent
f729160209
commit
c3271d7dcf
15 changed files with 152 additions and 83 deletions
95
api/app/profile.go
Normal file
95
api/app/profile.go
Normal 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))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue