sqlx: add following data logic implement WIP: 100%

r/paopao-ce-plus
Michael Li 1 year ago
parent 2a717d5a9a
commit 5ff800a904
No known key found for this signature in database

@ -14,7 +14,7 @@ type Following struct {
*Model
User *User `json:"-" gorm:"foreignKey:ID;references:FollowId"`
UserId int64 `json:"user_id"`
FollowId int64 `json:"friend_id"`
FollowId int64 `json:"follow_id"`
}
func (f *Following) GetFollowing(db *gorm.DB, userId, followId int64) (*Following, error) {

@ -5,9 +5,10 @@
package sakila
import (
"time"
"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/core/ms"
"github.com/rocboss/paopao-ce/internal/dao/sakila/yesql/cc"
)
@ -16,38 +17,79 @@ var (
_ core.FollowingManageService = (*followingManageSrv)(nil)
)
type followItem struct {
UserId int64
Username string
Nickname string
Avatar string
}
type followingManageSrv struct {
*sqlxSrv
q *cc.FollowingManager
}
func (s *followingManageSrv) FollowUser(userId int64, followId int64) error {
// TODO
return cs.ErrNotImplemented
func (s *followingManageSrv) FollowUser(userId int64, followId int64) (err error) {
exist := false
if err = s.q.ExistFollowing.Get(&exist, userId, followId); err == nil && exist {
return
}
_, err = s.q.CreateFollowing.Exec(userId, followId, time.Now().Unix())
return
}
func (s *followingManageSrv) UnfollowUser(userId int64, followId int64) error {
// TDOO
return cs.ErrNotImplemented
func (s *followingManageSrv) UnfollowUser(userId int64, followId int64) (err error) {
_, err = s.q.DeleteFollowing.Exec(time.Now().Unix(), userId, followId)
return
}
func (s *followingManageSrv) ListFollows(userId int64, limit, offset int) (*ms.ContactList, error) {
// TODO
return nil, cs.ErrNotImplemented
func (s *followingManageSrv) ListFollows(userId int64, limit, offset int) (res *ms.ContactList, err error) {
follows := []followItem{}
res = &ms.ContactList{}
if err = s.q.ListFollows.Select(&follows, userId, limit, offset); err == nil {
err = s.q.CountFollows.Get(&res.Total, userId)
}
for _, f := range follows {
res.Contacts = append(res.Contacts, ms.ContactItem{
UserId: f.UserId,
Username: f.Username,
Nickname: f.Nickname,
Avatar: f.Avatar,
IsFollow: true,
})
}
return
}
func (s *followingManageSrv) ListFollowings(userId int64, limit, offset int) (*ms.ContactList, error) {
// TODO
return nil, cs.ErrNotImplemented
func (s *followingManageSrv) ListFollowings(userId int64, limit, offset int) (res *ms.ContactList, err error) {
followings := []followItem{}
res = &ms.ContactList{}
if err = s.q.ListFollowings.Select(&followings, userId, limit, offset); err == nil {
err = s.q.CountFollowings.Get(&res.Total, userId)
}
for _, f := range followings {
res.Contacts = append(res.Contacts, ms.ContactItem{
UserId: f.UserId,
Username: f.Username,
Nickname: f.Nickname,
Avatar: f.Avatar,
IsFollow: s.IsFollow(userId, f.UserId),
})
}
return
}
func (s *followingManageSrv) GetFollowCount(userId int64) (int64, int64, error) {
// TODO
return 0, 0, cs.ErrNotImplemented
func (s *followingManageSrv) GetFollowCount(userId int64) (follows int64, followings int64, err error) {
if err = s.q.CountFollows.Get(&follows); err == nil {
err = s.q.CountFollowings.Get(&followings)
}
return
}
func (s *followingManageSrv) IsFollow(userId int64, followId int64) bool {
// TODO
func (s *followingManageSrv) IsFollow(userId int64, followId int64) (yn bool) {
if err := s.q.ExistFollowing.Get(&yn, userId, followId); err == nil {
return
}
return false
}

@ -50,7 +50,13 @@ const (
_ContactManager_ListFriend = `SELECT c.friend_id user_id, u.username username, u.nickname nickname, u.avatar avatar, u.phone phone FROM @contact c JOIN @user u ON c.friend_id=u.id WHERE user_id=? AND status=2 AND is_del=0 ORDER BY u.nickname ASC LIMIT ? OFFSET ?`
_ContactManager_RejectFriendMsgsUpdate = `UPDATE @message SET reply_id=?, modified_on=? WHERE sender_user_id = ? AND receiver_user_id = ? AND type = ? AND reply_id = ?`
_ContactManager_TotalFriendsById = `SELECT count(*) FROM @contact WHERE user_id=? AND status=2 AND is_del=0`
_FollowingManager_CountFollowings = `SELECT count(*) FROM @following WHERE follow_id=? AND is_del=0`
_FollowingManager_CountFollows = `SELECT count(*) FROM @following WHERE user_id=? AND is_del=0`
_FollowingManager_CreateFollowing = `INSERT INTO @following (user_id, follow_id, created_on) VALUES (?, ?, ?)`
_FollowingManager_DeleteFollowing = `UPDATE @following SET is_del=0, deleted_on=? WHERE user_id=? AND follow_id=? AND is_del=0`
_FollowingManager_ExistFollowing = `SELECT 1 FROM @following WHERE user_id=? AND follow_id AND is_del=0`
_FollowingManager_ListFollowings = `SELECT u.user_id user_id, u.username username, u.nickname nickname, u.avatar avatar FROM @following f JOIN @user u ON f.user_id=u.id WHERE f.follow_id=? AND f.is_del=0 ORDER BY u.nickname ASC LIMIT ? OFFSET ?`
_FollowingManager_ListFollows = `SELECT u.user_id user_id, u.username username, u.nickname nickname, u.avatar avatar FROM @following f JOIN @user u ON f.follow_id=u.id WHERE f.user_id=? AND f.is_del=0 ORDER BY u.nickname ASC LIMIT ? OFFSET ?`
_Message_CreateMessage = `INSERT INTO @message (sender_user_id, receiver_user_id, type, brief, content, post_id, comment_id, reply_id, created_on) VALUES (:sender_user_id, :receiver_user_id, :type, :brief, :content, :post_id, :comment_id, :reply_id, :created_on)`
_Message_GetMessageById = `SELECT * FROM @message WHERE id=? AND is_del=0`
_Message_GetMessageCount = `SELECT count(*) FROM @message WHERE receiver_user_id=:recerver_user_id AND is_del=0`
@ -231,7 +237,13 @@ type ContactManager struct {
type FollowingManager struct {
yesql.Namespace `yesql:"following_manager"`
CountFollowings *sqlx.Stmt `yesql:"count_followings"`
CountFollows *sqlx.Stmt `yesql:"count_follows"`
CreateFollowing *sqlx.Stmt `yesql:"create_following"`
DeleteFollowing *sqlx.Stmt `yesql:"delete_following"`
ExistFollowing *sqlx.Stmt `yesql:"exist_following"`
ListFollowings *sqlx.Stmt `yesql:"list_followings"`
ListFollows *sqlx.Stmt `yesql:"list_follows"`
}
type Message struct {
@ -582,9 +594,27 @@ func BuildFollowingManager(p yesql.PreparexBuilder, ctx ...context.Context) (obj
c = context.Background()
}
obj = &FollowingManager{}
if obj.CountFollowings, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_CountFollowings))); err != nil {
return
}
if obj.CountFollows, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_CountFollows))); err != nil {
return
}
if obj.CreateFollowing, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_CreateFollowing))); err != nil {
return
}
if obj.DeleteFollowing, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_DeleteFollowing))); err != nil {
return
}
if obj.ExistFollowing, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_ExistFollowing))); err != nil {
return
}
if obj.ListFollowings, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_ListFollowings))); err != nil {
return
}
if obj.ListFollows, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_FollowingManager_ListFollows))); err != nil {
return
}
return
}

@ -149,6 +149,46 @@ WHERE id=? AND is_del=0;
-- prepare: stmt
INSERT INTO @following (user_id, follow_id, created_on) VALUES (?, ?, ?);
-- name: exist_following@following_manager
-- prepare: stmt
SELECT 1 FROM @following WHERE user_id=? AND follow_id=? AND is_del=0;
-- name: delete_following@following_manager
-- prepare: stmt
UPDATE @following
SET is_del=0, deleted_on=?
WHERE user_id=? AND follow_id=? AND is_del=0;
-- name: list_follows@following_manager
-- prepare: stmt
SELECT u.user_id user_id,
u.username username,
u.nickname nickname,
u.avatar avatar
FROM @following f JOIN @user u ON f.follow_id=u.id
WHERE f.user_id=? AND f.is_del=0
ORDER BY u.nickname ASC
LIMIT ? OFFSET ?;
-- name: count_follows@following_manager
-- prepare: stmt
SELECT count(*) FROM @following WHERE user_id=? AND is_del=0;
-- name: list_followings@following_manager
-- prepare: stmt
SELECT u.user_id user_id,
u.username username,
u.nickname nickname,
u.avatar avatar
FROM @following f JOIN @user u ON f.user_id=u.id
WHERE f.follow_id=? AND f.is_del=0
ORDER BY u.nickname ASC
LIMIT ? OFFSET ?;
-- name: count_followings@following_manager
-- prepare: stmt
SELECT count(*) FROM @following WHERE follow_id=? AND is_del=0;
--------------------------------------------------------------------------------
-- contact_manager sql dml
--------------------------------------------------------------------------------

Loading…
Cancel
Save