From 5541675f57b0205f2de70440481c2dfd25e8e0b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 2 Dec 2020 16:50:30 +0100 Subject: [PATCH] use member id or create new. --- internal/api/members/crud.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/api/members/crud.go b/internal/api/members/crud.go index fb7649fd..357c27e1 100644 --- a/internal/api/members/crud.go +++ b/internal/api/members/crud.go @@ -28,13 +28,20 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) { return } - id, err := utils.NewUID(32) - if err != nil { - utils.HttpInternalServerError(w, err) - return + if data.ID == "" { + var err error + if data.ID, err = utils.NewUID(32); err != nil { + utils.HttpInternalServerError(w, err) + return + } + } else { + if _, ok := h.sessions.Get(data.ID); ok { + utils.HttpBadRequest(w, "Member ID already exists.") + return + } } - session := h.sessions.Create(id, types.MemberProfile{ + session := h.sessions.Create(data.ID, types.MemberProfile{ Secret: data.Secret, Name: data.Name, IsAdmin: data.IsAdmin, @@ -51,7 +58,7 @@ func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) { utils.HttpSuccess(w, MemberDataPayload{ ID: member.ID(), Name: member.Name(), - IsAdmin: member.Admin(), + IsAdmin: member.IsAdmin(), }) } @@ -66,7 +73,7 @@ func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) { utils.HttpSuccess(w, MemberDataPayload{ ID: member.ID(), Name: member.Name(), - IsAdmin: member.Admin(), + IsAdmin: member.IsAdmin(), }) }