mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
add dummy member endpoints.
This commit is contained in:
parent
45f029a2a4
commit
d39d922f85
@ -1 +0,0 @@
|
||||
package member
|
30
internal/api/member/crud.go
Normal file
30
internal/api/member/crud.go
Normal 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() + ".")
|
||||
}
|
@ -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))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
12
internal/api/member/list.go
Normal file
12
internal/api/member/list.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user