sqlx: contact logic is implemented complete. WIP %30

r/paopao-ce-plus
Michael Li 1 year ago
parent 0bb4810690
commit 7daa78cb12
No known key found for this signature in database

@ -21,4 +21,6 @@ type Contact struct {
IsTop int8 `json:"is_top"`
IsBlack int8 `json:"is_black"`
NoticeEnable int8 `json:"notice_enable"`
IsDel int8 `json:"-"`
DeletedOn int64 `db:"-" json:"-"`
}

@ -7,7 +7,7 @@ package ms
type (
ContactItem struct {
UserId int64 `json:"user_id"`
UserName string `json:"username"`
UserName string `db:"username" json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Phone string `json:"phone"`

@ -5,11 +5,14 @@
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"
"github.com/sirupsen/logrus"
)
var (
@ -29,49 +32,159 @@ type contactManageSrv struct {
q *cc.ContactManager
}
func (s *contactManageSrv) RequestingFriend(userId int64, friendId int64, greetings string) (err error) {
_, err = s.q.RejectFriend.Query(userId, friendId, greetings)
return
func (s *contactManageSrv) RequestingFriend(userId int64, friendId int64, greetings string) error {
return s.with(func(tx *sqlx.Tx) error {
contact, err := s.fetchOrNewContact(tx, userId, friendId, cs.ContactStatusRequesting)
if err != nil {
return err
}
now := time.Now().Unix()
// 如果已经好友,啥也不干
if contact.Status == cs.ContactStatusAgree {
return nil
} else if contact.Status == cs.ContactStatusReject || contact.Status == cs.ContactStatusDeleted {
contact.Status = cs.ContactStatusRequesting
contact.IsDel = 0 // remove deleted flag if needed
args := []any{cs.ContactStatusRequesting, now, contact.ID}
if _, err = tx.Stmtx(s.q.FreshContactStatus).Exec(args...); err != nil {
return err
}
}
msg := &ms.Message{
SenderUserID: userId,
ReceiverUserID: friendId,
Type: ms.MsgTypeRequestingFriend,
Brief: "请求添加好友,并附言:",
Content: greetings,
ReplyID: int64(cs.ContactStatusRequesting),
Model: &ms.Model{
CreatedOn: now,
},
}
if _, err = tx.NamedStmt(s.q.CreateMessage).Exec(msg); err != nil {
logrus.Errorf("contactManageSrv.RequestingFriend create message err:%s", err)
return err
}
return nil
})
}
func (s *contactManageSrv) AddFriend(userId int64, friendId int64) (err error) {
_, err = s.q.AddFriend.Exec(userId, friendId)
return
func (s *contactManageSrv) AddFriend(userId int64, friendId int64) error {
return s.with(func(tx *sqlx.Tx) error {
contact := &cs.Contact{}
err := tx.Stmtx(s.q.GetUserFriend).Get(contact, userId, friendId)
if err != nil {
return err
}
// 如果还不是请求好友,啥也不干
if contact.Status != cs.ContactStatusRequesting {
logrus.Debugf("contactManageSrv.AddFriend not reuesting status now so skip")
return nil
}
now := time.Now().Unix()
args := []any{cs.ContactStatusAgree, now, contact.ID}
if _, err = tx.Stmtx(s.q.FreshContactStatus).Exec(args...); err != nil {
return err
}
contact, err = s.fetchOrNewContact(tx, userId, friendId, cs.ContactStatusAgree)
if err != nil {
return err
}
// 如果已经好友,啥也不干
if contact.Status != cs.ContactStatusAgree {
args = []any{cs.ContactStatusAgree, now, contact.ID}
if _, err = tx.Stmtx(s.q.FreshContactStatus).Exec(args); err != nil {
return err
}
}
args = []any{cs.ContactStatusAgree, now, userId, friendId, friendId, userId, ms.MsgTypeRequestingFriend, cs.ContactStatusRequesting}
if _, err = tx.Stmtx(s.q.AddFriendMsgsUpdate).Exec(args...); err != nil {
return err
}
return nil
})
}
func (s *contactManageSrv) RejectFriend(userId int64, friendId int64) (err error) {
_, err = s.q.RejectFriend.Exec(userId, friendId)
return
func (s *contactManageSrv) RejectFriend(userId int64, friendId int64) error {
return s.with(func(tx *sqlx.Tx) error {
contact := &cs.Contact{}
err := tx.Stmtx(s.q.GetUserFriend).Get(contact, userId, friendId)
if err != nil {
return err
}
// 如果还不是请求好友,啥也不干
if contact.Status != cs.ContactStatusRequesting {
return nil
}
now := time.Now().Unix()
args := []any{cs.ContactStatusReject, now, contact.ID}
if _, err = tx.Stmtx(s.q.FreshContactStatus).Exec(args...); err != nil {
return err
}
args = []any{cs.ContactStatusReject, now, friendId, userId, ms.MsgTypeRequestingFriend, cs.ContactStatusRequesting}
if _, err = tx.Stmtx(s.q.RejectFriendMsgsUpdate).Exec(args...); err != nil {
return err
}
return nil
})
}
func (s *contactManageSrv) DeleteFriend(userId int64, friendId int64) (err error) {
_, err = s.q.DelFriend.Exec(userId, friendId)
return
func (s *contactManageSrv) DeleteFriend(userId int64, friendId int64) error {
return s.with(func(tx *sqlx.Tx) error {
var contacts []cs.Contact
err := tx.Stmtx(s.q.GetContacts).Select(&contacts, userId, friendId, friendId, userId)
if err != nil {
return err
}
for _, contact := range contacts {
// 如果还不是好友,啥也不干
if contact.Status != cs.ContactStatusAgree {
continue
}
if _, err = tx.Stmtx(s.q.DelFriend).Exec(time.Now().Unix(), contact.ID); err != nil {
return err
}
}
return nil
})
}
func (s *contactManageSrv) GetContacts(userId int64, offset int, limit int) (*ms.ContactList, error) {
res := &ms.ContactList{
func (s *contactManageSrv) GetContacts(userId int64, offset int, limit int) (res *ms.ContactList, err error) {
res = &ms.ContactList{
Contacts: []ms.ContactItem{},
}
if err := s.q.GetContacts.Select(&res.Contacts, userId, limit, offset); err != nil {
return nil, err
if err = s.q.ListFriend.Select(&res.Contacts, userId, limit, offset); err != nil {
return
}
if err := s.q.TotalContactsById.Get(&res.Total, userId); err != nil {
return nil, err
if err = s.q.TotalFriendsById.Get(&res.Total, userId); err != nil {
return
}
return res, nil
return
}
func (s *contactManageSrv) IsFriend(userId int64, friendId int64) bool {
ct := &contact{
UserId: friendId,
FriendId: userId,
func (s *contactManageSrv) IsFriend(userId int64, friendId int64) (res bool) {
s.q.IsFriend.Get(&res, userId, friendId)
return
}
func (s *contactManageSrv) fetchOrNewContact(tx *sqlx.Tx, userId int64, friendId int64, status int8) (res *cs.Contact, err error) {
if err = tx.Stmtx(s.q.GetContact).Get(res, userId, friendId); err == nil {
return
}
result, xerr := tx.Stmtx(s.q.CreateContact).Exec(userId, friendId, status, time.Now().Unix())
if xerr != nil {
return nil, xerr
}
err := s.q.GetUserFriend.Get(ct, userId, friendId)
if err == nil && ct.Status == cs.ContactStatusAgree {
return true
id, xerr := result.LastInsertId()
if xerr != nil {
return nil, xerr
}
return false
return &cs.Contact{
ID: id,
UserId: userId,
FriendId: friendId,
Status: status,
}, nil
}
func newContactManageService(db *sqlx.DB) core.ContactManageService {

@ -36,13 +36,18 @@ const (
_CommentManage_UpdateCommentThumbsCount = `UPDATE @comment SET thumbs_up_count=?, thumbs_down_count=?, modified_on=? WHERE id=? AND is_del=0`
_CommentManage_UpdateReplyThumbsCount = `UPDATE @comment_reply SET thumbs_up_count=?, thumbs_down_count=?, modified_on=? WHERE id=? AND is_del=0`
_CommentManage_UpdateThumbsUpdownComment = `UPDATE @tweet_comment_thumbs SET is_thumbs_up=:is_thumbs_up, is_thumbs_down=:is_thumbs_down, modified_on=:modified_on WHERE id=:id AND is_del=0`
_ContactManager_AddFriend = `SELECT * FROM @user WHERE username=?`
_ContactManager_DelFriend = `SELECT * FROM @user WHERE username=?`
_ContactManager_GetContacts = `SELECT * FROM @user WHERE username=?`
_ContactManager_GetUserFriend = `SELECT * FROM @user WHERE username=?`
_ContactManager_RejectFriend = `SELECT * FROM @user WHERE username=?`
_ContactManager_RequestingFriend = `SELECT * FROM @user WHERE username=?`
_ContactManager_TotalContactsById = `SELECT * FROM @user WHERE username=?`
_ContactManager_AddFriendMsgsUpdate = `UPDATE @message SET reply_id=?, modified_on=? WHERE ((sender_user_id = ? AND receiver_user_id = ?) OR (sender_user_id = ? AND receiver_user_id = ?)) AND type = ? AND reply_id = ?`
_ContactManager_CreateContact = `INSERT INTO @contact (user_id, friend_id, status, created_on) VALUES (?, ?, ?, ?)`
_ContactManager_CreateMessage = `INSERT INTO @message (sender_user_id, receiver_user_id, type, brief, content, reply_id, created_on) VALUES (:sender_user_id, :receiver_user_id, :type, :brief, :content, :reply_id, :created_on)`
_ContactManager_DelFriend = `UPDATE @contact SET status=4, is_del=1, deleted_on=? WHERE id=?`
_ContactManager_FreshContactStatus = `UPDATE @contact SET status=?, modified_on=?, is_del=0 WHERE id=?`
_ContactManager_GetContact = `SELECT id, user_id, friend_id, group_id, remark, status, is_top, is_black, notice_enable, is_del FROM @contact WHERE user_id=? AND friend_id=?`
_ContactManager_GetContacts = `SELECT id, user_id, friend_id, group_id, remark, status, is_top, is_black, notice_enable, is_del FROM @contact WHERE (user_id=? AND friend_id=?) OR (user_id=? AND friend_id=?)`
_ContactManager_GetUserFriend = `SELECT id, user_id, friend_id, group_id, remark, status, is_top, is_black, notice_enable, is_del FROM @contact WHERE user_id=? AND friend_id=? AND is_del=0`
_ContactManager_IsFriend = `SELECT true FROM @contact WHERE user_id=? AND friend_id=? AND is_del=0 AND status=2`
_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 LIMIT ? OFFSET ?`
_FollowIndexA_UserInfo = `SELECT * FROM @user WHERE username=?`
_FollowIndex_UserInfo = `SELECT * FROM @user WHERE username=?`
_FriendIndexA_UserInfo = `SELECT * FROM @user WHERE username=?`
@ -163,14 +168,19 @@ type CommentManage struct {
}
type ContactManager struct {
yesql.Namespace `yesql:"contact_manager"`
AddFriend *sqlx.Stmt `yesql:"add_friend"`
DelFriend *sqlx.Stmt `yesql:"del_friend"`
GetContacts *sqlx.Stmt `yesql:"get_contacts"`
GetUserFriend *sqlx.Stmt `yesql:"get_user_friend"`
RejectFriend *sqlx.Stmt `yesql:"reject_friend"`
RequestingFriend *sqlx.Stmt `yesql:"requesting_friend"`
TotalContactsById *sqlx.Stmt `yesql:"total_contacts_by_id"`
yesql.Namespace `yesql:"contact_manager"`
AddFriendMsgsUpdate *sqlx.Stmt `yesql:"add_friend_msgs_update"`
CreateContact *sqlx.Stmt `yesql:"create_contact"`
DelFriend *sqlx.Stmt `yesql:"del_friend"`
FreshContactStatus *sqlx.Stmt `yesql:"fresh_contact_status"`
GetContact *sqlx.Stmt `yesql:"get_contact"`
GetContacts *sqlx.Stmt `yesql:"get_contacts"`
GetUserFriend *sqlx.Stmt `yesql:"get_user_friend"`
IsFriend *sqlx.Stmt `yesql:"is_friend"`
ListFriend *sqlx.Stmt `yesql:"list_friend"`
RejectFriendMsgsUpdate *sqlx.Stmt `yesql:"reject_friend_msgs_update"`
TotalFriendsById *sqlx.Stmt `yesql:"total_friends_by_id"`
CreateMessage *sqlx.NamedStmt `yesql:"create_message"`
}
type FollowIndex struct {
@ -436,25 +446,40 @@ func BuildContactManager(p yesql.PreparexBuilder, ctx ...context.Context) (obj *
c = context.Background()
}
obj = &ContactManager{}
if obj.AddFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_AddFriend))); err != nil {
if obj.AddFriendMsgsUpdate, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_AddFriendMsgsUpdate))); err != nil {
return
}
if obj.CreateContact, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_CreateContact))); err != nil {
return
}
if obj.DelFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_DelFriend))); err != nil {
return
}
if obj.FreshContactStatus, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_FreshContactStatus))); err != nil {
return
}
if obj.GetContact, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_GetContact))); err != nil {
return
}
if obj.GetContacts, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_GetContacts))); err != nil {
return
}
if obj.GetUserFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_GetUserFriend))); err != nil {
return
}
if obj.RejectFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_RejectFriend))); err != nil {
if obj.IsFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_IsFriend))); err != nil {
return
}
if obj.ListFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_ListFriend))); err != nil {
return
}
if obj.RejectFriendMsgsUpdate, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_RejectFriendMsgsUpdate))); err != nil {
return
}
if obj.RequestingFriend, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_RequestingFriend))); err != nil {
if obj.TotalFriendsById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_TotalFriendsById))); err != nil {
return
}
if obj.TotalContactsById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_ContactManager_TotalContactsById))); err != nil {
if obj.CreateMessage, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_ContactManager_CreateMessage))); err != nil {
return
}
return

@ -113,33 +113,53 @@ UPDATE @comment_reply SET thumbs_up_count=?, thumbs_down_count=?, modified_on=?
-- contact_manager sql dml
--------------------------------------------------------------------------------
-- name: requesting_friend@contact_manager
-- name: create_contact@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
INSERT INTO @contact (user_id, friend_id, status, created_on) VALUES (?, ?, ?, ?);
-- name: add_friend@contact_manager
-- name: fresh_contact_status@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @contact SET status=?, modified_on=?, is_del=0 WHERE id=?;
-- name: reject_friend@contact_manager
-- name: create_message@contact_manager
-- prepare: named_stmt
INSERT INTO @message (sender_user_id, receiver_user_id, type, brief, content, reply_id, created_on) VALUES (:sender_user_id, :receiver_user_id, :type, :brief, :content, :reply_id, :created_on);
-- name: add_friend_msgs_update@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @message SET reply_id=?, modified_on=? WHERE ((sender_user_id = ? AND receiver_user_id = ?) OR (sender_user_id = ? AND receiver_user_id = ?)) AND type = ? AND reply_id = ?;
-- name: reject_friend_msgs_update@contact_manager
-- prepare: stmt
UPDATE @message SET reply_id=?, modified_on=? WHERE sender_user_id = ? AND receiver_user_id = ? AND type = ? AND reply_id = ?;
-- name: del_friend@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
UPDATE @contact SET status=4, is_del=1, deleted_on=? WHERE id=?;
-- name: get_contacts@contact_manager
-- name: list_friend@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
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 ?;
-- name: total_contacts_by_id@contact_manager
-- name: total_friends_by_id@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
SELECT count(*) FROM @contact WHERE user_id=? AND status=2 AND is_del=0 LIMIT ? OFFSET ?;
-- name: get_contacts@contact_manager
-- prepare: stmt
SELECT id, user_id, friend_id, group_id, remark, status, is_top, is_black, notice_enable, is_del FROM @contact WHERE (user_id=? AND friend_id=?) OR (user_id=? AND friend_id=?);
-- name: get_user_friend@contact_manager
-- prepare: stmt
SELECT * FROM @user WHERE username=?
SELECT id, user_id, friend_id, group_id, remark, status, is_top, is_black, notice_enable, is_del FROM @contact WHERE user_id=? AND friend_id=? AND is_del=0;
-- name: get_contact@contact_manager
-- prepare: stmt
SELECT id, user_id, friend_id, group_id, remark, status, is_top, is_black, notice_enable, is_del FROM @contact WHERE user_id=? AND friend_id=?;
-- name: is_friend@contact_manager
-- prepare: stmt
SELECT true FROM @contact WHERE user_id=? AND friend_id=? AND is_del=0 AND status=2;
--------------------------------------------------------------------------------
-- message sql dml

Loading…
Cancel
Save