Files
chat_room/internal/repository/question_repository.go
T

112 lines
3.0 KiB
Go
Raw Normal View History

package repository
import (
"time"
"gorm.io/gorm"
"dating-app/internal/cache"
"dating-app/internal/models"
)
const activeQuestionsCacheTTL = 5 * time.Minute
const activeQuestionsCacheKey = "active"
type QuestionRepository struct {
db *gorm.DB
activeCache *cache.TTLCache[string, []models.ProfileQuestion]
}
func NewQuestionRepository(db *gorm.DB) *QuestionRepository {
return &QuestionRepository{
db: db,
activeCache: cache.New[string, []models.ProfileQuestion](activeQuestionsCacheTTL),
}
}
func (r *QuestionRepository) ListActive() ([]models.ProfileQuestion, error) {
if cached, ok := r.activeCache.Get(activeQuestionsCacheKey); ok {
return cached, nil
}
var qs []models.ProfileQuestion
err := r.db.Where("is_active = ?", true).Order("sort_order asc, id asc").Find(&qs).Error
if err != nil {
return nil, err
}
r.activeCache.Set(activeQuestionsCacheKey, qs)
return qs, nil
}
func (r *QuestionRepository) ListAllAdmin() ([]models.ProfileQuestion, error) {
var qs []models.ProfileQuestion
err := r.db.Order("sort_order asc, id asc").Find(&qs).Error
return qs, err
}
func (r *QuestionRepository) FindByID(id uint) (*models.ProfileQuestion, error) {
var q models.ProfileQuestion
err := r.db.First(&q, id).Error
if err != nil {
return nil, err
}
return &q, nil
}
func (r *QuestionRepository) FindActiveByIDs(ids []uint) ([]models.ProfileQuestion, error) {
var qs []models.ProfileQuestion
err := r.db.Where("is_active = ? AND id IN ?", true, ids).Find(&qs).Error
return qs, err
}
func (r *QuestionRepository) Create(q *models.ProfileQuestion) error {
if err := r.db.Create(q).Error; err != nil {
return err
}
r.activeCache.Clear()
return nil
}
func (r *QuestionRepository) Update(q *models.ProfileQuestion) error {
if err := r.db.Save(q).Error; err != nil {
return err
}
r.activeCache.Clear()
return nil
}
func (r *QuestionRepository) Delete(id uint) error {
if err := r.db.Delete(&models.ProfileQuestion{}, id).Error; err != nil {
return err
}
r.activeCache.Clear()
return nil
}
// ReplaceUserAnswers پاسخ‌های قبلی کاربر رو با پاسخ‌های جدید جایگزین می‌کنه (تراکنشی)
func (r *QuestionRepository) ReplaceUserAnswers(userID uint, answers []models.UserProfileAnswer) error {
return r.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Where("user_id = ?", userID).Delete(&models.UserProfileAnswer{}).Error; err != nil {
return err
}
for i := range answers {
answers[i].UserID = userID
if err := tx.Create(&answers[i]).Error; err != nil {
return err
}
}
return nil
})
}
func (r *QuestionRepository) GetUserAnswers(userID uint) ([]models.UserProfileAnswer, error) {
var answers []models.UserProfileAnswer
err := r.db.Preload("Question").Where("user_id = ?", userID).Find(&answers).Error
return answers, err
}
func (r *QuestionRepository) CountUserAnswers(userID uint) (int64, error) {
var count int64
err := r.db.Model(&models.UserProfileAnswer{}).Where("user_id = ?", userID).Count(&count).Error
return count, err
}