neko/internal/api/members/bluk.go

58 lines
1.5 KiB
Go
Raw Normal View History

2021-04-06 09:03:49 +12:00
package members
import (
"encoding/json"
2021-08-30 03:12:37 +12:00
"io"
2021-04-06 09:03:49 +12:00
"net/http"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils"
2021-04-06 09:03:49 +12:00
)
type MemberBulkUpdatePayload struct {
IDs []string `json:"ids"`
Profile types.MemberProfile `json:"profile"`
}
2021-09-17 10:58:50 +12:00
func (h *MembersHandler) membersBulkUpdate(w http.ResponseWriter, r *http.Request) error {
2021-08-30 03:12:37 +12:00
bytes, err := io.ReadAll(r.Body)
2021-04-06 09:03:49 +12:00
if err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpBadRequest("unable to read post body").WithInternalErr(err)
2021-04-06 09:03:49 +12:00
}
header := &MemberBulkUpdatePayload{}
if err := json.Unmarshal(bytes, &header); err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpBadRequest("unable to unmarshal payload").WithInternalErr(err)
2021-04-06 09:03:49 +12:00
}
for _, memberId := range header.IDs {
// TODO: Bulk select?
profile, err := h.members.Select(memberId)
if err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to select member profile").
Msgf("failed to update member %s", memberId)
2021-04-06 09:03:49 +12:00
}
body := &MemberBulkUpdatePayload{
Profile: profile,
}
if err := json.Unmarshal(bytes, &body); err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpBadRequest().
WithInternalErr(err).
Msgf("unable to unmarshal payload for member %s", memberId)
2021-04-06 09:03:49 +12:00
}
if err := h.members.UpdateProfile(memberId, body.Profile); err != nil {
2021-09-17 10:58:50 +12:00
return utils.HttpInternalServerError().
WithInternalErr(err).
WithInternalMsg("unable to update member profile").
Msgf("failed to update member %s", memberId)
2021-04-06 09:03:49 +12:00
}
}
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
2021-04-06 09:03:49 +12:00
}