REST create & Delete member.

This commit is contained in:
Miroslav Šedivý 2020-11-25 22:39:54 +01:00
parent 71a84963bc
commit a330a3cc76

View File

@ -4,6 +4,7 @@ import (
"net/http" "net/http"
"demodesk/neko/internal/utils" "demodesk/neko/internal/utils"
"demodesk/neko/internal/types"
) )
type MemberCreatePayload struct { type MemberCreatePayload struct {
@ -13,7 +14,11 @@ type MemberCreatePayload struct {
type MemberDataPayload struct { type MemberDataPayload struct {
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Admin bool `json:"admin"` IsAdmin bool `json:"is_admin"`
//Enabled bool `json:"enabled"`
//CanControl bool `json:"can_control"`
//CanWatch bool `json:"can_watch"`
//ClipboardAccess bool `json:"clipboard_access"`
} }
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
@ -22,8 +27,17 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
return return
} }
session, err := h.sessions.Create(types.MemberProfile{
Name: data.Name,
IsAdmin: data.IsAdmin,
})
if err != nil {
utils.HttpInternalServer(w, err)
return
}
utils.HttpSuccess(w, MemberCreatePayload{ utils.HttpSuccess(w, MemberCreatePayload{
ID: "some_id", ID: session.ID(),
}) })
} }
@ -38,7 +52,7 @@ func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w, MemberDataPayload{ utils.HttpSuccess(w, MemberDataPayload{
ID: member.ID(), ID: member.ID(),
Name: member.Name(), Name: member.Name(),
Admin: member.Admin(), IsAdmin: member.Admin(),
}) })
} }
@ -53,16 +67,17 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w, MemberDataPayload{ utils.HttpSuccess(w, MemberDataPayload{
ID: member.ID(), ID: member.ID(),
Name: member.Name(), Name: member.Name(),
Admin: member.Admin(), IsAdmin: member.Admin(),
}) })
} }
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) { func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
member := GetMember(r) member := GetMember(r)
utils.HttpSuccess(w, MemberDataPayload{ if err := h.sessions.Delete(member.ID()); err != nil {
ID: member.ID(), utils.HttpInternalServer(w, err)
Name: member.Name(), return
Admin: member.Admin(), }
})
utils.HttpSuccess(w)
} }