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>
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"dating-app/internal/models"
|
||||
)
|
||||
|
||||
type MatchRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewMatchRepository(db *gorm.DB) *MatchRepository {
|
||||
return &MatchRepository{db: db}
|
||||
}
|
||||
|
||||
// normalizePair همیشه شناسهی کوچیکتر رو اول میذاره تا هر جفت فقط یک رکورد داشته باشه
|
||||
func normalizePair(a, b uint) (uint, uint) {
|
||||
if a < b {
|
||||
return a, b
|
||||
}
|
||||
return b, a
|
||||
}
|
||||
|
||||
// FindOrCreate اگه مچ بین این دو کاربر از قبل وجود داشته باشه همونو برمیگردونه،
|
||||
// وگرنه میسازتش. خروجی دوم true یعنی همین الان تازه ساخته شده.
|
||||
func (r *MatchRepository) FindOrCreate(userA, userB uint) (*models.Match, bool, error) {
|
||||
one, two := normalizePair(userA, userB)
|
||||
|
||||
var match models.Match
|
||||
err := r.db.Where("user_one_id = ? AND user_two_id = ?", one, two).First(&match).Error
|
||||
if err == nil {
|
||||
return &match, false, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
match = models.Match{UserOneID: one, UserTwoID: two}
|
||||
if err := r.db.Create(&match).Error; err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return &match, true, nil
|
||||
}
|
||||
|
||||
func (r *MatchRepository) FindByID(id uint) (*models.Match, error) {
|
||||
var match models.Match
|
||||
if err := r.db.First(&match, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &match, nil
|
||||
}
|
||||
|
||||
// ListByUser مچهای کاربر رو از جدیدترین به قدیمیترین برمیگردونه
|
||||
func (r *MatchRepository) ListByUser(userID uint) ([]models.Match, error) {
|
||||
var matches []models.Match
|
||||
err := r.db.
|
||||
Where("user_one_id = ? OR user_two_id = ?", userID, userID).
|
||||
Order("created_at desc").
|
||||
Find(&matches).Error
|
||||
return matches, err
|
||||
}
|
||||
|
||||
// OtherUserID شناسهی طرف مقابل کاربر جاری رو توی یک مچ برمیگردونه
|
||||
func OtherUserID(match *models.Match, currentUserID uint) uint {
|
||||
if match.UserOneID == currentUserID {
|
||||
return match.UserTwoID
|
||||
}
|
||||
return match.UserOneID
|
||||
}
|
||||
|
||||
// IsMember یعنی این کاربر عضو این مچه
|
||||
func IsMember(match *models.Match, userID uint) bool {
|
||||
return match.UserOneID == userID || match.UserTwoID == userID
|
||||
}
|
||||
Reference in New Issue
Block a user