lv8girl!
This commit is contained in:
69
internal/repositories/message.go
Normal file
69
internal/repositories/message.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"lv8girl/internal/models"
|
||||
)
|
||||
|
||||
type MessageRepository struct{}
|
||||
|
||||
func NewMessageRepository() *MessageRepository {
|
||||
return &MessageRepository{}
|
||||
}
|
||||
|
||||
func (r *MessageRepository) FindByID(id uint) (*models.PrivateMessage, error) {
|
||||
var msg models.PrivateMessage
|
||||
err := DB.First(&msg, id).Error
|
||||
return &msg, err
|
||||
}
|
||||
|
||||
func (r *MessageRepository) Create(message *models.PrivateMessage) error {
|
||||
return DB.Create(message).Error
|
||||
}
|
||||
|
||||
func (r *MessageRepository) MarkAsRead(id uint) error {
|
||||
return DB.Model(&models.PrivateMessage{}).Where("id = ?", id).Update("is_read", true).Error
|
||||
}
|
||||
|
||||
func (r *MessageRepository) CountUnread(userID uint) (int64, error) {
|
||||
var count int64
|
||||
err := DB.Model(&models.PrivateMessage{}).
|
||||
Where("to_user_id = ? AND is_read = ?", userID, false).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (r *MessageRepository) FindConversations(userID uint) ([]models.PrivateMessage, error) {
|
||||
var messages []models.PrivateMessage
|
||||
err := DB.Where("from_user_id = ? OR to_user_id = ?", userID, userID).
|
||||
Order("created_at DESC").
|
||||
Find(&messages).Error
|
||||
return messages, err
|
||||
}
|
||||
|
||||
func (r *MessageRepository) FindLastMessage(userID, otherUserID uint) (*models.PrivateMessage, error) {
|
||||
var msg models.PrivateMessage
|
||||
err := DB.Where(
|
||||
"(from_user_id = ? AND to_user_id = ?) OR (from_user_id = ? AND to_user_id = ?)",
|
||||
userID, otherUserID, otherUserID, userID,
|
||||
).Order("created_at DESC").First(&msg).Error
|
||||
return &msg, err
|
||||
}
|
||||
|
||||
func (r *MessageRepository) CountUnreadFromUser(fromUserID, toUserID uint) (int64, error) {
|
||||
var count int64
|
||||
err := DB.Model(&models.PrivateMessage{}).
|
||||
Where("from_user_id = ? AND to_user_id = ? AND is_read = ?", fromUserID, toUserID, false).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
type ConversationSummary struct {
|
||||
UserID uint
|
||||
Username string
|
||||
Avatar string
|
||||
LastMsg string
|
||||
Time time.Time
|
||||
Unread int64
|
||||
}
|
||||
Reference in New Issue
Block a user