mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
bulk update members.
This commit is contained in:
parent
ac04eaaa48
commit
b48998f3f9
55
internal/api/members/bluk.go
Normal file
55
internal/api/members/bluk.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
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)
|
||||||
|
}
|
@ -45,6 +45,12 @@ func (h *MembersHandler) Route(r chi.Router) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *MembersHandler) RouteBulk(r chi.Router) {
|
||||||
|
r.With(auth.AdminsOnly).Group(func(r chi.Router) {
|
||||||
|
r.Post("/update", h.membersBulkUpdate)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
type MemberData struct {
|
type MemberData struct {
|
||||||
ID string
|
ID string
|
||||||
Profile types.MemberProfile
|
Profile types.MemberProfile
|
||||||
|
@ -50,6 +50,7 @@ func (api *ApiManagerCtx) Route(r chi.Router) {
|
|||||||
|
|
||||||
membersHandler := members.New(api.members)
|
membersHandler := members.New(api.members)
|
||||||
r.Route("/members", membersHandler.Route)
|
r.Route("/members", membersHandler.Route)
|
||||||
|
r.Route("/members_bulk", membersHandler.RouteBulk)
|
||||||
|
|
||||||
roomHandler := room.New(api.sessions, api.desktop, api.capture)
|
roomHandler := room.New(api.sessions, api.desktop, api.capture)
|
||||||
r.Route("/room", roomHandler.Route)
|
r.Route("/room", roomHandler.Route)
|
||||||
|
Loading…
Reference in New Issue
Block a user