Files
chat_room/internal/repository/question_repository.go
T
mobiniqow 2823fb45f5 Initial commit: dating app backend (Go/Gin/GORM/Postgres)
Phone+OTP auth, staged onboarding, admin approval workflow, and a
full discovery/swipe/match/chat system with photo upload and
approximate distance (haversine) based on user location.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-15 23:23:47 +03:30

83 lines
2.4 KiB
Go

package repository
import (
"gorm.io/gorm"
"dating-app/internal/models"
)
type QuestionRepository struct {
db *gorm.DB
}
func NewQuestionRepository(db *gorm.DB) *QuestionRepository {
return &QuestionRepository{db: db}
}
func (r *QuestionRepository) ListActive() ([]models.ProfileQuestion, error) {
var qs []models.ProfileQuestion
err := r.db.Where("is_active = ?", true).Order("sort_order asc, id asc").Find(&qs).Error
return qs, err
}
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 {
return r.db.Create(q).Error
}
func (r *QuestionRepository) Update(q *models.ProfileQuestion) error {
return r.db.Save(q).Error
}
func (r *QuestionRepository) Delete(id uint) error {
return r.db.Delete(&models.ProfileQuestion{}, id).Error
}
// 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
}