You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
paopao-ce/internal/servants/web/routers/api/post.go

334 lines
7.6 KiB

// Copyright 2022 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
3 years ago
package api
import (
"github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/core"
"github.com/rocboss/paopao-ce/internal/servants/web/broker"
"github.com/rocboss/paopao-ce/pkg/app"
"github.com/rocboss/paopao-ce/pkg/convert"
"github.com/rocboss/paopao-ce/pkg/errcode"
"github.com/sirupsen/logrus"
3 years ago
)
func GetPostList(c *gin.Context) {
response := app.NewResponse(c)
q := &core.QueryReq{
3 years ago
Query: c.Query("query"),
Type: "search",
}
if c.Query("type") == "tag" {
q.Type = "tag"
}
user, _ := userFrom(c)
offset, limit := app.GetPageOffset(c)
3 years ago
if q.Query == "" && q.Type == "search" {
resp, err := broker.GetIndexPosts(user, offset, limit)
3 years ago
if err != nil {
logrus.Errorf("service.GetPostList err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.GetPostsFailed)
return
}
response.ToResponseList(resp.Tweets, resp.Total)
3 years ago
} else {
posts, totalRows, err := broker.GetPostListFromSearch(user, q, offset, limit)
3 years ago
if err != nil {
logrus.Errorf("service.GetPostListFromSearch err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.GetPostsFailed)
return
}
response.ToResponseList(posts, totalRows)
}
}
func GetPost(c *gin.Context) {
postID := convert.StrTo(c.Query("id")).MustInt64()
response := app.NewResponse(c)
postFormated, err := broker.GetPost(postID)
3 years ago
if err != nil {
logrus.Errorf("service.GetPost err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.GetPostFailed)
return
}
response.ToResponse(postFormated)
}
func CreatePost(c *gin.Context) {
param := broker.PostCreationReq{}
3 years ago
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
3 years ago
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
userID, _ := c.Get("UID")
post, err := broker.CreatePost(c, userID.(int64), param)
3 years ago
if err != nil {
logrus.Errorf("service.CreatePost err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.CreatePostFailed)
return
}
response.ToResponse(post)
}
func DeletePost(c *gin.Context) {
param := broker.PostDelReq{}
3 years ago
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
3 years ago
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
user, exist := userFrom(c)
if !exist {
3 years ago
response.ToErrorResponse(errcode.NoPermission)
return
}
err := broker.DeletePost(user, param.ID)
3 years ago
if err != nil {
logrus.Errorf("service.DeletePost err: %v\n", err)
response.ToErrorResponse(err)
3 years ago
return
}
response.ToResponse(nil)
}
func GetPostStar(c *gin.Context) {
postID := convert.StrTo(c.Query("id")).MustInt64()
response := app.NewResponse(c)
userID, _ := c.Get("UID")
_, err := broker.GetPostStar(postID, userID.(int64))
3 years ago
if err != nil {
response.ToResponse(gin.H{
"status": false,
})
return
}
response.ToResponse(gin.H{
"status": true,
})
}
func PostStar(c *gin.Context) {
param := broker.PostStarReq{}
3 years ago
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
3 years ago
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
userID, _ := c.Get("UID")
status := false
star, err := broker.GetPostStar(param.ID, userID.(int64))
3 years ago
if err != nil {
// 创建Star
_, err = broker.CreatePostStar(param.ID, userID.(int64))
3 years ago
status = true
} else {
// 取消Star
err = broker.DeletePostStar(star)
}
if err != nil {
response.ToErrorResponse(errcode.NoPermission)
return
3 years ago
}
response.ToResponse(gin.H{
"status": status,
})
}
func GetPostCollection(c *gin.Context) {
postID := convert.StrTo(c.Query("id")).MustInt64()
response := app.NewResponse(c)
userID, _ := c.Get("UID")
_, err := broker.GetPostCollection(postID, userID.(int64))
3 years ago
if err != nil {
response.ToResponse(gin.H{
"status": false,
})
return
}
response.ToResponse(gin.H{
"status": true,
})
}
func PostCollection(c *gin.Context) {
param := broker.PostCollectionReq{}
3 years ago
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
3 years ago
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
userID, _ := c.Get("UID")
status := false
collection, err := broker.GetPostCollection(param.ID, userID.(int64))
3 years ago
if err != nil {
// 创建collection
_, err = broker.CreatePostCollection(param.ID, userID.(int64))
3 years ago
status = true
} else {
// 取消Star
err = broker.DeletePostCollection(collection)
}
if err != nil {
response.ToErrorResponse(errcode.NoPermission)
return
3 years ago
}
response.ToResponse(gin.H{
"status": status,
})
}
func LockPost(c *gin.Context) {
param := broker.PostLockReq{}
3 years ago
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
3 years ago
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
user, _ := c.Get("USER")
// 获取Post
postFormated, err := broker.GetPost(param.ID)
3 years ago
if err != nil {
logrus.Errorf("service.GetPost err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.GetPostFailed)
return
}
if postFormated.UserID != user.(*core.User).ID && !user.(*core.User).IsAdmin {
3 years ago
response.ToErrorResponse(errcode.NoPermission)
return
}
err = broker.LockPost(param.ID)
3 years ago
if err != nil {
logrus.Errorf("service.LockPost err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.LockPostFailed)
return
}
response.ToResponse(gin.H{
"lock_status": 1 - postFormated.IsLock,
})
}
func StickPost(c *gin.Context) {
param := broker.PostStickReq{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
user, _ := c.Get("USER")
// 获取Post
postFormated, err := broker.GetPost(param.ID)
if err != nil {
logrus.Errorf("service.GetPost err: %v\n", err)
response.ToErrorResponse(errcode.GetPostFailed)
return
}
if !user.(*core.User).IsAdmin {
response.ToErrorResponse(errcode.NoPermission)
return
}
err = broker.StickPost(param.ID)
if err != nil {
logrus.Errorf("service.StickPost err: %v\n", err)
response.ToErrorResponse(errcode.LockPostFailed)
return
}
response.ToResponse(gin.H{
"top_status": 1 - postFormated.IsTop,
})
}
func VisiblePost(c *gin.Context) {
param := broker.PostVisibilityReq{}
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
user, _ := userFrom(c)
if err := broker.VisiblePost(user, param.ID, param.Visibility); err != nil {
logrus.Errorf("service.VisiblePost err: %v\n", err)
response.ToErrorResponse(err)
return
}
response.ToResponse(gin.H{
"visibility": param.Visibility,
})
}
3 years ago
func GetPostTags(c *gin.Context) {
param := broker.PostTagsReq{}
3 years ago
response := app.NewResponse(c)
valid, errs := app.BindAndValid(c, &param)
if !valid {
logrus.Errorf("app.BindAndValid errs: %v", errs)
3 years ago
response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
return
}
tags, err := broker.GetPostTags(&param)
3 years ago
if err != nil {
logrus.Errorf("service.GetPostTags err: %v\n", err)
3 years ago
response.ToErrorResponse(errcode.GetPostTagsFailed)
return
}
response.ToResponse(tags)
}