neko/internal/api/members/bluk.go

56 lines
1.2 KiB
Go
Raw Normal View History

2021-04-06 09:03:49 +12:00
package members
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"demodesk/neko/internal/types"
"demodesk/neko/internal/utils"
)
type MemberBulkUpdatePayload struct {
IDs []string `json:"ids"`
Profile types.MemberProfile `json:"profile"`
}
func (h *MembersHandler) membersBulkUpdate(w http.ResponseWriter, r *http.Request) {
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
header := &MemberBulkUpdatePayload{}
if err := json.Unmarshal(bytes, &header); err != nil {
utils.HttpInternalServerError(w, err)
return
}
for _, memberId := range header.IDs {
// TODO: Bulk select?
profile, err := h.members.Select(memberId)
if err != nil {
utils.HttpInternalServerError(w, fmt.Sprintf("member %s: %v", memberId, err))
return
}
body := &MemberBulkUpdatePayload{
Profile: profile,
}
if err := json.Unmarshal(bytes, &body); err != nil {
utils.HttpInternalServerError(w, fmt.Sprintf("member %s: %v", memberId, err))
return
}
if err := h.members.UpdateProfile(memberId, body.Profile); err != nil {
utils.HttpInternalServerError(w, fmt.Sprintf("member %s: %v", memberId, err))
return
}
}
utils.HttpSuccess(w)
}