Files
ds-galo/backend/internal/members/handlers.go
T
iu 50dff63ece feat: rename service to galo, add halo branding and release build
- rename Go module, binary, systemd unit, PG_DATABASE default, frontend package and docs from shared-album to galo
- add theme-aware halo emblem and favicon (new ui-branding capability)
- add make build-release (versioned, stripped, static binary) and run the binary from the repo root
2026-10-11 11:36:56 +03:00

107 lines
2.6 KiB
Go

package members
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"galo/internal/auth"
"galo/internal/httpx"
)
type Handler struct {
svc *Service
}
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
func (h *Handler) List(c *gin.Context) {
list, err := h.svc.List(c.Request.Context(), c.Param("id"), auth.UserID(c))
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"members": list})
}
type inviteRequest struct {
Role string `json:"role" binding:"required"`
ExpiresIn string `json:"expires_in"`
MaxUses int `json:"max_uses"`
}
func (h *Handler) CreateInvite(c *gin.Context) {
var req inviteRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpx.RespondError(c, httpx.BadRequest("invalid_request", "role is required"))
return
}
var ttl time.Duration
if req.ExpiresIn != "" {
d, err := time.ParseDuration(req.ExpiresIn)
if err != nil {
httpx.RespondError(c, httpx.BadRequest("invalid_expires_in", "invalid expires_in duration"))
return
}
ttl = d
}
inv, err := h.svc.CreateInvite(c.Request.Context(), c.Param("id"), auth.UserID(c), CreateInviteInput{
Role: req.Role,
TTL: ttl,
MaxUses: req.MaxUses,
})
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusCreated, gin.H{"invite": inv})
}
func (h *Handler) AcceptInvite(c *gin.Context) {
inv, err := h.svc.Accept(c.Request.Context(), c.Param("token"), auth.UserID(c))
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"album_id": inv.AlbumID, "role": inv.Role})
}
func (h *Handler) InviteInfo(c *gin.Context) {
inv, err := h.svc.InviteInfo(c.Request.Context(), c.Param("token"))
if err != nil {
httpx.RespondError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"invite": gin.H{
"album_id": inv.AlbumID,
"role": inv.Role,
"expires_at": inv.ExpiresAt,
}})
}
type roleRequest struct {
Role string `json:"role" binding:"required"`
}
func (h *Handler) ChangeRole(c *gin.Context) {
var req roleRequest
if err := c.ShouldBindJSON(&req); err != nil {
httpx.RespondError(c, httpx.BadRequest("invalid_request", "role is required"))
return
}
if err := h.svc.ChangeRole(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Param("user_id"), req.Role); err != nil {
httpx.RespondError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (h *Handler) Remove(c *gin.Context) {
if err := h.svc.Remove(c.Request.Context(), c.Param("id"), auth.UserID(c), c.Param("user_id")); err != nil {
httpx.RespondError(c, err)
return
}
c.Status(http.StatusNoContent)
}