2020-11-22 11:42:34 +13:00
|
|
|
package members
|
2020-10-31 06:16:21 +13:00
|
|
|
|
|
|
|
import (
|
2020-11-22 11:39:24 +13:00
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2020-10-31 06:16:21 +13:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
|
2021-02-15 02:40:17 +13:00
|
|
|
"demodesk/neko/internal/http/auth"
|
2020-10-31 06:16:21 +13:00
|
|
|
"demodesk/neko/internal/types"
|
2020-11-22 11:39:24 +13:00
|
|
|
"demodesk/neko/internal/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
2021-01-09 10:41:37 +13:00
|
|
|
keyMemberCtx key = iota
|
2020-10-31 06:16:21 +13:00
|
|
|
)
|
|
|
|
|
2020-11-22 11:42:34 +13:00
|
|
|
type MembersHandler struct {
|
2021-03-15 02:44:32 +13:00
|
|
|
members types.MemberManager
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
2021-03-15 02:44:32 +13:00
|
|
|
members types.MemberManager,
|
2020-11-22 11:42:34 +13:00
|
|
|
) *MembersHandler {
|
2020-10-31 06:16:21 +13:00
|
|
|
// Init
|
|
|
|
|
2020-11-22 11:42:34 +13:00
|
|
|
return &MembersHandler{
|
2021-03-15 02:44:32 +13:00
|
|
|
members: members,
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 11:42:34 +13:00
|
|
|
func (h *MembersHandler) Route(r chi.Router) {
|
2020-11-22 11:57:54 +13:00
|
|
|
r.Get("/", h.membersList)
|
2020-11-22 11:39:24 +13:00
|
|
|
|
2020-11-22 11:57:54 +13:00
|
|
|
r.With(auth.AdminsOnly).Group(func(r chi.Router) {
|
2020-11-22 11:42:34 +13:00
|
|
|
r.Post("/", h.membersCreate)
|
2020-11-22 11:57:54 +13:00
|
|
|
r.With(h.ExtractMember).Route("/{memberId}", func(r chi.Router) {
|
|
|
|
r.Get("/", h.membersRead)
|
2021-03-15 02:44:32 +13:00
|
|
|
r.Post("/", h.membersUpdateProfile)
|
|
|
|
r.Post("/password", h.membersUpdatePassword)
|
2020-11-22 11:57:54 +13:00
|
|
|
r.Delete("/", h.membersDelete)
|
|
|
|
})
|
2020-11-22 11:39:24 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 09:03:49 +12:00
|
|
|
func (h *MembersHandler) RouteBulk(r chi.Router) {
|
|
|
|
r.With(auth.AdminsOnly).Group(func(r chi.Router) {
|
|
|
|
r.Post("/update", h.membersBulkUpdate)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:44:32 +13:00
|
|
|
type MemberData struct {
|
|
|
|
ID string
|
|
|
|
Profile types.MemberProfile
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetMember(r *http.Request, session MemberData) *http.Request {
|
2020-11-22 11:39:24 +13:00
|
|
|
ctx := context.WithValue(r.Context(), keyMemberCtx, session)
|
|
|
|
return r.WithContext(ctx)
|
|
|
|
}
|
|
|
|
|
2021-03-15 02:44:32 +13:00
|
|
|
func GetMember(r *http.Request) MemberData {
|
|
|
|
return r.Context().Value(keyMemberCtx).(MemberData)
|
2020-11-22 11:39:24 +13:00
|
|
|
}
|
|
|
|
|
2020-11-22 11:42:34 +13:00
|
|
|
func (h *MembersHandler) ExtractMember(next http.Handler) http.Handler {
|
2020-11-22 11:39:24 +13:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
memberId := chi.URLParam(r, "memberId")
|
2021-03-15 02:44:32 +13:00
|
|
|
profile, err := h.members.Select(memberId)
|
|
|
|
if err != nil {
|
|
|
|
utils.HttpNotFound(w, err)
|
2020-11-22 11:39:24 +13:00
|
|
|
} else {
|
2021-03-15 02:44:32 +13:00
|
|
|
next.ServeHTTP(w, SetMember(r, MemberData{
|
|
|
|
ID: memberId,
|
|
|
|
Profile: profile,
|
|
|
|
}))
|
2020-11-22 11:39:24 +13:00
|
|
|
}
|
|
|
|
})
|
2020-10-31 06:16:21 +13:00
|
|
|
}
|