Release / release-apk (push) Successful in 9m25s
Release / release-docker (push) Successful in 33s
CI / docker (push) Successful in 7s
CI / lint-backend (push) Successful in 1m29s
CI / lint-mobile (push) Failing after 7m46s
CI / test-backend (push) Successful in 1m30s
CI / test-mobile (push) Successful in 9m27s
CI / build-backend (push) Successful in 47s
144 lines
3.6 KiB
Go
144 lines
3.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/kazier/backend/pkg/api"
|
|
"github.com/kazier/backend/service"
|
|
)
|
|
|
|
// groupIDParam validates a 32-hex group id path param (404, never 400 —
|
|
// group ids are unguessable, no enumeration).
|
|
func groupIDParam(c *gin.Context, name string) (string, bool) {
|
|
id := c.Param(name)
|
|
if !deviceIDPattern.MatchString(id) {
|
|
api.Error(c, http.StatusNotFound, "NOT_FOUND", "group not found")
|
|
return "", false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
// GroupsCreate stores an owned group : POST /groups {name}.
|
|
func GroupsCreate(c *gin.Context) {
|
|
if Store == nil {
|
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
var body struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
|
|
return
|
|
}
|
|
dto, err := Store.CreateGroup(c.GetString(UserIDKey), body.Name)
|
|
if err != nil {
|
|
if errors.Is(err, service.ErrInvalidUpload) {
|
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid group name")
|
|
return
|
|
}
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
api.OK(c, dto)
|
|
}
|
|
|
|
// GroupsList returns owned + joined groups : GET /groups.
|
|
func GroupsList(c *gin.Context) {
|
|
if Store == nil {
|
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
dtos, err := Store.ListGroups(c.GetString(UserIDKey))
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
api.OK(c, dtos)
|
|
}
|
|
|
|
// GroupMembersAdd adds an existing user : POST /groups/:id/members {userId}.
|
|
func GroupMembersAdd(c *gin.Context) {
|
|
if Store == nil {
|
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
groupID, ok := groupIDParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
var body struct {
|
|
UserID string `json:"userId"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil || !deviceIDPattern.MatchString(body.UserID) {
|
|
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `userId`")
|
|
return
|
|
}
|
|
if err := Store.AddGroupMember(c.GetString(UserIDKey), groupID, body.UserID); err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
api.OK(c, gin.H{"id": groupID})
|
|
}
|
|
|
|
// GroupMembersRemove removes a membership :
|
|
// DELETE /groups/:id/members/:userId.
|
|
func GroupMembersRemove(c *gin.Context) {
|
|
if Store == nil {
|
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
groupID, ok := groupIDParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
memberID, ok := groupIDParam(c, "userId")
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := Store.RemoveGroupMember(c.GetString(UserIDKey), groupID, memberID); err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
api.OK(c, gin.H{"id": groupID})
|
|
}
|
|
|
|
// GroupsDelete removes an owned group : DELETE /groups/:id.
|
|
func GroupsDelete(c *gin.Context) {
|
|
if Store == nil {
|
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
groupID, ok := groupIDParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := Store.DeleteGroup(c.GetString(UserIDKey), groupID); err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
api.OK(c, gin.H{"id": groupID})
|
|
}
|
|
|
|
// GroupMembersList returns memberships of an owned group :
|
|
// GET /groups/:id/members.
|
|
func GroupMembersList(c *gin.Context) {
|
|
if Store == nil {
|
|
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
|
|
return
|
|
}
|
|
groupID, ok := groupIDParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
members, err := Store.ListMembers(c.GetString(UserIDKey), groupID)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
api.OK(c, members)
|
|
}
|