neko/internal/api/members/controler.go

149 lines
3.1 KiB
Go
Raw Normal View History

2020-11-22 11:42:34 +13:00
package members
2020-11-22 11:39:24 +13:00
import (
"errors"
2020-11-22 11:39:24 +13:00
"net/http"
2021-03-15 08:26:58 +13:00
"strconv"
2020-11-22 11:39:24 +13:00
2020-11-26 10:39:54 +13:00
"demodesk/neko/internal/types"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/utils"
2020-11-22 11:39:24 +13:00
)
type MemberDataPayload struct {
2021-03-15 08:26:58 +13:00
ID string `json:"id"`
Profile types.MemberProfile `json:"profile"`
2020-11-22 12:06:55 +13:00
}
2021-03-15 02:44:32 +13:00
type MemberCreatePayload struct {
Username string `json:"username"`
Password string `json:"password"`
Profile types.MemberProfile `json:"profile"`
}
type MemberPasswordPayload struct {
Password string `json:"password"`
}
2020-12-09 04:05:54 +13:00
func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) {
2021-03-15 02:44:32 +13:00
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
// TODO: Default zero.
limit = 0
}
offset, err := strconv.Atoi(r.URL.Query().Get("offset"))
if err != nil {
// TODO: Default zero.
offset = 0
}
entries, err := h.members.SelectAll(limit, offset)
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
2020-12-09 04:05:54 +13:00
members := []MemberDataPayload{}
2021-03-15 02:44:32 +13:00
for id, profile := range entries {
2020-12-09 04:05:54 +13:00
members = append(members, MemberDataPayload{
ID: id,
Profile: profile,
2020-12-09 04:05:54 +13:00
})
}
utils.HttpSuccess(w, members)
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
2021-03-15 02:44:32 +13:00
data := &MemberCreatePayload{
2020-12-09 03:37:48 +13:00
// default values
2021-03-15 02:44:32 +13:00
Profile: types.MemberProfile{
2021-02-15 02:40:17 +13:00
IsAdmin: false,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
2020-12-09 03:14:20 +13:00
CanAccessClipboard: true,
},
2020-12-09 02:13:12 +13:00
}
2021-03-15 08:26:58 +13:00
2020-11-22 12:06:55 +13:00
if !utils.HttpJsonRequest(w, r, data) {
return
}
2020-11-22 11:39:24 +13:00
2021-03-15 02:44:32 +13:00
if data.Username == "" {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "username cannot be empty")
2020-12-07 09:10:45 +13:00
return
}
2021-03-15 02:44:32 +13:00
if data.Password == "" {
2021-04-25 07:37:43 +12:00
utils.HttpBadRequest(w, "password cannot be empty")
2021-03-15 02:44:32 +13:00
return
2020-11-26 10:39:54 +13:00
}
2021-03-15 02:44:32 +13:00
id, err := h.members.Insert(data.Username, data.Password, data.Profile)
2020-12-06 11:05:46 +13:00
if err != nil {
if errors.Is(err, types.ErrMemberAlreadyExists) {
utils.HttpUnprocessableEntity(w, err)
} else {
utils.HttpInternalServerError(w, err)
}
2020-12-06 11:05:46 +13:00
return
}
2021-03-15 08:26:58 +13:00
utils.HttpSuccess(w, MemberDataPayload{
ID: id,
Profile: data.Profile,
2020-11-22 12:06:55 +13:00
})
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2021-03-15 02:44:32 +13:00
profile := member.Profile
2020-11-22 11:39:24 +13:00
2020-12-09 03:37:48 +13:00
utils.HttpSuccess(w, profile)
2020-11-22 11:39:24 +13:00
}
2021-03-15 02:44:32 +13:00
func (h *MembersHandler) membersUpdateProfile(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2021-03-15 02:44:32 +13:00
profile := member.Profile
2020-12-07 09:10:45 +13:00
2020-12-09 03:14:20 +13:00
if !utils.HttpJsonRequest(w, r, &profile) {
2020-12-09 02:13:12 +13:00
return
2020-12-07 09:10:45 +13:00
}
2021-03-15 02:44:32 +13:00
if err := h.members.UpdateProfile(member.ID, profile); err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w)
}
func (h *MembersHandler) membersUpdatePassword(w http.ResponseWriter, r *http.Request) {
member := GetMember(r)
data := MemberPasswordPayload{}
if !utils.HttpJsonRequest(w, r, &data) {
return
}
if err := h.members.UpdatePassword(member.ID, data.Password); err != nil {
2020-12-06 10:18:45 +13:00
utils.HttpInternalServerError(w, err)
return
}
2020-12-03 05:43:34 +13:00
utils.HttpSuccess(w)
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2021-03-15 02:44:32 +13:00
if err := h.members.Delete(member.ID); err != nil {
2020-11-29 03:00:21 +13:00
utils.HttpInternalServerError(w, err)
2020-11-26 10:39:54 +13:00
return
}
utils.HttpSuccess(w)
2020-11-22 11:39:24 +13:00
}