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>
80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
package favhandler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
favlogic "spark-server/internal/module/favorite/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/transport/http/respond"
|
|
)
|
|
|
|
// Handler is the favorites HTTP controller.
|
|
type Handler struct {
|
|
favs *favlogic.Service
|
|
}
|
|
|
|
func New(favs *favlogic.Service) *Handler {
|
|
return &Handler{favs: favs}
|
|
}
|
|
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in favlogic.CreateInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.favs.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.favs.List(c.Request.Context(), uid, limit)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) Delete(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
respond.JSONError(c, errs.ErrInvalidArgument)
|
|
return
|
|
}
|
|
if err := h.favs.Delete(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
|
|
}
|