🎨 remove xorm & add gorm gen

This commit is contained in:
landaiqing
2024-11-21 13:03:47 +08:00
parent 9b5e454eca
commit f32ac241a6
84 changed files with 8865 additions and 3476 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/mssola/useragent"
"github.com/zeromicro/go-zero/core/logx"
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
"schisandra-album-cloud-microservices/app/core/api/common/constant"
"schisandra-album-cloud-microservices/app/core/api/common/response"
@@ -15,8 +17,6 @@ import (
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
"schisandra-album-cloud-microservices/app/core/api/internal/types"
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitCommentLogic struct {
@@ -57,7 +57,7 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
browser, _ := ua.Browser()
operatingSystem := ua.OS()
isAuthor := 0
var isAuthor int64 = 0
session, err := l.svcCtx.Session.Get(r, constant.SESSION_KEY)
if err == nil {
return nil, err
@@ -74,26 +74,25 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
}
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
comment := model.ScaCommentReply{
topicType := constant.CommentTopicType
commentType := constant.COMMENT
comment := &model.ScaCommentReply{
Content: commentContent,
UserId: uid,
TopicId: req.TopicId,
TopicType: constant.CommentTopicType,
CommentType: constant.COMMENT,
UserID: uid,
TopicID: req.TopicId,
TopicType: topicType,
CommentType: commentType,
Author: isAuthor,
CommentIp: ip,
CommentIP: ip,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
Agent: userAgent,
}
affected, err := l.svcCtx.DB.InsertOne(&comment)
err = l.svcCtx.DB.ScaCommentReply.Create(comment)
if err != nil {
return nil, err
}
if affected == 0 {
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
}
if len(req.Images) > 0 {
imagesData, err := utils.ProcessImages(req.Images)
@@ -103,16 +102,16 @@ func (l *SubmitCommentLogic) SubmitComment(r *http.Request, req *types.CommentRe
commentImages := types.CommentImages{
UserId: uid,
TopicId: req.TopicId,
CommentId: comment.Id,
CommentId: comment.ID,
Images: imagesData,
CreatedAt: comment.CreatedAt.String(),
}
if _, err = l.svcCtx.MongoClient.Collection("comment_images").InsertOne(l.ctx, commentImages); err != nil {
if _, err = l.svcCtx.MongoClient.Collection(constant.COMMENT_IMAGES).InsertOne(l.ctx, commentImages); err != nil {
return nil, err
}
}
commentResponse := types.CommentResponse{
Id: comment.Id,
Id: comment.ID,
Content: commentContent,
UserId: uid,
TopicId: req.TopicId,

View File

@@ -8,6 +8,8 @@ import (
"github.com/mssola/useragent"
"github.com/zeromicro/go-zero/core/logx"
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
"schisandra-album-cloud-microservices/app/core/api/common/constant"
"schisandra-album-cloud-microservices/app/core/api/common/response"
@@ -15,8 +17,6 @@ import (
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
"schisandra-album-cloud-microservices/app/core/api/internal/types"
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitReplyCommentLogic struct {
@@ -65,7 +65,7 @@ func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types
if !ok {
return nil, errors.New("uid not found in session")
}
isAuthor := 0
var isAuthor int64 = 0
if uid == req.Author {
isAuthor = 1
}
@@ -76,39 +76,34 @@ func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types
}
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
tx := l.svcCtx.DB.NewSession()
defer tx.Close()
if err = tx.Begin(); err != nil {
return nil, err
}
reply := model.ScaCommentReply{
tx := l.svcCtx.DB.Begin()
topicType := constant.CommentTopicType
commentType := constant.REPLY
reply := &model.ScaCommentReply{
Content: commentContent,
UserId: uid,
TopicId: req.TopicId,
TopicType: constant.CommentTopicType,
CommentType: constant.COMMENT,
UserID: uid,
TopicID: req.TopicId,
TopicType: topicType,
CommentType: commentType,
Author: isAuthor,
CommentIp: ip,
CommentIP: ip,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
Agent: userAgent,
ReplyId: req.ReplyId,
ReplyID: req.ReplyId,
ReplyUser: req.ReplyUser,
}
affected, err := tx.Insert(&reply)
err = tx.ScaCommentReply.Create(reply)
if err != nil {
return nil, err
}
if affected == 0 {
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
}
update, err := tx.Table(model.ScaCommentReply{}).Where("id = ? and deleted = 0", req.ReplyId).Incr("reply_count", 1).Update(nil)
commentReply := l.svcCtx.DB.ScaCommentReply
update, err := tx.ScaCommentReply.Updates(commentReply.ReplyCount.Add(1))
if err != nil {
return nil, err
}
if update == 0 {
if update.RowsAffected == 0 {
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
}
@@ -121,26 +116,26 @@ func (l *SubmitReplyCommentLogic) SubmitReplyComment(r *http.Request, req *types
commentImages := types.CommentImages{
UserId: uid,
TopicId: req.TopicId,
CommentId: reply.Id,
CommentId: reply.ID,
Images: imagesData,
CreatedAt: reply.CreatedAt.String(),
}
if _, err = l.svcCtx.MongoClient.Collection("comment_images").InsertOne(l.ctx, commentImages); err != nil {
if _, err = l.svcCtx.MongoClient.Collection(constant.COMMENT_IMAGES).InsertOne(l.ctx, commentImages); err != nil {
return nil, err
}
}
commentResponse := types.CommentResponse{
Id: reply.Id,
Id: reply.ID,
Content: commentContent,
UserId: uid,
TopicId: reply.TopicId,
TopicId: reply.TopicID,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now(),
ReplyId: reply.ReplyId,
ReplyId: reply.ReplyID,
ReplyUser: reply.ReplyUser,
}
err = tx.Commit()

View File

@@ -8,6 +8,8 @@ import (
"github.com/mssola/useragent"
"github.com/zeromicro/go-zero/core/logx"
"schisandra-album-cloud-microservices/app/core/api/common/captcha/verify"
"schisandra-album-cloud-microservices/app/core/api/common/constant"
"schisandra-album-cloud-microservices/app/core/api/common/response"
@@ -15,8 +17,6 @@ import (
"schisandra-album-cloud-microservices/app/core/api/internal/svc"
"schisandra-album-cloud-microservices/app/core/api/internal/types"
"schisandra-album-cloud-microservices/app/core/api/repository/mysql/model"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitReplyReplyLogic struct {
@@ -74,7 +74,7 @@ func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.Rep
}
// 判断作者身份
isAuthor := 0
var isAuthor int64 = 0
if uid == req.Author {
isAuthor = 1
}
@@ -86,40 +86,35 @@ func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.Rep
}
commentContent := l.svcCtx.Sensitive.Replace(xssFilterContent, '*')
tx := l.svcCtx.DB.NewSession()
defer tx.Close()
if err = tx.Begin(); err != nil {
return nil, err
}
replyReply := model.ScaCommentReply{
tx := l.svcCtx.DB.Begin()
topicType := constant.CommentTopicType
commentType := constant.REPLY
replyReply := &model.ScaCommentReply{
Content: commentContent,
UserId: uid,
TopicId: req.TopicId,
TopicType: constant.CommentTopicType,
CommentType: constant.COMMENT,
UserID: uid,
TopicID: req.TopicId,
TopicType: topicType,
CommentType: commentType,
Author: isAuthor,
CommentIp: ip,
CommentIP: ip,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
Agent: userAgent,
ReplyId: req.ReplyId,
ReplyID: req.ReplyId,
ReplyUser: req.ReplyUser,
ReplyTo: req.ReplyTo,
}
affected, err := tx.Insert(&replyReply)
err = tx.ScaCommentReply.Create(replyReply)
if err != nil {
return nil, err
}
if affected == 0 {
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
}
update, err := tx.Table(model.ScaCommentReply{}).Where("id = ? and version = ? and deleted = 0", req.ReplyId, replyReply.Version).Incr("reply_count", 1).Update(nil)
commentReply := l.svcCtx.DB.ScaCommentReply
update, err := tx.ScaCommentReply.Updates(commentReply.ReplyCount.Add(1))
if err != nil {
return nil, err
}
if update == 0 {
if update.RowsAffected == 0 {
return response.ErrorWithI18n(l.ctx, "comment.commentError"), nil
}
@@ -132,27 +127,27 @@ func (l *SubmitReplyReplyLogic) SubmitReplyReply(r *http.Request, req *types.Rep
commentImages := types.CommentImages{
UserId: uid,
TopicId: req.TopicId,
CommentId: replyReply.Id,
CommentId: replyReply.ID,
Images: imagesData,
CreatedAt: replyReply.CreatedAt.String(),
}
if _, err = l.svcCtx.MongoClient.Collection("comment_images").InsertOne(l.ctx, commentImages); err != nil {
if _, err = l.svcCtx.MongoClient.Collection(constant.COMMENT_IMAGES).InsertOne(l.ctx, commentImages); err != nil {
return nil, err
}
}
// 构建响应
commentResponse := types.CommentResponse{
Id: replyReply.Id,
Id: replyReply.ID,
Content: commentContent,
UserId: uid,
TopicId: replyReply.TopicId,
TopicId: replyReply.TopicID,
Author: isAuthor,
Location: location,
Browser: browser,
OperatingSystem: operatingSystem,
CreatedTime: time.Now(),
ReplyId: replyReply.ReplyId,
ReplyId: replyReply.ReplyID,
ReplyUser: replyReply.ReplyUser,
ReplyTo: replyReply.ReplyTo,
}