From 8578cd063b5e260d1a13d8fff9683b1bb961a7cb Mon Sep 17 00:00:00 2001 From: Michael Li Date: Tue, 7 Feb 2023 18:17:54 +0800 Subject: [PATCH] sqlc: prepare implement tweets data logic for sqlc feature --- internal/dao/slonik/ce/postgres/models.go | 21 ++++ .../dao/slonik/ce/postgres/query/topic.sql | 14 +-- .../schema/0001_initialize_schema.down.sql | 3 + .../schema/0001_initialize_schema.up.sql | 47 ++++++-- internal/dao/slonik/ce/postgres/topic.sql.go | 42 +++++-- internal/dao/slonik/{index.go => timeline.go} | 21 ++++ internal/dao/slonik/topics.go | 41 +++++-- internal/dao/slonik/tweets.go | 107 +++++++++++++++++- internal/dao/slonik/user.go | 6 - 9 files changed, 249 insertions(+), 53 deletions(-) rename internal/dao/slonik/{index.go => timeline.go} (80%) diff --git a/internal/dao/slonik/ce/postgres/models.go b/internal/dao/slonik/ce/postgres/models.go index f8389b70..d4d8e7ff 100644 --- a/internal/dao/slonik/ce/postgres/models.go +++ b/internal/dao/slonik/ce/postgres/models.go @@ -18,3 +18,24 @@ type PTag struct { // 是否删除 IsDel bool } + +// 用户 +type PUser struct { + ID int64 + Nickname string + Username string + Phone string + // MD5密码 + Password string + Salt string + // 状态, 1正常, 2停用 + Status int16 + Avatar string + // 用户余额(分) + Balance int64 + IsAdmin bool + CreatedOn int64 + ModifiedOn int64 + DeletedOn int64 + IsDel bool +} diff --git a/internal/dao/slonik/ce/postgres/query/topic.sql b/internal/dao/slonik/ce/postgres/query/topic.sql index 96de9bb6..b43048ca 100644 --- a/internal/dao/slonik/ce/postgres/query/topic.sql +++ b/internal/dao/slonik/ce/postgres/query/topic.sql @@ -1,14 +1,14 @@ -- name: NewestTags :many -SELECT id, user_id, tag, quote_num -FROM p_tag -WHERE is_del = false AND quote_num > 0 -ORDER BY id DESC +SELECT t.id, t.tag, t.quote_num, u.id user_id, u.nickname, u.username, u.status, u.avatar, u.is_admin +FROM p_tag t JOIN p_user u ON t.user_id = u.id +WHERE t.is_del = 0 AND t.quote_num > 0 +ORDER BY t.id DESC OFFSET $1 LIMIT $2; -- name: HotTags :many -SELECT id, user_id, tag, quote_num -FROM p_tag -WHERE is_del = false AND quote_num > 0 +SELECT t.id, t.tag, t.quote_num, u.id user_id, u.nickname, u.username, u.status, u.avatar, u.is_admin +FROM p_tag t JOIN p_user u ON t.user_id = u.id +WHERE t.is_del = false AND t.quote_num > 0 ORDER BY quote_num DESC OFFSET $1 LIMIT $2; diff --git a/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.down.sql b/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.down.sql index f447ca66..a7b22974 100644 --- a/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.down.sql +++ b/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.down.sql @@ -1,2 +1,5 @@ +DROP TABLE IF EXISTS p_user; +DROP INDEX IF EXISTS p_user_username_idx, p_user_phone_idx; + DROP TABLE IF EXISTS p_tag; DROP INDEX IF EXISTS p_tag_tag_idx, p_tag_user_idx, p_tag_quote_num_idx; diff --git a/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.up.sql b/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.up.sql index 546b5874..c097e776 100644 --- a/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.up.sql +++ b/internal/dao/slonik/ce/postgres/schema/0001_initialize_schema.up.sql @@ -1,17 +1,42 @@ +-- user ddl -- +CREATE TABLE p_user ( + id bigserial PRIMARY KEY, + nickname VARCHAR(32) NOT NULL DEFAULT '', + username VARCHAR(32) NOT NULL DEFAULT '', + phone VARCHAR(16) NOT NULL DEFAULT '', + PASSWORD VARCHAR(32) NOT NULL DEFAULT '', + salt VARCHAR(16) NOT NULL DEFAULT '', + status SMALLINT NOT NULL DEFAULT 1, + avatar VARCHAR(255) NOT NULL DEFAULT '', + balance BIGINT NOT NULL, + is_admin BOOLEAN NOT NULL DEFAULT FALSE, + created_on BIGINT NOT NULL, + modified_on BIGINT NOT NULL, + deleted_on BIGINT NOT NULL, + is_del BOOLEAN NOT NULL DEFAULT FALSE, + UNIQUE (username) +); +COMMENT ON TABLE p_user IS '用户'; +COMMENT ON COLUMN p_user.password IS 'MD5密码'; +COMMENT ON COLUMN p_user.status IS '状态, 1正常, 2停用'; +COMMENT ON COLUMN p_user.balance IS '用户余额(分)'; +CREATE UNIQUE INDEX p_user_username_idx ON p_user (username); +CREATE INDEX p_user_phone_idx ON p_user (phone); + +-- tag ddl -- CREATE TABLE p_tag ( - id bigserial PRIMARY KEY, - user_id bigserial NOT NULL DEFAULT 0, - tag varchar(255) NOT NULL, - quote_num bigint NOT NULL DEFAULT 0, - created_on bigint NOT NULL DEFAULT 0, - modified_on bigint NOT NULL DEFAULT 0, - deleted_on bigint NOT NULL DEFAULT 0, - is_del boolean NOT NULL DEFAULT false, - UNIQUE (tag) + id bigserial PRIMARY KEY, + user_id bigserial NOT NULL, + tag VARCHAR(255) NOT NULL, + quote_num BIGINT NOT NULL DEFAULT 0, + created_on BIGINT NOT NULL DEFAULT 0, + modified_on BIGINT NOT NULL DEFAULT 0, + deleted_on BIGINT NOT NULL DEFAULT 0, + is_del BOOLEAN NOT NULL DEFAULT FALSE, + UNIQUE (tag) ); COMMENT ON TABLE p_tag IS '主题标签'; COMMENT ON COLUMN p_tag.is_del IS '是否删除'; CREATE UNIQUE INDEX p_tag_tag_idx ON p_tag (tag); CREATE INDEX p_tag_user_idx ON p_tag (user_id); -CREATE INDEX p_tag_quote_num_idx On p_tag (quote_num); - +CREATE INDEX p_tag_quote_num_idx ON p_tag (quote_num); diff --git a/internal/dao/slonik/ce/postgres/topic.sql.go b/internal/dao/slonik/ce/postgres/topic.sql.go index 63027dd9..79a9b757 100644 --- a/internal/dao/slonik/ce/postgres/topic.sql.go +++ b/internal/dao/slonik/ce/postgres/topic.sql.go @@ -33,9 +33,9 @@ func (q *Queries) DecrTagsById(ctx context.Context, arg *DecrTagsByIdParams) err } const hotTags = `-- name: HotTags :many -SELECT id, user_id, tag, quote_num -FROM p_tag -WHERE is_del = false AND quote_num > 0 +SELECT t.id, t.tag, t.quote_num, u.id user_id, u.nickname, u.username, u.status, u.avatar, u.is_admin +FROM p_tag t JOIN p_user u ON t.user_id = u.id +WHERE t.is_del = false AND t.quote_num > 0 ORDER BY quote_num DESC OFFSET $1 LIMIT $2 ` @@ -47,9 +47,14 @@ type HotTagsParams struct { type HotTagsRow struct { ID int64 - UserID int64 Tag string QuoteNum int64 + UserID int64 + Nickname string + Username string + Status int16 + Avatar string + IsAdmin bool } func (q *Queries) HotTags(ctx context.Context, arg *HotTagsParams) ([]*HotTagsRow, error) { @@ -63,9 +68,14 @@ func (q *Queries) HotTags(ctx context.Context, arg *HotTagsParams) ([]*HotTagsRo var i HotTagsRow if err := rows.Scan( &i.ID, - &i.UserID, &i.Tag, &i.QuoteNum, + &i.UserID, + &i.Nickname, + &i.Username, + &i.Status, + &i.Avatar, + &i.IsAdmin, ); err != nil { return nil, err } @@ -147,10 +157,10 @@ func (q *Queries) InsertTags(ctx context.Context, arg *InsertTagsParams) (int64, } const newestTags = `-- name: NewestTags :many -SELECT id, user_id, tag, quote_num -FROM p_tag -WHERE is_del = false AND quote_num > 0 -ORDER BY id DESC +SELECT t.id, t.tag, t.quote_num, u.id user_id, u.nickname, u.username, u.status, u.avatar, u.is_admin +FROM p_tag t JOIN p_user u ON t.user_id = u.id +WHERE t.is_del = 0 AND t.quote_num > 0 +ORDER BY t.id DESC OFFSET $1 LIMIT $2 ` @@ -161,9 +171,14 @@ type NewestTagsParams struct { type NewestTagsRow struct { ID int64 - UserID int64 Tag string QuoteNum int64 + UserID int64 + Nickname string + Username string + Status int16 + Avatar string + IsAdmin bool } func (q *Queries) NewestTags(ctx context.Context, arg *NewestTagsParams) ([]*NewestTagsRow, error) { @@ -177,9 +192,14 @@ func (q *Queries) NewestTags(ctx context.Context, arg *NewestTagsParams) ([]*New var i NewestTagsRow if err := rows.Scan( &i.ID, - &i.UserID, &i.Tag, &i.QuoteNum, + &i.UserID, + &i.Nickname, + &i.Username, + &i.Status, + &i.Avatar, + &i.IsAdmin, ); err != nil { return nil, err } diff --git a/internal/dao/slonik/index.go b/internal/dao/slonik/timeline.go similarity index 80% rename from internal/dao/slonik/index.go rename to internal/dao/slonik/timeline.go index 2ae7cf8c..e6b9eeb6 100644 --- a/internal/dao/slonik/index.go +++ b/internal/dao/slonik/timeline.go @@ -7,6 +7,7 @@ package slonik import ( "github.com/jackc/pgx/v5" "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" "github.com/rocboss/paopao-ce/pkg/debug" ) @@ -44,24 +45,44 @@ func (s *friendIndexServant) IndexPosts(user *core.User, offset int, limit int) return nil, debug.ErrNotImplemented } +func (s *friendIndexServant) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + // IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; func (s *followIndexServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { // TODO return nil, debug.ErrNotImplemented } +func (s *followIndexServant) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + // IndexPosts 根据userId查询广场推文列表,简单做到不同用户的主页都是不同的; func (s *lightIndexServant) IndexPosts(user *core.User, offset int, limit int) (*core.IndexTweetList, error) { // TODO return nil, debug.ErrNotImplemented } +func (s *lightIndexServant) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + // simpleCacheIndexGetPosts simpleCacheIndex 专属获取广场推文列表函数 func (s *simpleIndexPostsServant) IndexPosts(_user *core.User, offset int, limit int) (*core.IndexTweetList, error) { // TODO return nil, debug.ErrNotImplemented } +func (s *simpleIndexPostsServant) TweetTimeline(userId int64, offset int, limit int) (*cs.TweetBox, error) { + // TODO + return nil, debug.ErrNotImplemented +} + func newFriendIndexService(db *pgx.Conn, ams core.AuthorizationManageService, ths core.TweetHelpService) core.IndexPostsService { return &friendIndexServant{ ams: ams, diff --git a/internal/dao/slonik/topics.go b/internal/dao/slonik/topics.go index 1e149b38..70bfc9ee 100644 --- a/internal/dao/slonik/topics.go +++ b/internal/dao/slonik/topics.go @@ -11,6 +11,7 @@ import ( "github.com/jackc/pgx/v5" "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" dbr "github.com/rocboss/paopao-ce/internal/dao/slonik/ce/postgres" "github.com/rocboss/paopao-ce/pkg/types" ) @@ -25,7 +26,7 @@ type topicServant struct { // UpsertTags update/insert tags info. // Assume tags slice is distinct elements. -func (s *topicServant) UpsertTags(userId int64, tags []string) (res []*core.Tag, err error) { +func (s *topicServant) UpsertTags(userId int64, tags []string) (res cs.TagInfoList, err error) { err = s.with(func(c context.Context, q dbr.Querier) error { now := time.Now().Unix() upTags, err := q.IncrTags(c, &dbr.IncrTagsParams{ @@ -45,7 +46,7 @@ func (s *topicServant) UpsertTags(userId int64, tags []string) (res []*core.Tag, break } } - res = append(res, &core.Tag{ + res = append(res, &cs.TagInfo{ ID: t.ID, UserID: t.UserID, Tag: t.Tag, @@ -62,7 +63,7 @@ func (s *topicServant) UpsertTags(userId int64, tags []string) (res []*core.Tag, if err != nil { return err } - res = append(res, &core.Tag{ + res = append(res, &cs.TagInfo{ ID: id, UserID: userId, Tag: tag, @@ -81,40 +82,56 @@ func (s *topicServant) DecrTagsById(ids []int64) error { }) } -func (s *topicServant) GetTags(category core.TagCategory, offset int, limit int) (res []*core.Tag, _ error) { +func (s *topicServant) ListTags(typ cs.TagType, offset int, limit int) (res cs.TagList, _ error) { ctx := context.Background() - switch category { - case core.TagCategoryHot: + switch typ { + case cs.TagTypeHot: tags, err := s.q.HotTags(ctx, &dbr.HotTagsParams{Offset: int32(offset), Limit: int32(limit)}) if err != nil { return nil, err } for _, tag := range tags { - res = append(res, &core.Tag{ + res = append(res, &cs.TagItem{ ID: tag.ID, UserID: tag.UserID, Tag: tag.Tag, QuoteNum: tag.QuoteNum, + User: &cs.UserInfo{ + ID: tag.UserID, + Nickname: tag.Nickname, + Username: tag.Username, + Status: int(tag.Status), + Avatar: tag.Avatar, + IsAdmin: tag.IsAdmin, + }, }) } - case core.TagCategoryNew: + case cs.TagTypeNew: tags, err := s.q.NewestTags(ctx, &dbr.NewestTagsParams{Offset: int32(offset), Limit: int32(limit)}) if err != nil { return nil, err } for _, tag := range tags { - res = append(res, &core.Tag{ + res = append(res, &cs.TagItem{ ID: tag.ID, UserID: tag.UserID, Tag: tag.Tag, QuoteNum: tag.QuoteNum, + User: &cs.UserInfo{ + ID: tag.UserID, + Nickname: tag.Nickname, + Username: tag.Username, + Status: int(tag.Status), + Avatar: tag.Avatar, + IsAdmin: tag.IsAdmin, + }, }) } } return } -func (s *topicServant) GetTagsByKeyword(keyword string) (res []*core.Tag, _ error) { +func (s *topicServant) TagsByKeyword(keyword string) (res cs.TagInfoList, _ error) { ctx := context.Background() keyword = "%" + strings.Trim(keyword, " ") + "%" if keyword == "%%" { @@ -123,7 +140,7 @@ func (s *topicServant) GetTagsByKeyword(keyword string) (res []*core.Tag, _ erro return nil, err } for _, tag := range tags { - res = append(res, &core.Tag{ + res = append(res, &cs.TagInfo{ ID: tag.ID, UserID: tag.UserID, Tag: tag.Tag, @@ -136,7 +153,7 @@ func (s *topicServant) GetTagsByKeyword(keyword string) (res []*core.Tag, _ erro return nil, err } for _, tag := range tags { - res = append(res, &core.Tag{ + res = append(res, &cs.TagInfo{ ID: tag.ID, UserID: tag.UserID, Tag: tag.Tag, diff --git a/internal/dao/slonik/tweets.go b/internal/dao/slonik/tweets.go index f07ff198..5b6185d4 100644 --- a/internal/dao/slonik/tweets.go +++ b/internal/dao/slonik/tweets.go @@ -7,6 +7,7 @@ package slonik import ( "github.com/jackc/pgx/v5" "github.com/rocboss/paopao-ce/internal/core" + "github.com/rocboss/paopao-ce/internal/core/cs" "github.com/rocboss/paopao-ce/pkg/debug" "gorm.io/gorm" ) @@ -43,6 +44,16 @@ func (s *tweetHelpServant) RevampPosts(posts []*core.PostFormated) ([]*core.Post return nil, nil } +func (s *tweetHelpServant) RevampTweets(tweets cs.TweetList) (cs.TweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetHelpServant) MergeTweets(tweets cs.TweetInfo) (cs.TweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + func (s *tweetHelpServant) getPostContentsByIDs(ids []int64) ([]*core.PostContent, error) { // TODO debug.NotImplemented() @@ -73,12 +84,6 @@ func (s *tweetManageServant) CreatePostContent(content *core.PostContent) (*core return nil, nil } -func (s *tweetManageServant) CreateAttachment(attachment *core.Attachment) (*core.Attachment, error) { - // TODO - debug.NotImplemented() - return nil, nil -} - func (s *tweetManageServant) CreatePost(post *core.Post) (*core.Post, error) { // TODO debug.NotImplemented() @@ -133,6 +138,56 @@ func (s *tweetManageServant) DeletePostStar(p *core.PostStar) error { return nil } +func (s *tweetManageServant) CreateAttachment(obj *cs.Attachment) (int64, error) { + // TODO + return 0, debug.ErrNotImplemented +} + +func (s *tweetManageServant) CreateTweet(userId int64, req *cs.NewTweetReq) (*cs.TweetItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetManageServant) DeleteTweet(userId int64, tweetId int64) ([]string, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetManageServant) LockTweet(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageServant) StickTweet(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageServant) VisibleTweet(userId int64, visibility cs.TweetVisibleType) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageServant) CreateReaction(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageServant) DeleteReaction(userId int64, reactionId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageServant) CreateFavorite(userId int64, tweetId int64) error { + // TODO + return debug.ErrNotImplemented +} + +func (s *tweetManageServant) DeleteFavorite(userId int64, favoriteId int64) error { + // TODO + return debug.ErrNotImplemented +} + func (s *tweetServant) GetPostByID(id int64) (*core.Post, error) { // TODO debug.NotImplemented() @@ -217,6 +272,46 @@ func (s *tweetServant) GetPostContentByID(id int64) (*core.PostContent, error) { return nil, nil } +func (s *tweetServant) TweetInfoById(id int64) (*cs.TweetInfo, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) TweetItemById(id int64) (*cs.TweetItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) UserTweets(visitorId, userId int64) (cs.TweetList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) ReactionByTweetId(userId int64, tweetId int64) (*cs.ReactionItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) UserReactions(userId int64, offset int, limit int) (cs.ReactionList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) FavoriteByTweetId(userId int64, tweetId int64) (*cs.FavoriteItem, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) UserFavorites(userId int64, offset int, limit int) (cs.FavoriteList, error) { + // TODO + return nil, debug.ErrNotImplemented +} + +func (s *tweetServant) AttachmentByTweetId(userId int64, tweetId int64) (*cs.AttachmentBill, error) { + // TODO + return nil, debug.ErrNotImplemented +} + func newTweetService(db *pgx.Conn) core.TweetService { return &tweetServant{ pgxServant: newPgxServant(db), diff --git a/internal/dao/slonik/user.go b/internal/dao/slonik/user.go index 94035833..382a971e 100644 --- a/internal/dao/slonik/user.go +++ b/internal/dao/slonik/user.go @@ -48,12 +48,6 @@ func (s *userManageServant) GetUsersByKeyword(keyword string) ([]*core.User, err return nil, nil } -func (s *userManageServant) GetTagsByKeyword(keyword string) ([]*core.Tag, error) { - // TODO - debug.NotImplemented() - return nil, nil -} - func (s *userManageServant) CreateUser(user *core.User) (*core.User, error) { // TODO debug.NotImplemented()