✨ add comment and reply framework
This commit is contained in:
@@ -3,6 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"schisandra-cloud-album/api/captcha_api"
|
"schisandra-cloud-album/api/captcha_api"
|
||||||
"schisandra-cloud-album/api/client_api"
|
"schisandra-cloud-album/api/client_api"
|
||||||
|
"schisandra-cloud-album/api/comment_api"
|
||||||
"schisandra-cloud-album/api/oauth_api"
|
"schisandra-cloud-album/api/oauth_api"
|
||||||
"schisandra-cloud-album/api/permission_api"
|
"schisandra-cloud-album/api/permission_api"
|
||||||
"schisandra-cloud-album/api/role_api"
|
"schisandra-cloud-album/api/role_api"
|
||||||
@@ -21,6 +22,7 @@ type Apis struct {
|
|||||||
RoleApi role_api.RoleAPI
|
RoleApi role_api.RoleAPI
|
||||||
PermissionApi permission_api.PermissionAPI
|
PermissionApi permission_api.PermissionAPI
|
||||||
ClientApi client_api.ClientAPI
|
ClientApi client_api.ClientAPI
|
||||||
|
CommonApi comment_api.CommentAPI
|
||||||
}
|
}
|
||||||
|
|
||||||
// Api new函数实例化,实例化完成后会返回结构体地指针类型
|
// Api new函数实例化,实例化完成后会返回结构体地指针类型
|
||||||
|
@@ -1,3 +1,7 @@
|
|||||||
package comment_api
|
package comment_api
|
||||||
|
|
||||||
|
import "schisandra-cloud-album/service"
|
||||||
|
|
||||||
type CommentAPI struct{}
|
type CommentAPI struct{}
|
||||||
|
|
||||||
|
var commentReplyService = service.Service.CommentReplyService
|
||||||
|
@@ -1 +1,28 @@
|
|||||||
package comment_api
|
package comment_api
|
||||||
|
|
||||||
|
import (
|
||||||
|
ginI18n "github.com/gin-contrib/i18n"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"schisandra-cloud-album/api/comment_api/dto"
|
||||||
|
"schisandra-cloud-album/common/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommentSubmit 提交评论
|
||||||
|
func (CommentAPI) CommentSubmit(c *gin.Context) {
|
||||||
|
commentRequest := dto.CommentRequest{}
|
||||||
|
err := c.ShouldBindJSON(&commentRequest)
|
||||||
|
if err != nil {
|
||||||
|
result.FailWithMessage(ginI18n.MustGetMessage(c, "ParamsError"), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
content := commentRequest.Content
|
||||||
|
images := commentRequest.Images
|
||||||
|
if content == "" {
|
||||||
|
result.FailWithMessage(ginI18n.MustGetMessage(c, "RequestParamsNotEmpty"), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(images) > 5 {
|
||||||
|
result.FailWithMessage(ginI18n.MustGetMessage(c, "TooManyImages"), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
6
api/comment_api/dto/request_dto.go
Normal file
6
api/comment_api/dto/request_dto.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type CommentRequest struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
Images []string `json:"images"`
|
||||||
|
}
|
@@ -63,3 +63,5 @@ RequestLimit = "request limit!"
|
|||||||
404NotFound = "404 not found!"
|
404NotFound = "404 not found!"
|
||||||
PermissionVerifyFailed = "permission verify failed!"
|
PermissionVerifyFailed = "permission verify failed!"
|
||||||
IllegalRequests = "illegal requests!"
|
IllegalRequests = "illegal requests!"
|
||||||
|
RequestParamsNotEmpty = "request params can not be empty!"
|
||||||
|
TooManyImages = "too many images!"
|
@@ -63,3 +63,5 @@ RequestLimit = "请求频率过高!"
|
|||||||
404NotFound = "未找到资源!"
|
404NotFound = "未找到资源!"
|
||||||
PermissionVerifyFailed = "权限验证失败!"
|
PermissionVerifyFailed = "权限验证失败!"
|
||||||
IllegalRequests = "非法请求!"
|
IllegalRequests = "非法请求!"
|
||||||
|
RequestParamsNotEmpty = "请求参数不能为空!"
|
||||||
|
TooManyImages = "最多只能上传5张图片!"
|
12
router/modules/comment_router.go
Normal file
12
router/modules/comment_router.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package modules
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"schisandra-cloud-album/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
var commonApi = api.Api.CommonApi
|
||||||
|
|
||||||
|
func CommentRouter(router *gin.RouterGroup) {
|
||||||
|
router.POST("/auth/comment/submit", commonApi.CommentSubmit)
|
||||||
|
}
|
@@ -2,9 +2,7 @@ package router
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-contrib/cors"
|
"github.com/gin-contrib/cors"
|
||||||
ginI18n "github.com/gin-contrib/i18n"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"schisandra-cloud-album/common/result"
|
|
||||||
"schisandra-cloud-album/global"
|
"schisandra-cloud-album/global"
|
||||||
"schisandra-cloud-album/middleware"
|
"schisandra-cloud-album/middleware"
|
||||||
"schisandra-cloud-album/router/modules"
|
"schisandra-cloud-album/router/modules"
|
||||||
@@ -14,8 +12,6 @@ import (
|
|||||||
func InitRouter() *gin.Engine {
|
func InitRouter() *gin.Engine {
|
||||||
gin.SetMode(global.CONFIG.System.Env)
|
gin.SetMode(global.CONFIG.System.Env)
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.NoRoute(HandleNotFound)
|
|
||||||
router.NoMethod(HandleNotFound)
|
|
||||||
err := router.SetTrustedProxies([]string{global.CONFIG.System.Ip})
|
err := router.SetTrustedProxies([]string{global.CONFIG.System.Ip})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.LOG.Error(err)
|
global.LOG.Error(err)
|
||||||
@@ -33,7 +29,7 @@ func InitRouter() *gin.Engine {
|
|||||||
// 国际化设置
|
// 国际化设置
|
||||||
router.Use(middleware.I18n())
|
router.Use(middleware.I18n())
|
||||||
|
|
||||||
noMiddlewareRouter := router.Group("api")
|
noMiddlewareRouter := router.Group("api") // 不需要中间件的路由组
|
||||||
{
|
{
|
||||||
modules.WebsocketRouter(noMiddlewareRouter) // 注册websocket路由
|
modules.WebsocketRouter(noMiddlewareRouter) // 注册websocket路由
|
||||||
modules.OauthRouter(noMiddlewareRouter) // 注册oauth路由
|
modules.OauthRouter(noMiddlewareRouter) // 注册oauth路由
|
||||||
@@ -48,24 +44,19 @@ func InitRouter() *gin.Engine {
|
|||||||
modules.SmsRouter(publicGroup) // 注册短信验证码路由
|
modules.SmsRouter(publicGroup) // 注册短信验证码路由
|
||||||
modules.UserRouter(publicGroup) // 注册鉴权路由
|
modules.UserRouter(publicGroup) // 注册鉴权路由
|
||||||
}
|
}
|
||||||
authGroup := router.Group("api") // 需要鉴权的路由组
|
|
||||||
|
|
||||||
|
authGroup := router.Group("api") // 需要鉴权的路由组
|
||||||
authGroup.Use(
|
authGroup.Use(
|
||||||
|
middleware.SecurityHeaders(),
|
||||||
middleware.JWTAuthMiddleware(),
|
middleware.JWTAuthMiddleware(),
|
||||||
middleware.CasbinMiddleware(),
|
middleware.CasbinMiddleware(),
|
||||||
middleware.SecurityHeaders(),
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
modules.UserRouterAuth(authGroup) // 注册鉴权路由
|
modules.UserRouterAuth(authGroup) // 注册鉴权路由
|
||||||
modules.RoleRouter(authGroup) // 注册角色路由
|
modules.RoleRouter(authGroup) // 注册角色路由
|
||||||
modules.PermissionRouter(authGroup) // 注册权限路由
|
modules.PermissionRouter(authGroup) // 注册权限路由
|
||||||
|
modules.CommentRouter(authGroup) // 注册评论路由
|
||||||
}
|
}
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleNotFound 404处理
|
|
||||||
func HandleNotFound(c *gin.Context) {
|
|
||||||
result.FailWithCodeAndMessage(404, ginI18n.MustGetMessage(c, "404NotFound"), c)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
@@ -1 +1,14 @@
|
|||||||
package comment_reply_service
|
package comment_reply_service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
|
"schisandra-cloud-album/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CreateCommentReply 创建评论
|
||||||
|
func (CommentReplyService) CreateCommentReply(comment *model.ScaCommentReply) error {
|
||||||
|
if err := global.DB.Create(&comment).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"schisandra-cloud-album/service/comment_reply_service"
|
||||||
"schisandra-cloud-album/service/permission_service"
|
"schisandra-cloud-album/service/permission_service"
|
||||||
"schisandra-cloud-album/service/role_service"
|
"schisandra-cloud-album/service/role_service"
|
||||||
"schisandra-cloud-album/service/user_device_service"
|
"schisandra-cloud-album/service/user_device_service"
|
||||||
@@ -15,6 +16,7 @@ type Services struct {
|
|||||||
PermissionService permission_service.PermissionService
|
PermissionService permission_service.PermissionService
|
||||||
UserSocialService user_social_service.UserSocialService
|
UserSocialService user_social_service.UserSocialService
|
||||||
UserDeviceService user_device_service.UserDeviceService
|
UserDeviceService user_device_service.UserDeviceService
|
||||||
|
CommentReplyService comment_reply_service.CommentReplyService
|
||||||
}
|
}
|
||||||
|
|
||||||
// Service new函数实例化,实例化完成后会返回结构体地指针类型
|
// Service new函数实例化,实例化完成后会返回结构体地指针类型
|
||||||
|
@@ -89,7 +89,6 @@ func ParseAccessToken(tokenString string) (*AccessJWTPayload, bool, error) {
|
|||||||
return MySecret, nil
|
return MySecret, nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.LOG.Error(err)
|
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
if claims, ok := token.Claims.(*AccessJWTClaims); ok && token.Valid {
|
if claims, ok := token.Claims.(*AccessJWTClaims); ok && token.Valid {
|
||||||
|
Reference in New Issue
Block a user