40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package repositories
|
|
|
|
import (
|
|
"lost-and-found/internal/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ChatRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewChatRepository(db *gorm.DB) *ChatRepository {
|
|
return &ChatRepository{db: db}
|
|
}
|
|
|
|
func (r *ChatRepository) Create(chat *models.ChatMessage) error {
|
|
return r.db.Create(chat).Error
|
|
}
|
|
|
|
func (r *ChatRepository) FindByUserID(userID uint, limit int) ([]models.ChatMessage, error) {
|
|
var chats []models.ChatMessage
|
|
err := r.db.Where("user_id = ?", userID).
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Find(&chats).Error
|
|
return chats, err
|
|
}
|
|
|
|
func (r *ChatRepository) GetUserChatHistory(userID uint, limit int) ([]models.ChatMessage, error) {
|
|
var chats []models.ChatMessage
|
|
err := r.db.Where("user_id = ?", userID).
|
|
Order("created_at DESC").
|
|
Limit(limit).
|
|
Find(&chats).Error
|
|
return chats, err
|
|
}
|
|
|
|
func (r *ChatRepository) DeleteUserHistory(userID uint) error {
|
|
return r.db.Where("user_id = ?", userID).Delete(&models.ChatMessage{}).Error
|
|
} |