sqlx: fixed get messages error

r/paopao-ce-plus
Michael Li 1 year ago
parent d1ad76ca6b
commit 03d374a123
No known key found for this signature in database

@ -63,8 +63,8 @@ const (
_FollowingManager_ListFollows = `SELECT u.id user_id, u.username username, u.nickname nickname, u.avatar avatar, u.created_on created_on 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 ?` _FollowingManager_ListFollows = `SELECT u.id user_id, u.username username, u.nickname nickname, u.avatar avatar, u.created_on created_on 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_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_GetMessageById = `SELECT * FROM @message WHERE id=? AND is_del=0`
_Message_GetMessageCount = `SELECT count(*) FROM @message WHERE receiver_user_id=:receiver_user_id AND is_del=0` _Message_GetMessageCount = `SELECT count(*) FROM @message WHERE (receiver_user_id=? OR (sender_user_id=? AND type=4)) AND is_del=0`
_Message_GetMessages = `SELECT * FROM @message WHERE receiver_user_id=:receiver_user_id AND is_del=0 ORDER BY id DESC LIMIT :limit OFFSET :offset` _Message_GetMessages = `SELECT * FROM @message WHERE (receiver_user_id=? OR (sender_user_id=? AND type=4)) AND is_del=0 ORDER BY id DESC LIMIT ? OFFSET ?`
_Message_GetUnreadCount = `SELECT count(*) FROM @message WHERE receiver_user_id=? AND is_read=0 AND is_del=0` _Message_GetUnreadCount = `SELECT count(*) FROM @message WHERE receiver_user_id=? AND is_read=0 AND is_del=0`
_Message_ReadMessage = `UPDATE @message SET is_read=1, modified_on=? WHERE id=?` _Message_ReadMessage = `UPDATE @message SET is_read=1, modified_on=? WHERE id=?`
_Security_CreatePhoneCaptcha = `INSERT INTO @captcha (phone, captcha, expired_on, created_on) VALUES (:phone, :captcha, :expired_on, :created_on)` _Security_CreatePhoneCaptcha = `INSERT INTO @captcha (phone, captcha, expired_on, created_on) VALUES (:phone, :captcha, :expired_on, :created_on)`
@ -243,11 +243,11 @@ type FollowingManager struct {
type Message struct { type Message struct {
yesql.Namespace `yesql:"message"` yesql.Namespace `yesql:"message"`
GetMessageById *sqlx.Stmt `yesql:"get_message_by_id"` GetMessageById *sqlx.Stmt `yesql:"get_message_by_id"`
GetMessageCount *sqlx.Stmt `yesql:"get_message_count"`
GetMessages *sqlx.Stmt `yesql:"get_messages"`
GetUnreadCount *sqlx.Stmt `yesql:"get_unread_count"` GetUnreadCount *sqlx.Stmt `yesql:"get_unread_count"`
ReadMessage *sqlx.Stmt `yesql:"read_message"` ReadMessage *sqlx.Stmt `yesql:"read_message"`
CreateMessage *sqlx.NamedStmt `yesql:"create_message"` CreateMessage *sqlx.NamedStmt `yesql:"create_message"`
GetMessageCount *sqlx.NamedStmt `yesql:"get_message_count"`
GetMessages *sqlx.NamedStmt `yesql:"get_messages"`
} }
type Security struct { type Security struct {
@ -562,6 +562,12 @@ func BuildMessage(p PreparexBuilder, ctx ...context.Context) (obj *Message, err
if obj.GetMessageById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Message_GetMessageById))); err != nil { if obj.GetMessageById, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Message_GetMessageById))); err != nil {
return nil, fmt.Errorf("prepare _Message_GetMessageById error: %w", err) return nil, fmt.Errorf("prepare _Message_GetMessageById error: %w", err)
} }
if obj.GetMessageCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Message_GetMessageCount))); err != nil {
return nil, fmt.Errorf("prepare _Message_GetMessageCount error: %w", err)
}
if obj.GetMessages, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Message_GetMessages))); err != nil {
return nil, fmt.Errorf("prepare _Message_GetMessages error: %w", err)
}
if obj.GetUnreadCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Message_GetUnreadCount))); err != nil { if obj.GetUnreadCount, err = p.PreparexContext(c, p.Rebind(p.QueryHook(_Message_GetUnreadCount))); err != nil {
return nil, fmt.Errorf("prepare _Message_GetUnreadCount error: %w", err) return nil, fmt.Errorf("prepare _Message_GetUnreadCount error: %w", err)
} }
@ -571,12 +577,6 @@ func BuildMessage(p PreparexBuilder, ctx ...context.Context) (obj *Message, err
if obj.CreateMessage, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_Message_CreateMessage))); err != nil { if obj.CreateMessage, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_Message_CreateMessage))); err != nil {
return nil, fmt.Errorf("prepare _Message_CreateMessage error: %w", err) return nil, fmt.Errorf("prepare _Message_CreateMessage error: %w", err)
} }
if obj.GetMessageCount, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_Message_GetMessageCount))); err != nil {
return nil, fmt.Errorf("prepare _Message_GetMessageCount error: %w", err)
}
if obj.GetMessages, err = p.PrepareNamedContext(c, p.Rebind(p.QueryHook(_Message_GetMessages))); err != nil {
return nil, fmt.Errorf("prepare _Message_GetMessages error: %w", err)
}
return return
} }

@ -55,10 +55,9 @@ func (s *messageSrv) ReadMessage(r *ms.Message) (err error) {
return return
} }
func (s *messageSrv) GetMessages(r *ms.ConditionsT, offset, limit int) ([]*ms.MessageFormated, error) { func (s *messageSrv) GetMessages(userId int64, offset, limit int) ([]*ms.MessageFormated, error) {
var messages []*ms.Message var messages []*ms.Message
(*r)["limit"], (*r)["offset"] = limit, offset if err := s.q.GetMessages.Select(&messages, userId, userId, limit, offset); err != nil {
if err := s.q.GetMessages.Select(&messages, *r); err != nil {
return nil, err return nil, err
} }
mfs := make([]*ms.MessageFormated, 0, len(messages)) mfs := make([]*ms.MessageFormated, 0, len(messages))
@ -69,8 +68,8 @@ func (s *messageSrv) GetMessages(r *ms.ConditionsT, offset, limit int) ([]*ms.Me
return mfs, nil return mfs, nil
} }
func (s *messageSrv) GetMessageCount(r *ms.ConditionsT) (res int64, err error) { func (s *messageSrv) GetMessageCount(userId int64) (res int64, err error) {
if err = s.q.GetMessageCount.Get(&res, *r); err != nil { if err = s.q.GetMessageCount.Get(&res, userId, userId); err != nil {
logrus.Errorf("get message count error: %s", err) logrus.Errorf("get message count error: %s", err)
} }
return return

@ -285,16 +285,16 @@ SELECT * FROM @message WHERE id=? AND is_del=0;
UPDATE @message SET is_read=1, modified_on=? WHERE id=?; UPDATE @message SET is_read=1, modified_on=? WHERE id=?;
-- name: get_messages@message -- name: get_messages@message
-- prepare: named_stmt -- prepare: stmt
SELECT * SELECT *
FROM @message FROM @message
WHERE receiver_user_id=:receiver_user_id AND is_del=0 WHERE (receiver_user_id=? OR (sender_user_id=? AND type=4)) AND is_del=0
ORDER BY id DESC ORDER BY id DESC
LIMIT :limit OFFSET :offset LIMIT ? OFFSET ?
-- name: get_message_count@message -- name: get_message_count@message
-- prepare: named_stmt -- prepare: stmt
SELECT count(*) FROM @message WHERE receiver_user_id=:receiver_user_id AND is_del=0; SELECT count(*) FROM @message WHERE (receiver_user_id=? OR (sender_user_id=? AND type=4)) AND is_del=0
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- security sql dml -- security sql dml

Loading…
Cancel
Save