The Go module name has been changed from `carrot_bbs` to `with_you`, which requires all import paths to be updated throughout the codebase and by external consumers. BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports and dependencies must be updated from carrot_bbs/* to with_you/*.
107 lines
3.0 KiB
Go
107 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"with_you/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type VerificationRepository interface {
|
|
Create(record *model.VerificationRecord) error
|
|
GetByID(id string) (*model.VerificationRecord, error)
|
|
GetLatestByUserID(userID string) (*model.VerificationRecord, error)
|
|
GetPendingByUserID(userID string) (*model.VerificationRecord, error)
|
|
List(page, pageSize int, status, keyword string) ([]*model.VerificationRecord, int64, error)
|
|
Update(record *model.VerificationRecord) error
|
|
GetRecordsByUserID(userID string, page, pageSize int) ([]*model.VerificationRecord, int64, error)
|
|
}
|
|
|
|
type verificationRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewVerificationRepository(db *gorm.DB) VerificationRepository {
|
|
return &verificationRepository{db: db}
|
|
}
|
|
|
|
func (r *verificationRepository) Create(record *model.VerificationRecord) error {
|
|
return r.db.Create(record).Error
|
|
}
|
|
|
|
func (r *verificationRepository) GetByID(id string) (*model.VerificationRecord, error) {
|
|
var record model.VerificationRecord
|
|
err := r.db.First(&record, "id = ?", id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &record, nil
|
|
}
|
|
|
|
func (r *verificationRepository) GetLatestByUserID(userID string) (*model.VerificationRecord, error) {
|
|
var record model.VerificationRecord
|
|
err := r.db.Where("user_id = ?", userID).
|
|
Order("created_at DESC").
|
|
First(&record).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &record, nil
|
|
}
|
|
|
|
func (r *verificationRepository) GetPendingByUserID(userID string) (*model.VerificationRecord, error) {
|
|
var record model.VerificationRecord
|
|
err := r.db.Where("user_id = ? AND status = ?", userID, model.VerificationStatusPending).
|
|
First(&record).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &record, nil
|
|
}
|
|
|
|
func (r *verificationRepository) List(page, pageSize int, status, keyword string) ([]*model.VerificationRecord, int64, error) {
|
|
var records []*model.VerificationRecord
|
|
var total int64
|
|
|
|
query := r.db.Model(&model.VerificationRecord{})
|
|
|
|
if status != "" {
|
|
query = query.Where("verification_records.status = ?", status)
|
|
}
|
|
|
|
if keyword != "" {
|
|
query = query.Joins("JOIN users ON users.id = verification_records.user_id").
|
|
Where("users.username LIKE ? OR users.nickname LIKE ? OR verification_records.real_name LIKE ?",
|
|
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
|
}
|
|
|
|
query.Count(&total)
|
|
|
|
offset := (page - 1) * pageSize
|
|
err := query.Order("created_at DESC").
|
|
Offset(offset).
|
|
Limit(pageSize).
|
|
Find(&records).Error
|
|
|
|
return records, total, err
|
|
}
|
|
|
|
func (r *verificationRepository) Update(record *model.VerificationRecord) error {
|
|
return r.db.Save(record).Error
|
|
}
|
|
|
|
func (r *verificationRepository) GetRecordsByUserID(userID string, page, pageSize int) ([]*model.VerificationRecord, int64, error) {
|
|
var records []*model.VerificationRecord
|
|
var total int64
|
|
|
|
query := r.db.Model(&model.VerificationRecord{}).Where("user_id = ?", userID)
|
|
query.Count(&total)
|
|
|
|
offset := (page - 1) * pageSize
|
|
err := query.Order("created_at DESC").
|
|
Offset(offset).
|
|
Limit(pageSize).
|
|
Find(&records).Error
|
|
|
|
return records, total, err
|
|
}
|