use member id or create new.

This commit is contained in:
Miroslav Šedivý 2020-12-02 16:50:30 +01:00
parent 123fbd8317
commit 5541675f57

View File

@ -28,13 +28,20 @@ func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
return return
} }
id, err := utils.NewUID(32) if data.ID == "" {
if err != nil { var err error
if data.ID, err = utils.NewUID(32); err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return 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, Secret: data.Secret,
Name: data.Name, Name: data.Name,
IsAdmin: data.IsAdmin, IsAdmin: data.IsAdmin,
@ -51,7 +58,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(),
IsAdmin: member.Admin(), IsAdmin: member.IsAdmin(),
}) })
} }
@ -66,7 +73,7 @@ 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(),
IsAdmin: member.Admin(), IsAdmin: member.IsAdmin(),
}) })
} }