From d39d922f8507e29e8947393569c49f74fb0fe354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 21 Nov 2020 23:39:24 +0100 Subject: [PATCH] add dummy member endpoints. --- internal/api/member/authenticate.go | 1 - internal/api/member/crud.go | 30 +++++++++++++++++++++ internal/api/member/handler.go | 41 +++++++++++++++++++++++++++++ internal/api/member/list.go | 12 +++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) delete mode 100644 internal/api/member/authenticate.go create mode 100644 internal/api/member/crud.go create mode 100644 internal/api/member/list.go diff --git a/internal/api/member/authenticate.go b/internal/api/member/authenticate.go deleted file mode 100644 index 363b41ee..00000000 --- a/internal/api/member/authenticate.go +++ /dev/null @@ -1 +0,0 @@ -package member diff --git a/internal/api/member/crud.go b/internal/api/member/crud.go new file mode 100644 index 00000000..7ab90178 --- /dev/null +++ b/internal/api/member/crud.go @@ -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() + ".") +} diff --git a/internal/api/member/handler.go b/internal/api/member/handler.go index 6d35d38a..c50cc26e 100644 --- a/internal/api/member/handler.go +++ b/internal/api/member/handler.go @@ -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)) + } + }) } diff --git a/internal/api/member/list.go b/internal/api/member/list.go new file mode 100644 index 00000000..e9485e6b --- /dev/null +++ b/internal/api/member/list.go @@ -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) +}