spark-server/internal/transport/http/handler/group/handler.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

129 lines
2.5 KiB
Go

package grouphandler
import (
"strconv"
"github.com/gin-gonic/gin"
grouplogic "spark-server/internal/module/group/logic"
"spark-server/internal/pkg/errs"
"spark-server/internal/pkg/reqctx"
"spark-server/internal/transport/http/respond"
)
// Handler is the groups HTTP controller.
type Handler struct {
groups *grouplogic.Service
}
func New(groups *grouplogic.Service) *Handler {
return &Handler{groups: groups}
}
func (h *Handler) Create(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
var in grouplogic.CreateInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.groups.Create(c.Request.Context(), uid, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) List(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
limit, _ := strconv.Atoi(c.Query("limit"))
out, err := h.groups.List(c.Request.Context(), uid, limit)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Get(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, err := pathID(c)
if err != nil {
respond.JSONError(c, err)
return
}
out, err := h.groups.Get(c.Request.Context(), uid, id)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Update(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, err := pathID(c)
if err != nil {
respond.JSONError(c, err)
return
}
var in grouplogic.UpdateInput
if err := respond.BindJSON(c, &in); err != nil {
respond.JSONError(c, err)
return
}
out, err := h.groups.Update(c.Request.Context(), uid, id, &in)
if err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, out)
}
func (h *Handler) Dissolve(c *gin.Context) {
uid, ok := currentUID(c)
if !ok {
return
}
id, err := pathID(c)
if err != nil {
respond.JSONError(c, err)
return
}
if err := h.groups.Dissolve(c.Request.Context(), uid, id); err != nil {
respond.JSONError(c, err)
return
}
respond.JSON(c, gin.H{})
}
func currentUID(c *gin.Context) (int64, bool) {
rc := reqctx.From(c.Request.Context())
if rc == nil || rc.UserID <= 0 {
respond.JSONError(c, errs.ErrUnauthorized)
return 0, false
}
return rc.UserID, true
}
func pathID(c *gin.Context) (int64, error) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil || id <= 0 {
return 0, errs.ErrInvalidArgument
}
return id, nil
}