feat(verification): add user identity verification system
Implement a complete user identity verification system with support for multiple identity types (student, teacher, staff, alumni). The system includes user-facing verification submission and status checking endpoints, admin endpoints for reviewing verification requests, middleware for protecting verification-required routes, and integration with the user model to track verification status.
This commit is contained in:
106
internal/repository/verification_repo.go
Normal file
106
internal/repository/verification_repo.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"carrot_bbs/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
|
||||
}
|
||||
Reference in New Issue
Block a user