diff --git a/internal/dao/sakila/authority.go b/internal/dao/sakila/authority.go new file mode 100644 index 00000000..2811ddd9 --- /dev/null +++ b/internal/dao/sakila/authority.go @@ -0,0 +1,59 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.AuthorizationManageService = (*authorizationManageSrv)(nil) +) + +type authorizationManageSrv struct { + *sqlxSrv + stmtIdx *sqlx.Stmt + stmtUpdateFriend *sqlx.Stmt +} + +func (s *authorizationManageSrv) IsAllow(user *core.User, action *core.Action) bool { + // TODO + debug.NotImplemented() + return false +} + +func (s *authorizationManageSrv) MyFriendSet(userId int64) core.FriendSet { + // TODO + debug.NotImplemented() + return nil +} + +func (s *authorizationManageSrv) BeFriendFilter(userId int64) core.FriendFilter { + // TODO + debug.NotImplemented() + return nil +} + +func (s *authorizationManageSrv) BeFriendIds(userId int64) ([]int64, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *authorizationManageSrv) isFriend(userId int64, friendId int64) bool { + // TODO + debug.NotImplemented() + return false +} + +func newAuthorizationManageService(db *sqlx.DB) core.AuthorizationManageService { + return &authorizationManageSrv{ + sqlxSrv: newSqlxSrv(db), + stmtIdx: c(`SELECT * FROM @person WHERE first_name=?`), + stmtUpdateFriend: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/comments.go b/internal/dao/sakila/comments.go new file mode 100644 index 00000000..b15d0135 --- /dev/null +++ b/internal/dao/sakila/comments.go @@ -0,0 +1,110 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.CommentService = (*commentSrv)(nil) + _ core.CommentManageService = (*commentManageSrv)(nil) +) + +type commentSrv struct { + *sqlxSrv + stmtGetComments *sqlx.Stmt + stmtGetReply *sqlx.Stmt +} + +type commentManageSrv struct { + *sqlxSrv + stmtDelComments *sqlx.Stmt + stmtAddComents *sqlx.Stmt +} + +func (s *commentSrv) GetComments(conditions *core.ConditionsT, offset, limit int) ([]*core.Comment, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentSrv) GetCommentByID(id int64) (*core.Comment, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentSrv) GetCommentReplyByID(id int64) (*core.CommentReply, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentSrv) GetCommentCount(conditions *core.ConditionsT) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *commentSrv) GetCommentContentsByIDs(ids []int64) ([]*core.CommentContent, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentSrv) GetCommentRepliesByID(ids []int64) ([]*core.CommentReplyFormated, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentManageSrv) DeleteComment(comment *core.Comment) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *commentManageSrv) CreateComment(comment *core.Comment) (*core.Comment, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentManageSrv) CreateCommentReply(reply *core.CommentReply) (*core.CommentReply, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *commentManageSrv) DeleteCommentReply(reply *core.CommentReply) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *commentManageSrv) CreateCommentContent(content *core.CommentContent) (*core.CommentContent, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func newCommentService(db *sqlx.DB) core.CommentService { + return &commentSrv{ + sqlxSrv: newSqlxSrv(db), + stmtGetComments: c(`SELECT * FROM @person WHERE first_name=?`), + stmtGetReply: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newCommentManageService(db *sqlx.DB) core.CommentManageService { + return &commentManageSrv{ + sqlxSrv: newSqlxSrv(db), + stmtAddComents: c(`SELECT * FROM @person WHERE first_name=?`), + stmtDelComments: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/contacts.go b/internal/dao/sakila/contacts.go new file mode 100644 index 00000000..673e527e --- /dev/null +++ b/internal/dao/sakila/contacts.go @@ -0,0 +1,65 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.ContactManageService = (*contactManageSrv)(nil) +) + +type contactManageSrv struct { + *sqlxSrv + stmtAddFriend *sqlx.Stmt + stmtDelFriend *sqlx.Stmt +} + +func (s *contactManageSrv) RequestingFriend(userId int64, friendId int64, greetings string) (err error) { + // TODO + debug.NotImplemented() + return nil +} + +func (s *contactManageSrv) AddFriend(userId int64, friendId int64) (err error) { + // TODO + debug.NotImplemented() + return nil +} + +func (s *contactManageSrv) RejectFriend(userId int64, friendId int64) (err error) { + // TODO + debug.NotImplemented() + return nil +} + +func (s *contactManageSrv) DeleteFriend(userId int64, friendId int64) (err error) { + // TODO + debug.NotImplemented() + return nil +} + +func (s *contactManageSrv) GetContacts(userId int64, offset int, limit int) (*core.ContactList, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *contactManageSrv) IsFriend(userId int64, friendId int64) bool { + // TODO + debug.NotImplemented() + return false +} + +func newContactManageService(db *sqlx.DB) core.ContactManageService { + return &contactManageSrv{ + sqlxSrv: newSqlxSrv(db), + stmtAddFriend: c(`SELECT * FROM @person WHERE first_name=?`), + stmtDelFriend: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/messages.go b/internal/dao/sakila/messages.go new file mode 100644 index 00000000..c4663737 --- /dev/null +++ b/internal/dao/sakila/messages.go @@ -0,0 +1,67 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.MessageService = (*messageSrv)(nil) +) + +type messageSrv struct { + *sqlxSrv + stmtAddMsg *sqlx.Stmt + stmtGetMsg *sqlx.Stmt + stmtReadMsg *sqlx.Stmt +} + +func (s *messageSrv) CreateMessage(msg *core.Message) (*core.Message, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *messageSrv) GetUnreadCount(userID int64) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *messageSrv) GetMessageByID(id int64) (*core.Message, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *messageSrv) ReadMessage(message *core.Message) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *messageSrv) GetMessages(conditions *core.ConditionsT, offset, limit int) ([]*core.MessageFormated, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *messageSrv) GetMessageCount(conditions *core.ConditionsT) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func newMessageService(db *sqlx.DB) core.MessageService { + return &messageSrv{ + sqlxSrv: newSqlxSrv(db), + stmtAddMsg: c(`SELECT * FROM @person WHERE first_name=?`), + stmtGetMsg: c(`SELECT * FROM @person WHERE first_name=?`), + stmtReadMsg: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/sakila.go b/internal/dao/sakila/sakila.go index d82a0d23..20df522c 100644 --- a/internal/dao/sakila/sakila.go +++ b/internal/dao/sakila/sakila.go @@ -1,23 +1,114 @@ -// Copyright 2022 ROC. All rights reserved. +// Copyright 2023 ROC. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -// Core service implement base sqlx+mysql. All sub-service -// will declare here and provide initial function. - package sakila import ( + "sync" + + "github.com/Masterminds/semver/v3" + "github.com/alimy/cfg" "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/dao/cache" + "github.com/rocboss/paopao-ce/internal/dao/security" "github.com/sirupsen/logrus" ) +var ( + _ core.DataService = (*dataSrv)(nil) + _ core.VersionInfo = (*dataSrv)(nil) + + _onceInitial sync.Once +) + +type dataSrv struct { + core.IndexPostsService + core.WalletService + core.MessageService + core.TopicService + core.TweetService + core.TweetManageService + core.TweetHelpService + core.CommentService + core.CommentManageService + core.UserManageService + core.ContactManageService + core.SecurityService + core.AttachmentCheckService +} + func NewDataService() (core.DataService, core.VersionInfo) { - logrus.Fatal("not support now") - return nil, nil + lazyInitial() + + var ( + v core.VersionInfo + cis core.CacheIndexService + ips core.IndexPostsService + ) + pvs := security.NewPhoneVerifyService() + ams := NewAuthorizationManageService() + ths := newTweetHelpService(_db) + + // initialize core.IndexPostsService + if cfg.If("Friendship") { + ips = newFriendIndexService(_db, ams, ths) + } else if cfg.If("Followship") { + ips = newFollowIndexService(_db, ths) + } else if cfg.If("Lightship") { + ips = newLightIndexService(_db, ths) + } else { + // default use lightship post index service + ips = newLightIndexService(_db, ths) + } + + // initialize core.CacheIndexService + if cfg.If("SimpleCacheIndex") { + // simpleCache use special post index service + ips = newSimpleIndexPostsService(_db, ths) + cis, v = cache.NewSimpleCacheIndexService(ips) + } else if cfg.If("BigCacheIndex") { + // TODO: make cache index post in different scence like friendship/followship/lightship + cis, v = cache.NewBigCacheIndexService(ips, ams) + } else { + cis, v = cache.NewNoneCacheIndexService(ips) + } + logrus.Infof("use %s as cache index service by version: %s", v.Name(), v.Version()) + + ds := &dataSrv{ + IndexPostsService: cis, + WalletService: newWalletService(_db), + MessageService: newMessageService(_db), + TopicService: newTopicService(_db), + TweetService: newTweetService(_db), + TweetManageService: newTweetManageService(_db, cis), + TweetHelpService: newTweetHelpService(_db), + CommentService: newCommentService(_db), + CommentManageService: newCommentManageService(_db), + UserManageService: newUserManageService(_db), + ContactManageService: newContactManageService(_db), + SecurityService: newSecurityService(_db, pvs), + AttachmentCheckService: security.NewAttachmentCheckService(), + } + return ds, ds } func NewAuthorizationManageService() core.AuthorizationManageService { - logrus.Fatal("not support now") - return nil + lazyInitial() + return newAuthorizationManageService(_db) +} + +func (s *dataSrv) Name() string { + return "Sqlx" +} + +func (s *dataSrv) Version() *semver.Version { + return semver.MustParse("v0.1.0") +} + +// lazyInitial do some package lazy initialize for performance +func lazyInitial() { + _onceInitial.Do(func() { + initSqlxDB() + }) } diff --git a/internal/dao/sakila/security.go b/internal/dao/sakila/security.go new file mode 100644 index 00000000..e4bd1f86 --- /dev/null +++ b/internal/dao/sakila/security.go @@ -0,0 +1,52 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.SecurityService = (*securitySrv)(nil) +) + +type securitySrv struct { + *sqlxSrv + phoneVerify core.PhoneVerifyService + stmtAddCaptcha *sqlx.Stmt + stmtGetCaptcha *sqlx.Stmt +} + +// GetLatestPhoneCaptcha 获取最新短信验证码 +func (s *securitySrv) GetLatestPhoneCaptcha(phone string) (*core.Captcha, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +// UsePhoneCaptcha 更新短信验证码 +func (s *securitySrv) UsePhoneCaptcha(captcha *core.Captcha) error { + // TODO + debug.NotImplemented() + return nil +} + +// SendPhoneCaptcha 发送短信验证码 +func (s *securitySrv) SendPhoneCaptcha(phone string) error { + // TODO + debug.NotImplemented() + return nil +} + +func newSecurityService(db *sqlx.DB, phoneVerify core.PhoneVerifyService) core.SecurityService { + return &securitySrv{ + sqlxSrv: newSqlxSrv(db), + phoneVerify: phoneVerify, + stmtAddCaptcha: c(`SELECT * FROM @person WHERE first_name=?`), + stmtGetCaptcha: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/sqlx.go b/internal/dao/sakila/sqlx.go new file mode 100644 index 00000000..3b3be210 --- /dev/null +++ b/internal/dao/sakila/sqlx.go @@ -0,0 +1,114 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "context" + "database/sql" + "strings" + + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/conf" + "github.com/sirupsen/logrus" +) + +var ( + _db *sqlx.DB +) + +type sqlxSrv struct { + db *sqlx.DB +} + +func (s *sqlxSrv) with(handle func(tx *sqlx.Tx) error) error { + tx, err := s.db.Beginx() + if err != nil { + return err + } + defer tx.Rollback() + if err = handle(tx); err != nil { + return err + } + return tx.Commit() +} + +func (s *sqlxSrv) withTx(ctx context.Context, opts *sql.TxOptions, handle func(*sqlx.Tx) error) error { + tx, err := s.db.BeginTxx(ctx, opts) + if err != nil { + return err + } + defer tx.Rollback() + if err = handle(tx); err != nil { + return err + } + return tx.Commit() +} + +func (s *sqlxSrv) in(query string, args ...any) (string, []any, error) { + q, params, err := sqlx.In(query, args...) + if err != nil { + return "", nil, err + } + return s.db.Rebind(q), params, nil +} + +func (s *sqlxSrv) inExec(execer sqlx.Execer, query string, args ...any) (sql.Result, error) { + q, params, err := sqlx.In(query, args...) + if err != nil { + return nil, err + } + return execer.Exec(s.db.Rebind(q), params...) +} + +func (s *sqlxSrv) inSelect(queryer sqlx.Queryer, dest any, query string, args ...any) error { + q, params, err := sqlx.In(query, args...) + if err != nil { + return err + } + return sqlx.Select(queryer, dest, s.db.Rebind(q), params...) +} + +func (s *sqlxSrv) inGet(queryer sqlx.Queryer, dest any, query string, args ...any) error { + q, params, err := sqlx.In(query, args...) + if err != nil { + return err + } + return sqlx.Get(queryer, dest, s.db.Rebind(q), params...) +} + +func newSqlxSrv(db *sqlx.DB) *sqlxSrv { + return &sqlxSrv{ + db: db, + } +} + +func r(query string) string { + return _db.Rebind(t(query)) +} + +func c(query string) *sqlx.Stmt { + stmt, err := _db.Preparex(_db.Rebind(t(query))) + if err != nil { + logrus.Fatalf("prepare query(%s) error: %s", query, err) + } + return stmt +} + +func n(query string) *sqlx.NamedStmt { + stmt, err := _db.PrepareNamed(t(query)) + if err != nil { + logrus.Fatalf("prepare named query(%s) error: %s", query, err) + } + return stmt +} + +// t repace table prefix for query +func t(query string) string { + return strings.Replace(query, "@", conf.DatabaseSetting.TablePrefix, -1) +} + +func initSqlxDB() { + _db = conf.MustSqlxDB() +} diff --git a/internal/dao/sakila/timeline.go b/internal/dao/sakila/timeline.go new file mode 100644 index 00000000..5bc8485b --- /dev/null +++ b/internal/dao/sakila/timeline.go @@ -0,0 +1,120 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.IndexPostsService = (*friendIndexSrv)(nil) + _ core.IndexPostsService = (*followIndexSrv)(nil) + _ core.IndexPostsService = (*lightIndexSrv)(nil) + _ core.IndexPostsService = (*simpleIndexPostsSrv)(nil) +) + +type friendIndexSrv struct { + *sqlxSrv + ams core.AuthorizationManageService + ths core.TweetHelpService + stmtIndex *sqlx.Stmt +} + +type followIndexSrv struct { + *sqlxSrv + ths core.TweetHelpService + stmtIndex *sqlx.Stmt +} + +type lightIndexSrv struct { + *sqlxSrv + ths core.TweetHelpService + stmtIndex *sqlx.Stmt +} + +type simpleIndexPostsSrv struct { + *sqlxSrv + ths core.TweetHelpService + stmtIndex *sqlx.Stmt +} + +// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; +func (s *friendIndexSrv) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *friendIndexSrv) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; +func (s *followIndexSrv) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *followIndexSrv) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +// IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; +func (s *lightIndexSrv) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *lightIndexSrv) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +// simpleCacheIndexGetPosts simpleCacheIndex 专属获取广场推文列表函数 +func (s *simpleIndexPostsSrv) IndexPosts(_user *core.User, offset int, limit int) (*core.IndexTweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *simpleIndexPostsSrv) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func newFriendIndexService(db *sqlx.DB, ams core.AuthorizationManageService, ths core.TweetHelpService) core.IndexPostsService { + return &friendIndexSrv{ + ams: ams, + sqlxSrv: newSqlxSrv(db), + stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newFollowIndexService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { + return &followIndexSrv{ + ths: ths, + sqlxSrv: newSqlxSrv(db), + stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newLightIndexService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { + return &lightIndexSrv{ + ths: ths, + sqlxSrv: newSqlxSrv(db), + stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newSimpleIndexPostsService(db *sqlx.DB, ths core.TweetHelpService) core.IndexPostsService { + return &simpleIndexPostsSrv{ + ths: ths, + sqlxSrv: newSqlxSrv(db), + stmtIndex: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/topics.go b/internal/dao/sakila/topics.go new file mode 100644 index 00000000..815d88cd --- /dev/null +++ b/internal/dao/sakila/topics.go @@ -0,0 +1,137 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "strings" + "time" + + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" +) + +var ( + _ core.TopicService = (*topicSrv)(nil) +) + +type topicSrv struct { + *sqlxSrv + stmtNewestTags *sqlx.Stmt + stmtHotTags *sqlx.Stmt + stmtTagsByKeywordA *sqlx.Stmt + stmtTagsByKeywordB *sqlx.Stmt + stmtInsertTag *sqlx.Stmt + sqlTagsByIdA string + sqlTagsByIdB string + sqlDecrTagsById string + sqlTagsForIncr string + sqlIncrTagsById string +} + +func (s *topicSrv) UpsertTags(userId int64, tags []string) (res cs.TagInfoList, xerr error) { + if len(tags) == 0 { + return nil, nil + } + xerr = s.with(func(tx *sqlx.Tx) error { + var upTags cs.TagInfoList + if err := s.inSelect(tx, &upTags, s.sqlTagsForIncr, tags); err != nil { + return err + } + now := time.Now().Unix() + if len(upTags) > 0 { + var ids []int64 + for _, t := range upTags { + ids = append(ids, t.ID) + t.QuoteNum++ + // prepare remain tags just delete updated tag + // notice ensure tags slice is distinct elements + for i, name := range tags { + if name == t.Tag { + lastIdx := len(tags) - 1 + tags[i] = tags[lastIdx] + tags = tags[:lastIdx] + break + } + } + } + if _, err := s.inExec(tx, s.sqlIncrTagsById, now, ids); err != nil { + return err + } + res = append(res, upTags...) + } + // process remain tags if tags is not empty + if len(tags) == 0 { + return nil + } + var ids []int64 + for _, tag := range tags { + res, err := s.stmtInsertTag.Exec(userId, tag, now, now) + if err != nil { + return err + } + id, err := res.LastInsertId() + if err != nil { + return err + } + ids = append(ids, id) + } + var newTags cs.TagInfoList + if err := s.inSelect(tx, &newTags, s.sqlTagsByIdB, ids); err != nil { + return err + } + res = append(res, newTags...) + return nil + }) + return +} + +func (s *topicSrv) DecrTagsById(ids []int64) error { + return s.with(func(tx *sqlx.Tx) error { + var ids []int64 + err := s.inSelect(tx, &ids, s.sqlTagsByIdA, ids) + if err != nil { + return err + } + _, err = s.inExec(tx, s.sqlDecrTagsById, time.Now().Unix(), ids) + return err + }) +} + +func (s *topicSrv) ListTags(typ cs.TagType, limit int, offset int) (res cs.TagList, err error) { + switch typ { + case cs.TagTypeHot: + err = s.stmtHotTags.Select(&res, limit, offset) + case cs.TagTypeNew: + err = s.stmtNewestTags.Select(&res, limit, offset) + } + return +} + +func (s *topicSrv) TagsByKeyword(keyword string) (res cs.TagInfoList, err error) { + keyword = "%" + strings.Trim(keyword, " ") + "%" + if keyword == "%%" { + err = s.stmtTagsByKeywordA.Select(&res) + } else { + err = s.stmtTagsByKeywordB.Select(&res) + } + return +} + +func newTopicService(db *sqlx.DB) core.TopicService { + return &topicSrv{ + sqlxSrv: newSqlxSrv(db), + stmtNewestTags: c(`SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.id DESC LIMIT ? OFFSET ?`), + stmtHotTags: c(`SELECT t.id id, t.user_id user_id, t.tag tag, t.quote_num quote_num, u.id, u.nickname, u.username, u.status, u.avatar, u.is_admin FROM @tag t JOIN @user u ON t.user_id = u.id WHERE t.is_del = 0 AND t.quote_num > 0 ORDER BY t.quote_num DESC LIMIT ? OFFSET ?`), + stmtTagsByKeywordA: c(`SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 ORDER BY quote_num DESC LIMIT 6`), + stmtTagsByKeywordB: c(`SELECT id, user_id, tag, quote_num FROM @tag WHERE is_del = 0 AND tag LIKE ? ORDER BY quote_num DESC LIMIT 6`), + stmtInsertTag: c(`INSERT INTO @tag (user_id, tag, created_on, modified_on, quote_num) VALUES (?, ?, ?, ?, 1)`), + sqlTagsByIdA: t(`SELECT id FROM @tag WHERE id IN (?) AND is_del = 0 AND quote_num > 0`), + sqlTagsByIdB: t(`SELECT id, user_id, tag, quote_num FROM @tag WHERE id IN (?)`), + sqlDecrTagsById: t(`UPDATE @tag SET quote_num=quote_num-1, modified_on=? WHERE id IN (?)`), + sqlTagsForIncr: t(`SELECT id, user_id, tag, quote_num FROM @tag WHERE tag IN (?)`), + sqlIncrTagsById: t(`UPDATE @tag SET quote_num=quote_num+1, is_del=0, modified_on=? WHERE id IN (?)`), + } +} diff --git a/internal/dao/sakila/tweets.go b/internal/dao/sakila/tweets.go new file mode 100644 index 00000000..3802cac7 --- /dev/null +++ b/internal/dao/sakila/tweets.go @@ -0,0 +1,352 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" + "github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr" + "github.com/rocboss/paopao-ce/pkg/debug" + "gorm.io/gorm" +) + +var ( + _ core.TweetService = (*tweetSrv)(nil) + _ core.TweetManageService = (*tweetManageSrv)(nil) + _ core.TweetHelpService = (*tweetHelpSrv)(nil) +) + +type tweetSrv struct { + *sqlxSrv + stmtGetTweet *sqlx.Stmt + stmtListTweet *sqlx.Stmt + stmtListStar *sqlx.Stmt +} + +type tweetManageSrv struct { + *sqlxSrv + cacheIndex core.CacheIndexService + stmtAddTweet *sqlx.Stmt + stmtDelTweet *sqlx.Stmt + stmtStickTweet *sqlx.Stmt +} + +type tweetHelpSrv struct { + *sqlxSrv + stmtAddTag *sqlx.Stmt + stmtDelTag *sqlx.Stmt + stmtListTag *sqlx.Stmt +} + +// MergePosts post数据整合 +func (s *tweetHelpSrv) MergePosts(posts []*core.Post) ([]*core.PostFormated, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +// RevampPosts post数据整形修复 +func (s *tweetHelpSrv) RevampPosts(posts []*core.PostFormated) ([]*core.PostFormated, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetHelpSrv) RevampTweets(tweets cs.TweetList) (cs.TweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetHelpSrv) MergeTweets(tweets cs.TweetInfo) (cs.TweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetHelpSrv) getPostContentsByIDs(ids []int64) ([]*dbr.PostContent, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetHelpSrv) getUsersByIDs(ids []int64) ([]*dbr.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) CreatePostCollection(postID, userID int64) (*core.PostCollection, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) DeletePostCollection(p *core.PostCollection) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *tweetManageSrv) CreatePostContent(content *core.PostContent) (*core.PostContent, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) CreateAttachment(obj *cs.Attachment) (int64, error) { + // TODO + return 0, debug.ErrNotImplemented +} + +func (s *tweetManageSrv) CreatePost(post *core.Post) (*core.Post, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) DeletePost(post *core.Post) ([]string, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) deleteCommentByPostId(db *gorm.DB, postId int64) ([]string, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) LockPost(post *core.Post) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *tweetManageSrv) StickPost(post *core.Post) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *tweetManageSrv) VisiblePost(post *core.Post, visibility core.PostVisibleT) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *tweetManageSrv) UpdatePost(post *core.Post) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *tweetManageSrv) CreatePostStar(postID, userID int64) (*core.PostStar, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetManageSrv) DeletePostStar(p *core.PostStar) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *tweetManageSrv) CreateTweet(userId int64, req *cs.NewTweetReq) (*cs.TweetItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetManageSrv) DeleteTweet(userId int64, tweetId int64) ([]string, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetManageSrv) LockTweet(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageSrv) StickTweet(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageSrv) VisibleTweet(userId int64, visibility cs.TweetVisibleType) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageSrv) CreateReaction(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageSrv) DeleteReaction(userId int64, reactionId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageSrv) CreateFavorite(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageSrv) DeleteFavorite(userId int64, favoriteId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetSrv) GetPostByID(id int64) (*core.Post, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetPosts(conditions *core.ConditionsT, offset, limit int) ([]*core.Post, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetPostCount(conditions *core.ConditionsT) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *tweetSrv) GetUserPostStar(postID, userID int64) (*core.PostStar, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetUserPostStars(userID int64, offset, limit int) ([]*core.PostStar, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetUserPostStarCount(userID int64) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *tweetSrv) GetUserPostCollection(postID, userID int64) (*core.PostCollection, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetUserPostCollections(userID int64, offset, limit int) ([]*core.PostCollection, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetUserPostCollectionCount(userID int64) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *tweetSrv) GetUserWalletBills(userID int64, offset, limit int) ([]*core.WalletStatement, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetUserWalletBillCount(userID int64) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *tweetSrv) GetPostAttatchmentBill(postID, userID int64) (*core.PostAttachmentBill, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetPostContentsByIDs(ids []int64) ([]*core.PostContent, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) GetPostContentByID(id int64) (*core.PostContent, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *tweetSrv) TweetInfoById(id int64) (*cs.TweetInfo, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) TweetItemById(id int64) (*cs.TweetItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) UserTweets(visitorId, userId int64) (cs.TweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) ReactionByTweetId(userId int64, tweetId int64) (*cs.ReactionItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) UserReactions(userId int64, offset int, limit int) (cs.ReactionList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) FavoriteByTweetId(userId int64, tweetId int64) (*cs.FavoriteItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) UserFavorites(userId int64, offset int, limit int) (cs.FavoriteList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetSrv) AttachmentByTweetId(userId int64, tweetId int64) (*cs.AttachmentBill, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func newTweetService(db *sqlx.DB) core.TweetService { + return &tweetSrv{ + sqlxSrv: newSqlxSrv(db), + stmtGetTweet: c(`SELECT * FROM @person WHERE first_name=?`), + stmtListTweet: c(`SELECT * FROM @person WHERE first_name=?`), + stmtListStar: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newTweetManageService(db *sqlx.DB, cacheIndex core.CacheIndexService) core.TweetManageService { + return &tweetManageSrv{ + sqlxSrv: newSqlxSrv(db), + cacheIndex: cacheIndex, + stmtAddTweet: c(`SELECT * FROM @person WHERE first_name=?`), + stmtDelTweet: c(`SELECT * FROM @person WHERE first_name=?`), + stmtStickTweet: c(`SELECT * FROM @person WHERE first_name=?`), + } +} + +func newTweetHelpService(db *sqlx.DB) core.TweetHelpService { + return &tweetHelpSrv{ + sqlxSrv: newSqlxSrv(db), + stmtAddTag: c(`SELECT * FROM @person WHERE first_name=?`), + stmtDelTag: c(`SELECT * FROM @person WHERE first_name=?`), + stmtListTag: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/user.go b/internal/dao/sakila/user.go new file mode 100644 index 00000000..b0709abf --- /dev/null +++ b/internal/dao/sakila/user.go @@ -0,0 +1,73 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.UserManageService = (*userManageSrv)(nil) +) + +type userManageSrv struct { + *sqlxSrv + stmtAddUser *sqlx.Stmt + stmtUpdateUser *sqlx.Stmt + stmtGetUser *sqlx.Stmt +} + +func (s *userManageSrv) GetUserByID(id int64) (*core.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *userManageSrv) GetUserByUsername(username string) (*core.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *userManageSrv) GetUserByPhone(phone string) (*core.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *userManageSrv) GetUsersByIDs(ids []int64) ([]*core.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *userManageSrv) GetUsersByKeyword(keyword string) ([]*core.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *userManageSrv) CreateUser(user *core.User) (*core.User, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *userManageSrv) UpdateUser(user *core.User) error { + // TODO + debug.NotImplemented() + return nil +} + +func newUserManageService(db *sqlx.DB) core.UserManageService { + return &userManageSrv{ + sqlxSrv: newSqlxSrv(db), + stmtAddUser: c(`SELECT * FROM @person WHERE first_name=?`), + stmtUpdateUser: c(`SELECT * FROM @person WHERE first_name=?`), + stmtGetUser: c(`SELECT * FROM @person WHERE first_name=?`), + } +} diff --git a/internal/dao/sakila/wallet.go b/internal/dao/sakila/wallet.go new file mode 100644 index 00000000..6a48b1e1 --- /dev/null +++ b/internal/dao/sakila/wallet.go @@ -0,0 +1,66 @@ +// Copyright 2023 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package sakila + +import ( + "github.com/jmoiron/sqlx" + "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/pkg/debug" +) + +var ( + _ core.WalletService = (*walletSrv)(nil) +) + +type walletSrv struct { + *sqlxSrv + stmtAddRecharge *sqlx.Stmt + stmtGetRecharge *sqlx.Stmt + stmtGetBills *sqlx.Stmt +} + +func (s *walletSrv) GetRechargeByID(id int64) (*core.WalletRecharge, error) { + // TODO + debug.NotImplemented() + return nil, nil +} +func (s *walletSrv) CreateRecharge(userId, amount int64) (*core.WalletRecharge, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *walletSrv) GetUserWalletBills(userID int64, offset, limit int) ([]*core.WalletStatement, error) { + // TODO + debug.NotImplemented() + return nil, nil +} + +func (s *walletSrv) GetUserWalletBillCount(userID int64) (int64, error) { + // TODO + debug.NotImplemented() + return 0, nil +} + +func (s *walletSrv) HandleRechargeSuccess(recharge *core.WalletRecharge, tradeNo string) error { + // TODO + debug.NotImplemented() + return nil +} + +func (s *walletSrv) HandlePostAttachmentBought(post *core.Post, user *core.User) error { + // TODO + debug.NotImplemented() + return nil +} + +func newWalletService(db *sqlx.DB) core.WalletService { + return &walletSrv{ + sqlxSrv: newSqlxSrv(db), + stmtAddRecharge: c(`SELECT * FROM @person WHERE first_name=?`), + stmtGetRecharge: c(`SELECT * FROM @person WHERE first_name=?`), + stmtGetBills: c(`SELECT * FROM @person WHERE first_name=?`), + } +}