246 lines
4.6 KiB
Go
246 lines
4.6 KiB
Go
package squarehandler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
squarelogic "spark-server/internal/module/square/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/transport/http/respond"
|
|
)
|
|
|
|
// Handler is the square HTTP controller.
|
|
type Handler struct {
|
|
square *squarelogic.Service
|
|
}
|
|
|
|
func New(square *squarelogic.Service) *Handler {
|
|
return &Handler{square: square}
|
|
}
|
|
|
|
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 pathInt(c *gin.Context, name string) (int64, bool) {
|
|
id, err := strconv.ParseInt(c.Param(name), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
respond.JSONError(c, errs.ErrInvalidArgument)
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
func queryInt(c *gin.Context, name string) int64 {
|
|
n, _ := strconv.ParseInt(c.Query(name), 10, 64)
|
|
return n
|
|
}
|
|
|
|
func (h *Handler) Feed(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.Feed(c.Request.Context(), uid, queryInt(c, "cursor"), int(queryInt(c, "limit")))
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) Publish(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in squarelogic.PublishInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.square.Publish(c.Request.Context(), uid, &in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) UserPosts(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
owner, ok := pathInt(c, "uid")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.UserPosts(c.Request.Context(), uid, owner, queryInt(c, "cursor"), int(queryInt(c, "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, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.Get(c.Request.Context(), uid, id)
|
|
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, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.square.Delete(c.Request.Context(), uid, id); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, gin.H{})
|
|
}
|
|
|
|
func (h *Handler) Like(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.Like(c.Request.Context(), uid, id)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) Unlike(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.Unlike(c.Request.Context(), uid, id)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) CreateComment(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
id, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
var in squarelogic.CommentInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
out, err := h.square.Comment(c.Request.Context(), uid, id, &in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) DeleteComment(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
postID, ok := pathInt(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
cid, ok := pathInt(c, "cid")
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.DeleteComment(c.Request.Context(), uid, postID, cid)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) ListNotices(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.ListNotices(c.Request.Context(), uid, queryInt(c, "cursor"), int(queryInt(c, "limit")))
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) NoticeUnread(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.square.NoticeUnread(c.Request.Context(), uid)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|
|
|
|
func (h *Handler) ReadNotices(c *gin.Context) {
|
|
uid, ok := currentUID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var in squarelogic.NoticeReadInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
if err := h.square.ReadNotices(c.Request.Context(), uid, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, gin.H{})
|
|
}
|