add dummy member endpoints.

This commit is contained in:
Miroslav Šedivý 2020-11-21 23:39:24 +01:00
parent 45f029a2a4
commit d39d922f85
4 changed files with 83 additions and 1 deletions

View File

@ -1 +0,0 @@
package member

View File

@ -0,0 +1,30 @@
package member
import (
"net/http"
"demodesk/neko/internal/utils"
)
func (h *MemberHandler) memberCreate(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w)
}
func (h *MemberHandler) memberRead(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
utils.HttpSuccess(w, "Your name is " + member.Name() + ".")
}
func (h *MemberHandler) memberUpdate(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
utils.HttpSuccess(w, "Your name is " + member.Name() + ".")
}
func (h *MemberHandler) memberDelete(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
utils.HttpSuccess(w, "Your name is " + member.Name() + ".")
}

View File

@ -1,9 +1,20 @@
package member
import (
"context"
"net/http"
"github.com/go-chi/chi"
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
"demodesk/neko/internal/http/auth"
)
type key int
const (
keyMemberCtx key = iota
)
type MemberHandler struct {
@ -22,4 +33,34 @@ func New(
func (h *MemberHandler) Route(r chi.Router) {
r.With(auth.AdminsOnly).Group(func(r chi.Router) {
r.Get("/", h.memberList)
r.Post("/", h.memberCreate)
r.Get("/{memberId}/", h.memberRead)
r.Post("/{memberId}/", h.memberUpdate)
r.Delete("/{memberId}/", h.memberDelete)
})
}
func SetMember(r *http.Request, session types.Session) *http.Request {
ctx := context.WithValue(r.Context(), keyMemberCtx, session)
return r.WithContext(ctx)
}
func GetMember(r *http.Request) types.Session {
return r.Context().Value(keyMemberCtx).(types.Session)
}
func (h *MemberHandler) ExtractMember(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
memberId := chi.URLParam(r, "memberId")
session, ok := h.sessions.Get(memberId)
if !ok {
utils.HttpNotFound(w, "Member was not found.")
} else {
next.ServeHTTP(w, SetMember(r, session))
}
})
}

View File

@ -0,0 +1,12 @@
package member
import (
"net/http"
"demodesk/neko/internal/utils"
)
func (h *MemberHandler) memberList(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w)
}