This commit is contained in:
2026-02-23 23:50:04 +08:00
commit 084d3b0faf
45 changed files with 4090 additions and 0 deletions

View 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
}