From 4b4b35269800afd99fa0e71f5f2e7bf1c3731ae6 Mon Sep 17 00:00:00 2001 From: alimy Date: Sat, 4 Jun 2022 06:35:22 +0800 Subject: [PATCH] optimize: abstract backend process logic to service interface --- internal/core/cache.go | 6 +++ internal/core/core.go | 72 +++++++++++++++++++++++++ internal/core/search.go | 27 ++++++++++ internal/core/storage.go | 10 ++++ internal/core/wallet.go | 13 +++++ internal/dao/attachment.go | 2 +- internal/dao/comment.go | 16 +++--- internal/dao/comment_content.go | 6 +-- internal/dao/dao.go | 11 ++-- internal/dao/message.go | 12 ++--- internal/dao/post.go | 42 +++++++-------- internal/dao/post_content.go | 6 +-- internal/dao/search.go | 27 ++++------ internal/dao/tag.go | 6 +-- internal/dao/user.go | 22 ++++---- internal/dao/wallet.go | 8 +-- internal/routers/api/post.go | 4 +- internal/service/attachment.go | 2 +- internal/service/comment.go | 66 +++++++++++------------ internal/service/message.go | 14 ++--- internal/service/post.go | 96 ++++++++++++++++----------------- internal/service/service.go | 5 +- internal/service/user.go | 40 +++++++------- internal/service/wallet.go | 10 ++-- 24 files changed, 324 insertions(+), 199 deletions(-) create mode 100644 internal/core/cache.go create mode 100644 internal/core/core.go create mode 100644 internal/core/search.go create mode 100644 internal/core/storage.go create mode 100644 internal/core/wallet.go diff --git a/internal/core/cache.go b/internal/core/cache.go new file mode 100644 index 00000000..a092f182 --- /dev/null +++ b/internal/core/cache.go @@ -0,0 +1,6 @@ +package core + +// CacheService cache service interface that implement base Redis or other +type CacheService interface { + // TODO +} diff --git a/internal/core/core.go b/internal/core/core.go new file mode 100644 index 00000000..15ba27f6 --- /dev/null +++ b/internal/core/core.go @@ -0,0 +1,72 @@ +package core + +import ( + "github.com/rocboss/paopao-ce/internal/model" +) + +// DataService data service interface that process data related logic on database +type DataService interface { + WalletService + SearchService + StorageService + + GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) + GetCommentByID(id int64) (*model.Comment, error) + DeleteComment(comment *model.Comment) error + GetCommentCount(conditions *model.ConditionsT) (int64, error) + CreateComment(comment *model.Comment) (*model.Comment, error) + CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error) + GetCommentReplyByID(id int64) (*model.CommentReply, error) + DeleteCommentReply(reply *model.CommentReply) error + GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error) + GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error) + CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error) + + CreateMessage(msg *model.Message) (*model.Message, error) + GetUnreadCount(userID int64) (int64, error) + GetMessageByID(id int64) (*model.Message, error) + ReadMessage(message *model.Message) error + GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error) + GetMessageCount(conditions *model.ConditionsT) (int64, error) + + CreatePost(post *model.Post) (*model.Post, error) + DeletePost(post *model.Post) error + LockPost(post *model.Post) error + StickPost(post *model.Post) error + GetPostByID(id int64) (*model.Post, error) + GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error) + GetPostCount(conditions *model.ConditionsT) (int64, error) + UpdatePost(post *model.Post) error + GetUserPostStar(postID, userID int64) (*model.PostStar, error) + GetUserPostStars(userID int64, offset, limit int) ([]*model.PostStar, error) + GetUserPostStarCount(userID int64) (int64, error) + CreatePostStar(postID, userID int64) (*model.PostStar, error) + DeletePostStar(p *model.PostStar) error + GetUserPostCollection(postID, userID int64) (*model.PostCollection, error) + GetUserPostCollections(userID int64, offset, limit int) ([]*model.PostCollection, error) + GetUserPostCollectionCount(userID int64) (int64, error) + GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error) + GetUserWalletBillCount(userID int64) (int64, error) + CreatePostCollection(postID, userID int64) (*model.PostCollection, error) + DeletePostCollection(p *model.PostCollection) error + GetPostAttatchmentBill(postID, userID int64) (*model.PostAttachmentBill, error) + CreatePostContent(content *model.PostContent) (*model.PostContent, error) + GetPostContentsByIDs(ids []int64) ([]*model.PostContent, error) + GetPostContentByID(id int64) (*model.PostContent, error) + + CreateTag(tag *model.Tag) (*model.Tag, error) + DeleteTag(tag *model.Tag) error + GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error) + + GetUserByID(id int64) (*model.User, error) + GetUserByUsername(username string) (*model.User, error) + GetUserByPhone(phone string) (*model.User, error) + GetUsersByIDs(ids []int64) ([]*model.User, error) + GetUsersByKeyword(keyword string) ([]*model.User, error) + GetTagsByKeyword(keyword string) ([]*model.Tag, error) + CreateUser(user *model.User) (*model.User, error) + UpdateUser(user *model.User) error + GetLatestPhoneCaptcha(phone string) (*model.Captcha, error) + UsePhoneCaptcha(captcha *model.Captcha) error + SendPhoneCaptcha(phone string) error +} diff --git a/internal/core/search.go b/internal/core/search.go new file mode 100644 index 00000000..b2033cd3 --- /dev/null +++ b/internal/core/search.go @@ -0,0 +1,27 @@ +package core + +import ( + "github.com/rocboss/paopao-ce/pkg/zinc" +) + +const ( + SearchTypeDefault SearchType = "search" + SearchTypeTag SearchType = "tag" +) + +type SearchType string + +type QueryT struct { + Query string + Type SearchType +} + +// SearchService search service interface that implement base zinc +type SearchService interface { + CreateSearchIndex(indexName string) + BulkPushDoc(data []map[string]interface{}) (bool, error) + DelDoc(indexName, id string) error + QueryAll(q *QueryT, indexName string, offset, limit int) (*zinc.QueryResultT, error) + QuerySearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) + QueryTagSearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) +} diff --git a/internal/core/storage.go b/internal/core/storage.go new file mode 100644 index 00000000..158c250b --- /dev/null +++ b/internal/core/storage.go @@ -0,0 +1,10 @@ +package core + +import ( + "github.com/rocboss/paopao-ce/internal/model" +) + +// StorageService storage service interface that implement base AliOSS、MINIO or other +type StorageService interface { + CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) +} diff --git a/internal/core/wallet.go b/internal/core/wallet.go new file mode 100644 index 00000000..e6cea7c9 --- /dev/null +++ b/internal/core/wallet.go @@ -0,0 +1,13 @@ +package core + +import ( + "github.com/rocboss/paopao-ce/internal/model" +) + +// WalletService wallet service interface +type WalletService interface { + GetRechargeByID(id int64) (*model.WalletRecharge, error) + CreateRecharge(userId, amount int64) (*model.WalletRecharge, error) + HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error + HandlePostAttachmentBought(post *model.Post, user *model.User) error +} diff --git a/internal/dao/attachment.go b/internal/dao/attachment.go index 41d1e825..43500d54 100644 --- a/internal/dao/attachment.go +++ b/internal/dao/attachment.go @@ -2,6 +2,6 @@ package dao import "github.com/rocboss/paopao-ce/internal/model" -func (d *Dao) CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) { +func (d *dataServant) CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) { return attachment.Create(d.engine) } diff --git a/internal/dao/comment.go b/internal/dao/comment.go index d190c0f1..f2b9714e 100644 --- a/internal/dao/comment.go +++ b/internal/dao/comment.go @@ -2,11 +2,11 @@ package dao import "github.com/rocboss/paopao-ce/internal/model" -func (d *Dao) GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) { +func (d *dataServant) GetComments(conditions *model.ConditionsT, offset, limit int) ([]*model.Comment, error) { return (&model.Comment{}).List(d.engine, conditions, offset, limit) } -func (d *Dao) GetCommentByID(id int64) (*model.Comment, error) { +func (d *dataServant) GetCommentByID(id int64) (*model.Comment, error) { comment := &model.Comment{ Model: &model.Model{ ID: id, @@ -15,23 +15,23 @@ func (d *Dao) GetCommentByID(id int64) (*model.Comment, error) { return comment.Get(d.engine) } -func (d *Dao) DeleteComment(comment *model.Comment) error { +func (d *dataServant) DeleteComment(comment *model.Comment) error { return comment.Delete(d.engine) } -func (d *Dao) GetCommentCount(conditions *model.ConditionsT) (int64, error) { +func (d *dataServant) GetCommentCount(conditions *model.ConditionsT) (int64, error) { return (&model.Comment{}).Count(d.engine, conditions) } -func (d *Dao) CreateComment(comment *model.Comment) (*model.Comment, error) { +func (d *dataServant) CreateComment(comment *model.Comment) (*model.Comment, error) { return comment.Create(d.engine) } -func (d *Dao) CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error) { +func (d *dataServant) CreateCommentReply(reply *model.CommentReply) (*model.CommentReply, error) { return reply.Create(d.engine) } -func (d *Dao) GetCommentReplyByID(id int64) (*model.CommentReply, error) { +func (d *dataServant) GetCommentReplyByID(id int64) (*model.CommentReply, error) { reply := &model.CommentReply{ Model: &model.Model{ ID: id, @@ -40,6 +40,6 @@ func (d *Dao) GetCommentReplyByID(id int64) (*model.CommentReply, error) { return reply.Get(d.engine) } -func (d *Dao) DeleteCommentReply(reply *model.CommentReply) error { +func (d *dataServant) DeleteCommentReply(reply *model.CommentReply) error { return reply.Delete(d.engine) } diff --git a/internal/dao/comment_content.go b/internal/dao/comment_content.go index e982b306..26fb4a14 100644 --- a/internal/dao/comment_content.go +++ b/internal/dao/comment_content.go @@ -2,13 +2,13 @@ package dao import "github.com/rocboss/paopao-ce/internal/model" -func (d *Dao) GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error) { +func (d *dataServant) GetCommentContentsByIDs(ids []int64) ([]*model.CommentContent, error) { commentContent := &model.CommentContent{} return commentContent.List(d.engine, &model.ConditionsT{ "comment_id IN ?": ids, }, 0, 0) } -func (d *Dao) GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error) { +func (d *dataServant) GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, error) { CommentReply := &model.CommentReply{} replies, err := CommentReply.List(d.engine, &model.ConditionsT{ "comment_id IN ?": ids, @@ -45,6 +45,6 @@ func (d *Dao) GetCommentRepliesByID(ids []int64) ([]*model.CommentReplyFormated, return repliesFormated, nil } -func (d *Dao) CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error) { +func (d *dataServant) CreateCommentContent(content *model.CommentContent) (*model.CommentContent, error) { return content.Create(d.engine) } diff --git a/internal/dao/dao.go b/internal/dao/dao.go index c1745f14..5bb900e8 100644 --- a/internal/dao/dao.go +++ b/internal/dao/dao.go @@ -1,17 +1,22 @@ package dao import ( + "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/pkg/zinc" "gorm.io/gorm" ) -type Dao struct { +var ( + _ core.DataService = (*dataServant)(nil) +) + +type dataServant struct { engine *gorm.DB zinc *zinc.ZincClient } -func New(engine *gorm.DB, zinc *zinc.ZincClient) *Dao { - return &Dao{ +func NewDataService(engine *gorm.DB, zinc *zinc.ZincClient) core.DataService { + return &dataServant{ engine: engine, zinc: zinc, } diff --git a/internal/dao/message.go b/internal/dao/message.go index afaaaa76..6afc3858 100644 --- a/internal/dao/message.go +++ b/internal/dao/message.go @@ -2,18 +2,18 @@ package dao import "github.com/rocboss/paopao-ce/internal/model" -func (d *Dao) CreateMessage(msg *model.Message) (*model.Message, error) { +func (d *dataServant) CreateMessage(msg *model.Message) (*model.Message, error) { return msg.Create(d.engine) } -func (d *Dao) GetUnreadCount(userID int64) (int64, error) { +func (d *dataServant) GetUnreadCount(userID int64) (int64, error) { return (&model.Message{}).Count(d.engine, &model.ConditionsT{ "receiver_user_id": userID, "is_read": model.MSG_UNREAD, }) } -func (d *Dao) GetMessageByID(id int64) (*model.Message, error) { +func (d *dataServant) GetMessageByID(id int64) (*model.Message, error) { return (&model.Message{ Model: &model.Model{ ID: id, @@ -21,12 +21,12 @@ func (d *Dao) GetMessageByID(id int64) (*model.Message, error) { }).Get(d.engine) } -func (d *Dao) ReadMessage(message *model.Message) error { +func (d *dataServant) ReadMessage(message *model.Message) error { message.IsRead = 1 return message.Update(d.engine) } -func (d *Dao) GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error) { +func (d *dataServant) GetMessages(conditions *model.ConditionsT, offset, limit int) ([]*model.MessageFormated, error) { messages, err := (&model.Message{}).List(d.engine, conditions, offset, limit) if err != nil { return nil, err @@ -41,6 +41,6 @@ func (d *Dao) GetMessages(conditions *model.ConditionsT, offset, limit int) ([]* return mfs, nil } -func (d *Dao) GetMessageCount(conditions *model.ConditionsT) (int64, error) { +func (d *dataServant) GetMessageCount(conditions *model.ConditionsT) (int64, error) { return (&model.Message{}).Count(d.engine, conditions) } diff --git a/internal/dao/post.go b/internal/dao/post.go index e7ed83e8..9773a61c 100644 --- a/internal/dao/post.go +++ b/internal/dao/post.go @@ -6,26 +6,26 @@ import ( "github.com/rocboss/paopao-ce/internal/model" ) -func (d *Dao) CreatePost(post *model.Post) (*model.Post, error) { +func (d *dataServant) CreatePost(post *model.Post) (*model.Post, error) { post.LatestRepliedOn = time.Now().Unix() return post.Create(d.engine) } -func (d *Dao) DeletePost(post *model.Post) error { +func (d *dataServant) DeletePost(post *model.Post) error { return post.Delete(d.engine) } -func (d *Dao) LockPost(post *model.Post) error { +func (d *dataServant) LockPost(post *model.Post) error { post.IsLock = 1 - post.IsLock return post.Update(d.engine) } -func (d *Dao) StickPost(post *model.Post) error { +func (d *dataServant) StickPost(post *model.Post) error { post.IsTop = 1 - post.IsTop return post.Update(d.engine) } -func (d *Dao) GetPostByID(id int64) (*model.Post, error) { +func (d *dataServant) GetPostByID(id int64) (*model.Post, error) { post := &model.Post{ Model: &model.Model{ ID: id, @@ -34,19 +34,19 @@ func (d *Dao) GetPostByID(id int64) (*model.Post, error) { return post.Get(d.engine) } -func (d *Dao) GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error) { +func (d *dataServant) GetPosts(conditions *model.ConditionsT, offset, limit int) ([]*model.Post, error) { return (&model.Post{}).List(d.engine, conditions, offset, limit) } -func (d *Dao) GetPostCount(conditions *model.ConditionsT) (int64, error) { +func (d *dataServant) GetPostCount(conditions *model.ConditionsT) (int64, error) { return (&model.Post{}).Count(d.engine, conditions) } -func (d *Dao) UpdatePost(post *model.Post) error { +func (d *dataServant) UpdatePost(post *model.Post) error { return post.Update(d.engine) } -func (d *Dao) GetUserPostStar(postID, userID int64) (*model.PostStar, error) { +func (d *dataServant) GetUserPostStar(postID, userID int64) (*model.PostStar, error) { star := &model.PostStar{ PostID: postID, UserID: userID, @@ -55,7 +55,7 @@ func (d *Dao) GetUserPostStar(postID, userID int64) (*model.PostStar, error) { return star.Get(d.engine) } -func (d *Dao) GetUserPostStars(userID int64, offset, limit int) ([]*model.PostStar, error) { +func (d *dataServant) GetUserPostStars(userID int64, offset, limit int) ([]*model.PostStar, error) { star := &model.PostStar{ UserID: userID, } @@ -65,14 +65,14 @@ func (d *Dao) GetUserPostStars(userID int64, offset, limit int) ([]*model.PostSt }, offset, limit) } -func (d *Dao) GetUserPostStarCount(userID int64) (int64, error) { +func (d *dataServant) GetUserPostStarCount(userID int64) (int64, error) { star := &model.PostStar{ UserID: userID, } return star.Count(d.engine, &model.ConditionsT{}) } -func (d *Dao) CreatePostStar(postID, userID int64) (*model.PostStar, error) { +func (d *dataServant) CreatePostStar(postID, userID int64) (*model.PostStar, error) { star := &model.PostStar{ PostID: postID, UserID: userID, @@ -81,11 +81,11 @@ func (d *Dao) CreatePostStar(postID, userID int64) (*model.PostStar, error) { return star.Create(d.engine) } -func (d *Dao) DeletePostStar(p *model.PostStar) error { +func (d *dataServant) DeletePostStar(p *model.PostStar) error { return p.Delete(d.engine) } -func (d *Dao) GetUserPostCollection(postID, userID int64) (*model.PostCollection, error) { +func (d *dataServant) GetUserPostCollection(postID, userID int64) (*model.PostCollection, error) { star := &model.PostCollection{ PostID: postID, UserID: userID, @@ -94,7 +94,7 @@ func (d *Dao) GetUserPostCollection(postID, userID int64) (*model.PostCollection return star.Get(d.engine) } -func (d *Dao) GetUserPostCollections(userID int64, offset, limit int) ([]*model.PostCollection, error) { +func (d *dataServant) GetUserPostCollections(userID int64, offset, limit int) ([]*model.PostCollection, error) { collection := &model.PostCollection{ UserID: userID, } @@ -104,13 +104,13 @@ func (d *Dao) GetUserPostCollections(userID int64, offset, limit int) ([]*model. }, offset, limit) } -func (d *Dao) GetUserPostCollectionCount(userID int64) (int64, error) { +func (d *dataServant) GetUserPostCollectionCount(userID int64) (int64, error) { collection := &model.PostCollection{ UserID: userID, } return collection.Count(d.engine, &model.ConditionsT{}) } -func (d *Dao) GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error) { +func (d *dataServant) GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, error) { statement := &model.WalletStatement{ UserID: userID, } @@ -120,14 +120,14 @@ func (d *Dao) GetUserWalletBills(userID int64, offset, limit int) ([]*model.Wall }, offset, limit) } -func (d *Dao) GetUserWalletBillCount(userID int64) (int64, error) { +func (d *dataServant) GetUserWalletBillCount(userID int64) (int64, error) { statement := &model.WalletStatement{ UserID: userID, } return statement.Count(d.engine, &model.ConditionsT{}) } -func (d *Dao) CreatePostCollection(postID, userID int64) (*model.PostCollection, error) { +func (d *dataServant) CreatePostCollection(postID, userID int64) (*model.PostCollection, error) { collection := &model.PostCollection{ PostID: postID, UserID: userID, @@ -136,11 +136,11 @@ func (d *Dao) CreatePostCollection(postID, userID int64) (*model.PostCollection, return collection.Create(d.engine) } -func (d *Dao) DeletePostCollection(p *model.PostCollection) error { +func (d *dataServant) DeletePostCollection(p *model.PostCollection) error { return p.Delete(d.engine) } -func (d *Dao) GetPostAttatchmentBill(postID, userID int64) (*model.PostAttachmentBill, error) { +func (d *dataServant) GetPostAttatchmentBill(postID, userID int64) (*model.PostAttachmentBill, error) { bill := &model.PostAttachmentBill{ PostID: postID, UserID: userID, diff --git a/internal/dao/post_content.go b/internal/dao/post_content.go index eded8b60..51995197 100644 --- a/internal/dao/post_content.go +++ b/internal/dao/post_content.go @@ -2,18 +2,18 @@ package dao import "github.com/rocboss/paopao-ce/internal/model" -func (d *Dao) CreatePostContent(content *model.PostContent) (*model.PostContent, error) { +func (d *dataServant) CreatePostContent(content *model.PostContent) (*model.PostContent, error) { return content.Create(d.engine) } -func (d *Dao) GetPostContentsByIDs(ids []int64) ([]*model.PostContent, error) { +func (d *dataServant) GetPostContentsByIDs(ids []int64) ([]*model.PostContent, error) { return (&model.PostContent{}).List(d.engine, &model.ConditionsT{ "post_id IN ?": ids, "ORDER": "sort ASC", }, 0, 0) } -func (d *Dao) GetPostContentByID(id int64) (*model.PostContent, error) { +func (d *dataServant) GetPostContentByID(id int64) (*model.PostContent, error) { return (&model.PostContent{ Model: &model.Model{ ID: id, diff --git a/internal/dao/search.go b/internal/dao/search.go index 803ca738..d30417db 100644 --- a/internal/dao/search.go +++ b/internal/dao/search.go @@ -1,20 +1,11 @@ package dao import ( + "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/pkg/zinc" ) -type SearchType string - -const SearchTypeDefault SearchType = "search" -const SearchTypeTag SearchType = "tag" - -type QueryT struct { - Query string - Type SearchType -} - -func (d *Dao) CreateSearchIndex(indexName string) { +func (d *dataServant) CreateSearchIndex(indexName string) { // 不存在则创建索引 d.zinc.CreateIndex(indexName, &zinc.ZincIndexProperty{ "id": &zinc.ZincIndexPropertyT{ @@ -104,21 +95,21 @@ func (d *Dao) CreateSearchIndex(indexName string) { } -func (d *Dao) BulkPushDoc(data []map[string]interface{}) (bool, error) { +func (d *dataServant) BulkPushDoc(data []map[string]interface{}) (bool, error) { return d.zinc.BulkPushDoc(data) } -func (d *Dao) DelDoc(indexName, id string) error { +func (d *dataServant) DelDoc(indexName, id string) error { return d.zinc.DelDoc(indexName, id) } -func (d *Dao) QueryAll(q *QueryT, indexName string, offset, limit int) (*zinc.QueryResultT, error) { +func (d *dataServant) QueryAll(q *core.QueryT, indexName string, offset, limit int) (*zinc.QueryResultT, error) { // 普通搜索 - if q.Type == SearchTypeDefault && q.Query != "" { + if q.Type == core.SearchTypeDefault && q.Query != "" { return d.QuerySearch(indexName, q.Query, offset, limit) } // Tag分类 - if q.Type == SearchTypeTag && q.Query != "" { + if q.Type == core.SearchTypeTag && q.Query != "" { return d.QueryTagSearch(indexName, q.Query, offset, limit) } @@ -138,7 +129,7 @@ func (d *Dao) QueryAll(q *QueryT, indexName string, offset, limit int) (*zinc.Qu return rsp, err } -func (d *Dao) QuerySearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) { +func (d *dataServant) QuerySearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) { rsp, err := d.zinc.EsQuery(indexName, map[string]interface{}{ "query": map[string]interface{}{ "match_phrase": map[string]interface{}{ @@ -156,7 +147,7 @@ func (d *Dao) QuerySearch(indexName, query string, offset, limit int) (*zinc.Que return rsp, err } -func (d *Dao) QueryTagSearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) { +func (d *dataServant) QueryTagSearch(indexName, query string, offset, limit int) (*zinc.QueryResultT, error) { rsp, err := d.zinc.ApiQuery(indexName, map[string]interface{}{ "search_type": "querystring", "query": map[string]interface{}{ diff --git a/internal/dao/tag.go b/internal/dao/tag.go index a15565db..3f2765ae 100644 --- a/internal/dao/tag.go +++ b/internal/dao/tag.go @@ -2,7 +2,7 @@ package dao import "github.com/rocboss/paopao-ce/internal/model" -func (d *Dao) CreateTag(tag *model.Tag) (*model.Tag, error) { +func (d *dataServant) CreateTag(tag *model.Tag) (*model.Tag, error) { t, err := tag.Get(d.engine) if err != nil { tag.QuoteNum = 1 @@ -20,7 +20,7 @@ func (d *Dao) CreateTag(tag *model.Tag) (*model.Tag, error) { return t, nil } -func (d *Dao) DeleteTag(tag *model.Tag) error { +func (d *dataServant) DeleteTag(tag *model.Tag) error { tag, err := tag.Get(d.engine) if err != nil { return err @@ -30,6 +30,6 @@ func (d *Dao) DeleteTag(tag *model.Tag) error { return tag.Update(d.engine) } -func (d *Dao) GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error) { +func (d *dataServant) GetTags(conditions *model.ConditionsT, offset, limit int) ([]*model.Tag, error) { return (&model.Tag{}).List(d.engine, conditions, offset, limit) } diff --git a/internal/dao/user.go b/internal/dao/user.go index d6cbe471..3859f67c 100644 --- a/internal/dao/user.go +++ b/internal/dao/user.go @@ -21,7 +21,7 @@ type JuhePhoneCaptchaRsp struct { } // 根据用户ID获取用户 -func (d *Dao) GetUserByID(id int64) (*model.User, error) { +func (d *dataServant) GetUserByID(id int64) (*model.User, error) { user := &model.User{ Model: &model.Model{ ID: id, @@ -32,7 +32,7 @@ func (d *Dao) GetUserByID(id int64) (*model.User, error) { } // 根据用户名获取用户 -func (d *Dao) GetUserByUsername(username string) (*model.User, error) { +func (d *dataServant) GetUserByUsername(username string) (*model.User, error) { user := &model.User{ Username: username, } @@ -41,7 +41,7 @@ func (d *Dao) GetUserByUsername(username string) (*model.User, error) { } // 根据手机号获取用户 -func (d *Dao) GetUserByPhone(phone string) (*model.User, error) { +func (d *dataServant) GetUserByPhone(phone string) (*model.User, error) { user := &model.User{ Phone: phone, } @@ -50,7 +50,7 @@ func (d *Dao) GetUserByPhone(phone string) (*model.User, error) { } // 根据IDs获取用户列表 -func (d *Dao) GetUsersByIDs(ids []int64) ([]*model.User, error) { +func (d *dataServant) GetUsersByIDs(ids []int64) ([]*model.User, error) { user := &model.User{} return user.List(d.engine, &model.ConditionsT{ @@ -59,7 +59,7 @@ func (d *Dao) GetUsersByIDs(ids []int64) ([]*model.User, error) { } // 根据关键词模糊获取用户列表 -func (d *Dao) GetUsersByKeyword(keyword string) ([]*model.User, error) { +func (d *dataServant) GetUsersByKeyword(keyword string) ([]*model.User, error) { user := &model.User{} if strings.Trim(keyword, "") == "" { @@ -75,7 +75,7 @@ func (d *Dao) GetUsersByKeyword(keyword string) ([]*model.User, error) { } // 根据关键词模糊获取用户列表 -func (d *Dao) GetTagsByKeyword(keyword string) ([]*model.Tag, error) { +func (d *dataServant) GetTagsByKeyword(keyword string) ([]*model.Tag, error) { tag := &model.Tag{} if strings.Trim(keyword, "") == "" { @@ -92,30 +92,30 @@ func (d *Dao) GetTagsByKeyword(keyword string) ([]*model.Tag, error) { } // 创建用户 -func (d *Dao) CreateUser(user *model.User) (*model.User, error) { +func (d *dataServant) CreateUser(user *model.User) (*model.User, error) { return user.Create(d.engine) } // 更新用户 -func (d *Dao) UpdateUser(user *model.User) error { +func (d *dataServant) UpdateUser(user *model.User) error { return user.Update(d.engine) } // 获取最新短信验证码 -func (d *Dao) GetLatestPhoneCaptcha(phone string) (*model.Captcha, error) { +func (d *dataServant) GetLatestPhoneCaptcha(phone string) (*model.Captcha, error) { return (&model.Captcha{ Phone: phone, }).Get(d.engine) } // 更新短信验证码 -func (d *Dao) UsePhoneCaptcha(captcha *model.Captcha) error { +func (d *dataServant) UsePhoneCaptcha(captcha *model.Captcha) error { captcha.UseTimes++ return captcha.Update(d.engine) } // 发送短信验证码 -func (d *Dao) SendPhoneCaptcha(phone string) error { +func (d *dataServant) SendPhoneCaptcha(phone string) error { rand.Seed(time.Now().UnixNano()) captcha := rand.Intn(900000) + 100000 m := 5 diff --git a/internal/dao/wallet.go b/internal/dao/wallet.go index becae12b..272b1f5a 100644 --- a/internal/dao/wallet.go +++ b/internal/dao/wallet.go @@ -6,7 +6,7 @@ import ( "gorm.io/gorm" ) -func (d *Dao) GetRechargeByID(id int64) (*model.WalletRecharge, error) { +func (d *dataServant) GetRechargeByID(id int64) (*model.WalletRecharge, error) { recharge := &model.WalletRecharge{ Model: &model.Model{ ID: id, @@ -15,7 +15,7 @@ func (d *Dao) GetRechargeByID(id int64) (*model.WalletRecharge, error) { return recharge.Get(d.engine) } -func (d *Dao) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error) { +func (d *dataServant) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error) { recharge := &model.WalletRecharge{ UserID: userId, Amount: amount, @@ -24,7 +24,7 @@ func (d *Dao) CreateRecharge(userId, amount int64) (*model.WalletRecharge, error return recharge.Create(d.engine) } -func (d *Dao) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error { +func (d *dataServant) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo string) error { user, _ := (&model.User{ Model: &model.Model{ ID: recharge.UserID, @@ -61,7 +61,7 @@ func (d *Dao) HandleRechargeSuccess(recharge *model.WalletRecharge, tradeNo stri }) } -func (d *Dao) HandlePostAttachmentBought(post *model.Post, user *model.User) error { +func (d *dataServant) HandlePostAttachmentBought(post *model.Post, user *model.User) error { return d.engine.Transaction(func(tx *gorm.DB) error { // 扣除金额 if err := tx.Model(user).Update("balance", gorm.Expr("balance - ?", post.AttachmentPrice)).Error; err != nil { diff --git a/internal/routers/api/post.go b/internal/routers/api/post.go index 226c9c6a..fed3d230 100644 --- a/internal/routers/api/post.go +++ b/internal/routers/api/post.go @@ -3,7 +3,7 @@ package api import ( "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/global" - "github.com/rocboss/paopao-ce/internal/dao" + "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/internal/service" "github.com/rocboss/paopao-ce/pkg/app" @@ -14,7 +14,7 @@ import ( func GetPostList(c *gin.Context) { response := app.NewResponse(c) - q := &dao.QueryT{ + q := &core.QueryT{ Query: c.Query("query"), Type: "search", } diff --git a/internal/service/attachment.go b/internal/service/attachment.go index 053b555c..432a1a2f 100644 --- a/internal/service/attachment.go +++ b/internal/service/attachment.go @@ -3,5 +3,5 @@ package service import "github.com/rocboss/paopao-ce/internal/model" func CreateAttachment(attachment *model.Attachment) (*model.Attachment, error) { - return myDao.CreateAttachment(attachment) + return ds.CreateAttachment(attachment) } diff --git a/internal/service/comment.go b/internal/service/comment.go index 88cb917c..9be69089 100644 --- a/internal/service/comment.go +++ b/internal/service/comment.go @@ -34,7 +34,7 @@ func GetPostComments(postID int64, sort string, offset, limit int) ([]*model.Com "post_id": postID, "ORDER": sort, } - comments, err := myDao.GetComments(conditions, offset, limit) + comments, err := ds.GetComments(conditions, offset, limit) if err != nil { return nil, 0, err @@ -47,17 +47,17 @@ func GetPostComments(postID int64, sort string, offset, limit int) ([]*model.Com commentIDs = append(commentIDs, comment.ID) } - users, err := myDao.GetUsersByIDs(userIDs) + users, err := ds.GetUsersByIDs(userIDs) if err != nil { return nil, 0, err } - contents, err := myDao.GetCommentContentsByIDs(commentIDs) + contents, err := ds.GetCommentContentsByIDs(commentIDs) if err != nil { return nil, 0, err } - replies, err := myDao.GetCommentRepliesByID(commentIDs) + replies, err := ds.GetCommentRepliesByID(commentIDs) if err != nil { return nil, 0, err } @@ -85,14 +85,14 @@ func GetPostComments(postID int64, sort string, offset, limit int) ([]*model.Com } // 获取总量 - totalRows, _ := myDao.GetCommentCount(conditions) + totalRows, _ := ds.GetCommentCount(conditions) return commentsFormated, totalRows, nil } func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) (*model.Comment, error) { // 加载Post - post, err := myDao.GetPostByID(param.PostID) + post, err := ds.GetPostByID(param.PostID) if err != nil { return nil, err @@ -108,7 +108,7 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) IP: ip, IPLoc: util.GetIPLoc(ip), } - comment, err = myDao.CreateComment(comment) + comment, err = ds.CreateComment(comment) if err != nil { return nil, err } @@ -128,21 +128,21 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) Type: item.Type, Sort: item.Sort, } - myDao.CreateCommentContent(postContent) + ds.CreateCommentContent(postContent) } // 更新Post回复数 post.CommentCount++ post.LatestRepliedOn = time.Now().Unix() - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) // 创建用户消息提醒 - postMaster, err := myDao.GetUserByID(post.UserID) + postMaster, err := ds.GetUserByID(post.UserID) if err == nil && postMaster.ID != userID { - go myDao.CreateMessage(&model.Message{ + go ds.CreateMessage(&model.Message{ SenderUserID: userID, ReceiverUserID: postMaster.ID, Type: model.MESSAGE_COMMENT, @@ -152,13 +152,13 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) }) } for _, u := range param.Users { - user, err := myDao.GetUserByUsername(u) + user, err := ds.GetUserByUsername(u) if err != nil || user.ID == userID || user.ID == postMaster.ID { continue } // 创建消息提醒 - go myDao.CreateMessage(&model.Message{ + go ds.CreateMessage(&model.Message{ SenderUserID: userID, ReceiverUserID: user.ID, Type: model.MESSAGE_COMMENT, @@ -172,31 +172,31 @@ func CreatePostComment(ctx *gin.Context, userID int64, param CommentCreationReq) } func GetPostComment(id int64) (*model.Comment, error) { - return myDao.GetCommentByID(id) + return ds.GetCommentByID(id) } func DeletePostComment(comment *model.Comment) error { // 加载post - post, err := myDao.GetPostByID(comment.PostID) + post, err := ds.GetPostByID(comment.PostID) if err == nil { // 更新post回复数 post.CommentCount-- - myDao.UpdatePost(post) + ds.UpdatePost(post) } - return myDao.DeleteComment(comment) + return ds.DeleteComment(comment) } func createPostPreHandler(commentID int64, userID, atUserID int64) (*model.Post, *model.Comment, int64, error) { // 加载Comment - comment, err := myDao.GetCommentByID(commentID) + comment, err := ds.GetCommentByID(commentID) if err != nil { return nil, nil, atUserID, err } // 加载comment的post - post, err := myDao.GetPostByID(comment.PostID) + post, err := ds.GetPostByID(comment.PostID) if err != nil { return nil, nil, atUserID, err } @@ -211,7 +211,7 @@ func createPostPreHandler(commentID int64, userID, atUserID int64) (*model.Post, if atUserID > 0 { // 检测目前用户是否存在 - users, _ := myDao.GetUsersByIDs([]int64{atUserID}) + users, _ := ds.GetUsersByIDs([]int64{atUserID}) if len(users) == 0 { atUserID = 0 } @@ -241,7 +241,7 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u IPLoc: util.GetIPLoc(ip), } - reply, err = myDao.CreateCommentReply(reply) + reply, err = ds.CreateCommentReply(reply) if err != nil { return nil, err } @@ -249,15 +249,15 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u // 更新Post回复数 post.CommentCount++ post.LatestRepliedOn = time.Now().Unix() - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) // 创建用户消息提醒 - commentMaster, err := myDao.GetUserByID(comment.UserID) + commentMaster, err := ds.GetUserByID(comment.UserID) if err == nil && commentMaster.ID != userID { - go myDao.CreateMessage(&model.Message{ + go ds.CreateMessage(&model.Message{ SenderUserID: userID, ReceiverUserID: commentMaster.ID, Type: model.MESSAGE_REPLY, @@ -267,9 +267,9 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u ReplyID: reply.ID, }) } - postMaster, err := myDao.GetUserByID(post.UserID) + postMaster, err := ds.GetUserByID(post.UserID) if err == nil && postMaster.ID != userID && commentMaster.ID != postMaster.ID { - go myDao.CreateMessage(&model.Message{ + go ds.CreateMessage(&model.Message{ SenderUserID: userID, ReceiverUserID: postMaster.ID, Type: model.MESSAGE_REPLY, @@ -280,10 +280,10 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u }) } if atUserID > 0 { - user, err := myDao.GetUserByID(atUserID) + user, err := ds.GetUserByID(atUserID) if err == nil && user.ID != userID && commentMaster.ID != user.ID && postMaster.ID != user.ID { // 创建消息提醒 - go myDao.CreateMessage(&model.Message{ + go ds.CreateMessage(&model.Message{ SenderUserID: userID, ReceiverUserID: user.ID, Type: model.MESSAGE_REPLY, @@ -299,23 +299,23 @@ func CreatePostCommentReply(ctx *gin.Context, commentID int64, content string, u } func GetPostCommentReply(id int64) (*model.CommentReply, error) { - return myDao.GetCommentReplyByID(id) + return ds.GetCommentReplyByID(id) } func DeletePostCommentReply(reply *model.CommentReply) error { - err := myDao.DeleteCommentReply(reply) + err := ds.DeleteCommentReply(reply) if err != nil { return err } // 加载Comment - comment, err := myDao.GetCommentByID(reply.CommentID) + comment, err := ds.GetCommentByID(reply.CommentID) if err != nil { return err } // 加载comment的post - post, err := myDao.GetPostByID(comment.PostID) + post, err := ds.GetPostByID(comment.PostID) if err != nil { return err } @@ -323,7 +323,7 @@ func DeletePostCommentReply(reply *model.CommentReply) error { // 更新Post回复数 post.CommentCount-- post.LatestRepliedOn = time.Now().Unix() - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) diff --git a/internal/service/message.go b/internal/service/message.go index 64e0cd4a..3fd8c321 100644 --- a/internal/service/message.go +++ b/internal/service/message.go @@ -32,7 +32,7 @@ func CreateWhisper(c *gin.Context, msg *model.Message) (*model.Message, error) { } // 创建私信 - msg, err := myDao.CreateMessage(msg) + msg, err := ds.CreateMessage(msg) if err != nil { return nil, err } @@ -48,11 +48,11 @@ func CreateWhisper(c *gin.Context, msg *model.Message) (*model.Message, error) { } func GetUnreadCount(userID int64) (int64, error) { - return myDao.GetUnreadCount(userID) + return ds.GetUnreadCount(userID) } func ReadMessage(id, userID int64) error { // 获取message - message, err := myDao.GetMessageByID(id) + message, err := ds.GetMessageByID(id) if err != nil { return err } @@ -62,7 +62,7 @@ func ReadMessage(id, userID int64) error { } // 已读消息 - return myDao.ReadMessage(message) + return ds.ReadMessage(message) } func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int64, error) { @@ -70,12 +70,12 @@ func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int "receiver_user_id": userID, "ORDER": "id DESC", } - messages, err := myDao.GetMessages(conditions, offset, limit) + messages, err := ds.GetMessages(conditions, offset, limit) for _, mf := range messages { if mf.SenderUserID > 0 { - user, err := myDao.GetUserByID(mf.SenderUserID) + user, err := ds.GetUserByID(mf.SenderUserID) if err == nil { mf.SenderUser = user.Format() } @@ -109,7 +109,7 @@ func GetMessages(userID int64, offset, limit int) ([]*model.MessageFormated, int } // 获取总量 - totalRows, _ := myDao.GetMessageCount(conditions) + totalRows, _ := ds.GetMessageCount(conditions) return messages, totalRows, nil } diff --git a/internal/service/post.go b/internal/service/post.go index 7554c2ed..e375fc03 100644 --- a/internal/service/post.go +++ b/internal/service/post.go @@ -9,7 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/rocboss/paopao-ce/global" - "github.com/rocboss/paopao-ce/internal/dao" + "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/pkg/util" "github.com/rocboss/paopao-ce/pkg/zinc" @@ -92,7 +92,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos IPLoc: util.GetIPLoc(ip), AttachmentPrice: param.AttachmentPrice, } - post, err := myDao.CreatePost(post) + post, err := ds.CreatePost(post) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos UserID: userID, Tag: t, } - myDao.CreateTag(tag) + ds.CreateTag(tag) } for _, item := range param.Contents { @@ -123,7 +123,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos Type: item.Type, Sort: item.Sort, } - myDao.CreatePostContent(postContent) + ds.CreatePostContent(postContent) } // 推送Search @@ -131,13 +131,13 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos // 创建用户消息提醒 for _, u := range param.Users { - user, err := myDao.GetUserByUsername(u) + user, err := ds.GetUserByUsername(u) if err != nil || user.ID == userID { continue } // 创建消息提醒 - go myDao.CreateMessage(&model.Message{ + go ds.CreateMessage(&model.Message{ SenderUserID: userID, ReceiverUserID: user.ID, Type: model.MESSAGE_POST, @@ -150,7 +150,7 @@ func CreatePost(c *gin.Context, userID int64, param PostCreationReq) (*model.Pos } func DeletePost(id int64) error { - post, _ := myDao.GetPostByID(id) + post, _ := ds.GetPostByID(id) // tag删除 tags := strings.Split(post.Tags, ",") @@ -159,10 +159,10 @@ func DeletePost(id int64) error { tag := &model.Tag{ Tag: t, } - myDao.DeleteTag(tag) + ds.DeleteTag(tag) } - err := myDao.DeletePost(post) + err := ds.DeletePost(post) if err != nil { return err @@ -175,9 +175,9 @@ func DeletePost(id int64) error { } func LockPost(id int64) error { - post, _ := myDao.GetPostByID(id) + post, _ := ds.GetPostByID(id) - err := myDao.LockPost(post) + err := ds.LockPost(post) if err != nil { return err @@ -187,9 +187,9 @@ func LockPost(id int64) error { } func StickPost(id int64) error { - post, _ := myDao.GetPostByID(id) + post, _ := ds.GetPostByID(id) - err := myDao.StickPost(post) + err := ds.StickPost(post) if err != nil { return err @@ -199,23 +199,23 @@ func StickPost(id int64) error { } func GetPostStar(postID, userID int64) (*model.PostStar, error) { - return myDao.GetUserPostStar(postID, userID) + return ds.GetUserPostStar(postID, userID) } func CreatePostStar(postID, userID int64) (*model.PostStar, error) { // 加载Post - post, err := myDao.GetPostByID(postID) + post, err := ds.GetPostByID(postID) if err != nil { return nil, err } - star, err := myDao.CreatePostStar(postID, userID) + star, err := ds.CreatePostStar(postID, userID) if err != nil { return nil, err } // 更新Post点赞数 post.UpvoteCount++ - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) @@ -224,18 +224,18 @@ func CreatePostStar(postID, userID int64) (*model.PostStar, error) { } func DeletePostStar(star *model.PostStar) error { - err := myDao.DeletePostStar(star) + err := ds.DeletePostStar(star) if err != nil { return err } // 加载Post - post, err := myDao.GetPostByID(star.PostID) + post, err := ds.GetPostByID(star.PostID) if err != nil { return err } // 更新Post点赞数 post.UpvoteCount-- - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) @@ -244,23 +244,23 @@ func DeletePostStar(star *model.PostStar) error { } func GetPostCollection(postID, userID int64) (*model.PostCollection, error) { - return myDao.GetUserPostCollection(postID, userID) + return ds.GetUserPostCollection(postID, userID) } func CreatePostCollection(postID, userID int64) (*model.PostCollection, error) { // 加载Post - post, err := myDao.GetPostByID(postID) + post, err := ds.GetPostByID(postID) if err != nil { return nil, err } - collection, err := myDao.CreatePostCollection(postID, userID) + collection, err := ds.CreatePostCollection(postID, userID) if err != nil { return nil, err } // 更新Post点赞数 post.CollectionCount++ - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) @@ -269,18 +269,18 @@ func CreatePostCollection(postID, userID int64) (*model.PostCollection, error) { } func DeletePostCollection(collection *model.PostCollection) error { - err := myDao.DeletePostCollection(collection) + err := ds.DeletePostCollection(collection) if err != nil { return err } // 加载Post - post, err := myDao.GetPostByID(collection.PostID) + post, err := ds.GetPostByID(collection.PostID) if err != nil { return err } // 更新Post点赞数 post.CollectionCount-- - myDao.UpdatePost(post) + ds.UpdatePost(post) // 更新索引 go PushPostToSearch(post) @@ -289,18 +289,18 @@ func DeletePostCollection(collection *model.PostCollection) error { } func GetPost(id int64) (*model.PostFormated, error) { - post, err := myDao.GetPostByID(id) + post, err := ds.GetPostByID(id) if err != nil { return nil, err } - postContents, err := myDao.GetPostContentsByIDs([]int64{post.ID}) + postContents, err := ds.GetPostContentsByIDs([]int64{post.ID}) if err != nil { return nil, err } - users, err := myDao.GetUsersByIDs([]int64{post.UserID}) + users, err := ds.GetUsersByIDs([]int64{post.UserID}) if err != nil { return nil, err } @@ -319,11 +319,11 @@ func GetPost(id int64) (*model.PostFormated, error) { } func GetPostContentByID(id int64) (*model.PostContent, error) { - return myDao.GetPostContentByID(id) + return ds.GetPostContentByID(id) } func GetPostList(req *PostListReq) ([]*model.PostFormated, error) { - posts, err := myDao.GetPosts(req.Conditions, req.Offset, req.Limit) + posts, err := ds.GetPosts(req.Conditions, req.Offset, req.Limit) if err != nil { return nil, err @@ -340,12 +340,12 @@ func FormatPosts(posts []*model.Post) ([]*model.PostFormated, error) { userIds = append(userIds, post.UserID) } - postContents, err := myDao.GetPostContentsByIDs(postIds) + postContents, err := ds.GetPostContentsByIDs(postIds) if err != nil { return nil, err } - users, err := myDao.GetUsersByIDs(userIds) + users, err := ds.GetUsersByIDs(userIds) if err != nil { return nil, err } @@ -373,11 +373,11 @@ func FormatPosts(posts []*model.Post) ([]*model.PostFormated, error) { } func GetPostCount(conditions *model.ConditionsT) (int64, error) { - return myDao.GetPostCount(conditions) + return ds.GetPostCount(conditions) } -func GetPostListFromSearch(q *dao.QueryT, offset, limit int) ([]*model.PostFormated, int64, error) { - queryResult, err := myDao.QueryAll(q, global.SearchSetting.ZincIndex, offset, limit) +func GetPostListFromSearch(q *core.QueryT, offset, limit int) ([]*model.PostFormated, int64, error) { + queryResult, err := ds.QueryAll(q, global.SearchSetting.ZincIndex, offset, limit) if err != nil { return nil, 0, err } @@ -391,7 +391,7 @@ func GetPostListFromSearch(q *dao.QueryT, offset, limit int) ([]*model.PostForma } func GetPostListFromSearchByQuery(query string, offset, limit int) ([]*model.PostFormated, int64, error) { - queryResult, err := myDao.QuerySearch(global.SearchSetting.ZincIndex, query, offset, limit) + queryResult, err := ds.QuerySearch(global.SearchSetting.ZincIndex, query, offset, limit) if err != nil { return nil, 0, err } @@ -411,7 +411,7 @@ func PushPostToSearch(post *model.Post) { postFormated.User = &model.UserFormated{ ID: post.UserID, } - contents, _ := myDao.GetPostContentsByIDs([]int64{post.ID}) + contents, _ := ds.GetPostContentsByIDs([]int64{post.ID}) for _, content := range contents { postFormated.Contents = append(postFormated.Contents, content.Format()) } @@ -452,13 +452,13 @@ func PushPostToSearch(post *model.Post) { "modified_on": post.ModifiedOn, }) - myDao.BulkPushDoc(data) + ds.BulkPushDoc(data) } func DeleteSearchPost(post *model.Post) error { indexName := global.SearchSetting.ZincIndex - return myDao.DelDoc(indexName, fmt.Sprintf("%d", post.ID)) + return ds.DelDoc(indexName, fmt.Sprintf("%d", post.ID)) } func PushPostsToSearch(c *gin.Context) { @@ -471,7 +471,7 @@ func PushPostsToSearch(c *gin.Context) { indexName := global.SearchSetting.ZincIndex // 创建索引 - myDao.CreateSearchIndex(indexName) + ds.CreateSearchIndex(indexName) for i := 0; i < nums; i++ { data := []map[string]interface{}{} @@ -515,7 +515,7 @@ func PushPostsToSearch(c *gin.Context) { } if len(data) > 0 { - myDao.BulkPushDoc(data) + ds.BulkPushDoc(data) } } @@ -541,12 +541,12 @@ func FormatZincPost(queryResult *zinc.QueryResultT) ([]*model.PostFormated, erro postIds = append(postIds, post.ID) userIds = append(userIds, post.UserID) } - postContents, err := myDao.GetPostContentsByIDs(postIds) + postContents, err := ds.GetPostContentsByIDs(postIds) if err != nil { return nil, err } - users, err := myDao.GetUsersByIDs(userIds) + users, err := ds.GetUsersByIDs(userIds) if err != nil { return nil, err } @@ -591,7 +591,7 @@ func GetPostTags(param *PostTagsReq) ([]*model.TagFormated, error) { } } - tags, err := myDao.GetTags(conditions, 0, num) + tags, err := ds.GetTags(conditions, 0, num) if err != nil { return nil, err } @@ -602,7 +602,7 @@ func GetPostTags(param *PostTagsReq) ([]*model.TagFormated, error) { userIds = append(userIds, tag.UserID) } - users, _ := myDao.GetUsersByIDs(userIds) + users, _ := ds.GetUsersByIDs(userIds) tagsFormated := []*model.TagFormated{} for _, tag := range tags { @@ -619,7 +619,7 @@ func GetPostTags(param *PostTagsReq) ([]*model.TagFormated, error) { } func CheckPostAttachmentIsPaid(postID, userID int64) bool { - bill, err := myDao.GetPostAttatchmentBill(postID, userID) + bill, err := ds.GetPostAttatchmentBill(postID, userID) return err == nil && bill.Model != nil && bill.ID > 0 } diff --git a/internal/service/service.go b/internal/service/service.go index d9b91b40..b08d7fd6 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,15 +1,16 @@ package service import ( + "github.com/rocboss/paopao-ce/internal/core" "github.com/rocboss/paopao-ce/internal/dao" "github.com/rocboss/paopao-ce/pkg/zinc" "gorm.io/gorm" ) var ( - myDao *dao.Dao + ds core.DataService ) func Initialize(engine *gorm.DB, client *zinc.ZincClient) { - myDao = dao.New(engine, client) + ds = dao.NewDataService(engine, client) } diff --git a/internal/service/user.go b/internal/service/user.go index e055d0f4..dde7d49c 100644 --- a/internal/service/user.go +++ b/internal/service/user.go @@ -57,7 +57,7 @@ const MAX_LOGIN_ERR_TIMES = 10 // DoLogin 用户认证 func DoLogin(ctx *gin.Context, param *AuthRequest) (*model.User, error) { - user, err := myDao.GetUserByUsername(param.Username) + user, err := ds.GetUserByUsername(param.Username) if err != nil { return nil, errcode.UnauthorizedAuthNotExist } @@ -115,7 +115,7 @@ func ValidUsername(username string) error { } // 重复检查 - user, _ := myDao.GetUserByUsername(username) + user, _ := ds.GetUserByUsername(username) if user.Model != nil && user.ID > 0 { return errcode.UsernameHasExisted @@ -136,7 +136,7 @@ func CheckPassword(password string) error { // CheckPhoneCaptcha 验证手机验证码 func CheckPhoneCaptcha(phone, captcha string) *errcode.Error { - c, err := myDao.GetLatestPhoneCaptcha(phone) + c, err := ds.GetLatestPhoneCaptcha(phone) if err != nil { return errcode.ErrorPhoneCaptcha } @@ -154,14 +154,14 @@ func CheckPhoneCaptcha(phone, captcha string) *errcode.Error { } // 更新检测次数 - myDao.UsePhoneCaptcha(c) + ds.UsePhoneCaptcha(c) return nil } // CheckPhoneExist 检测手机号是否存在 func CheckPhoneExist(uid int64, phone string) bool { - u, err := myDao.GetUserByPhone(phone) + u, err := ds.GetUserByPhone(phone) if err != nil { return false } @@ -198,7 +198,7 @@ func Register(username, password string) (*model.User, error) { Status: model.UserStatusNormal, } - user, err := myDao.CreateUser(user) + user, err := ds.CreateUser(user) if err != nil { return nil, err } @@ -208,7 +208,7 @@ func Register(username, password string) (*model.User, error) { // GetUserInfo 获取用户信息 func GetUserInfo(param *AuthRequest) (*model.User, error) { - user, err := myDao.GetUserByUsername(param.Username) + user, err := ds.GetUserByUsername(param.Username) if err != nil { return nil, err @@ -222,7 +222,7 @@ func GetUserInfo(param *AuthRequest) (*model.User, error) { } func GetUserByUsername(username string) (*model.User, error) { - user, err := myDao.GetUserByUsername(username) + user, err := ds.GetUserByUsername(username) if err != nil { return nil, err @@ -237,16 +237,16 @@ func GetUserByUsername(username string) (*model.User, error) { // UpdateUserInfo 更新用户信息 func UpdateUserInfo(user *model.User) error { - return myDao.UpdateUser(user) + return ds.UpdateUser(user) } // GetUserCollections 获取用户收藏列表 func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated, int64, error) { - collections, err := myDao.GetUserPostCollections(userID, offset, limit) + collections, err := ds.GetUserPostCollections(userID, offset, limit) if err != nil { return nil, 0, err } - totalRows, err := myDao.GetUserPostCollectionCount(userID) + totalRows, err := ds.GetUserPostCollectionCount(userID) if err != nil { return nil, 0, err } @@ -256,7 +256,7 @@ func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated, } // 获取Posts - posts, err := myDao.GetPosts(&model.ConditionsT{ + posts, err := ds.GetPosts(&model.ConditionsT{ "id IN ?": postIDs, "ORDER": "id DESC", }, 0, 0) @@ -274,11 +274,11 @@ func GetUserCollections(userID int64, offset, limit int) ([]*model.PostFormated, // GetUserStars 获取用户点赞列表 func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64, error) { - stars, err := myDao.GetUserPostStars(userID, offset, limit) + stars, err := ds.GetUserPostStars(userID, offset, limit) if err != nil { return nil, 0, err } - totalRows, err := myDao.GetUserPostStarCount(userID) + totalRows, err := ds.GetUserPostStarCount(userID) if err != nil { return nil, 0, err } @@ -288,7 +288,7 @@ func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64 } // 获取Posts - posts, err := myDao.GetPosts(&model.ConditionsT{ + posts, err := ds.GetPosts(&model.ConditionsT{ "id IN ?": postIDs, "ORDER": "id DESC", }, 0, 0) @@ -306,11 +306,11 @@ func GetUserStars(userID int64, offset, limit int) ([]*model.PostFormated, int64 // GetUserWalletBills 获取用户账单列表 func GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStatement, int64, error) { - bills, err := myDao.GetUserWalletBills(userID, offset, limit) + bills, err := ds.GetUserWalletBills(userID, offset, limit) if err != nil { return nil, 0, err } - totalRows, err := myDao.GetUserWalletBillCount(userID) + totalRows, err := ds.GetUserWalletBillCount(userID) if err != nil { return nil, 0, err } @@ -321,7 +321,7 @@ func GetUserWalletBills(userID int64, offset, limit int) ([]*model.WalletStateme // SendPhoneCaptcha 发送短信验证码 func SendPhoneCaptcha(ctx *gin.Context, phone string) error { - err := myDao.SendPhoneCaptcha(phone) + err := ds.SendPhoneCaptcha(phone) if err != nil { return err } @@ -339,7 +339,7 @@ func SendPhoneCaptcha(ctx *gin.Context, phone string) error { // GetSuggestUsers 根据关键词获取用户推荐 func GetSuggestUsers(keyword string) ([]string, error) { - users, err := myDao.GetUsersByKeyword(keyword) + users, err := ds.GetUsersByKeyword(keyword) if err != nil { return nil, err } @@ -354,7 +354,7 @@ func GetSuggestUsers(keyword string) ([]string, error) { // GetSuggestTags 根据关键词获取标签推荐 func GetSuggestTags(keyword string) ([]string, error) { - tags, err := myDao.GetTagsByKeyword(keyword) + tags, err := ds.GetTagsByKeyword(keyword) if err != nil { return nil, err } diff --git a/internal/service/wallet.go b/internal/service/wallet.go index 8c5da819..982f61bd 100644 --- a/internal/service/wallet.go +++ b/internal/service/wallet.go @@ -14,16 +14,16 @@ type RechargeReq struct { } func GetRechargeByID(id int64) (*model.WalletRecharge, error) { - return myDao.GetRechargeByID(id) + return ds.GetRechargeByID(id) } func CreateRecharge(userID, amount int64) (*model.WalletRecharge, error) { - return myDao.CreateRecharge(userID, amount) + return ds.CreateRecharge(userID, amount) } func FinishRecharge(ctx *gin.Context, id int64, tradeNo string) error { if ok, _ := global.Redis.SetNX(ctx, "PaoPaoRecharge:"+tradeNo, 1, time.Second*5).Result(); ok { - recharge, err := myDao.GetRechargeByID(id) + recharge, err := ds.GetRechargeByID(id) if err != nil { return err } @@ -31,7 +31,7 @@ func FinishRecharge(ctx *gin.Context, id int64, tradeNo string) error { if recharge.TradeStatus != "TRADE_SUCCESS" { // 标记为已付款 - err := myDao.HandleRechargeSuccess(recharge, tradeNo) + err := ds.HandleRechargeSuccess(recharge, tradeNo) defer global.Redis.Del(ctx, "PaoPaoRecharge:"+tradeNo) if err != nil { @@ -50,5 +50,5 @@ func BuyPostAttachment(post *model.Post, user *model.User) error { } // 执行购买 - return myDao.HandlePostAttachmentBought(post, user) + return ds.HandlePostAttachmentBought(post, user) }