neko/internal/api/members/bluk.go
2022-03-20 11:27:41 +01:00

58 lines
1.5 KiB
Go

package members
import (
"encoding/json"
"io"
"net/http"
"gitlab.com/demodesk/neko/server/internal/types"
"gitlab.com/demodesk/neko/server/internal/utils"
)
type MemberBulkUpdatePayload struct {
IDs []string `json:"ids"`
Profile types.MemberProfile `json:"profile"`
}
func (h *MembersHandler) membersBulkUpdate(w http.ResponseWriter, r *http.Request) error {
bytes, err := io.ReadAll(r.Body)
if err != nil {
return utils.HttpBadRequest("unable to read post body").WithInternalErr(err)
}
header := &MemberBulkUpdatePayload{}
if err := json.Unmarshal(bytes, &header); err != nil {
return utils.HttpBadRequest("unable to unmarshal payload").WithInternalErr(err)
}
for _, memberId := range header.IDs {
// TODO: Bulk select?
profile, err := h.members.Select(memberId)
if err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to select member profile").
Msgf("failed to update member %s", memberId)
}
body := &MemberBulkUpdatePayload{
Profile: profile,
}
if err := json.Unmarshal(bytes, &body); err != nil {
return utils.HttpBadRequest().
WithInternalErr(err).
Msgf("unable to unmarshal payload for member %s", memberId)
}
if err := h.members.UpdateProfile(memberId, body.Profile); err != nil {
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to update member profile").
Msgf("failed to update member %s", memberId)
}
}
return utils.HttpSuccess(w)
}