draft
This commit is contained in:
71
backend/internal/repository/app.go
Normal file
71
backend/internal/repository/app.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"pay-bridge/internal/model"
|
||||
)
|
||||
|
||||
// AppRepository app 数据访问
|
||||
type AppRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewAppRepository(db *gorm.DB) *AppRepository {
|
||||
return &AppRepository{db: db}
|
||||
}
|
||||
|
||||
// GetByAppID 根据 appId 查询
|
||||
func (r *AppRepository) GetByAppID(ctx context.Context, appID string) (*model.App, error) {
|
||||
var app model.App
|
||||
err := r.db.WithContext(ctx).Where("app_id = ? AND status = 1", appID).First(&app).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return &app, err
|
||||
}
|
||||
|
||||
// Create 创建应用
|
||||
func (r *AppRepository) Create(ctx context.Context, app *model.App) error {
|
||||
return r.db.WithContext(ctx).Create(app).Error
|
||||
}
|
||||
|
||||
// ListActive 查询所有启用的应用
|
||||
func (r *AppRepository) ListActive(ctx context.Context) ([]*model.App, error) {
|
||||
var apps []*model.App
|
||||
err := r.db.WithContext(ctx).Where("status = 1").Find(&apps).Error
|
||||
return apps, err
|
||||
}
|
||||
|
||||
// List 分页查询所有应用(不过滤状态)
|
||||
func (r *AppRepository) List(ctx context.Context, limit, offset int) ([]*model.App, error) {
|
||||
var apps []*model.App
|
||||
err := r.db.WithContext(ctx).Order("id DESC").Limit(limit).Offset(offset).Find(&apps).Error
|
||||
return apps, err
|
||||
}
|
||||
|
||||
// GetByAppIDUnscoped 不过滤状态地查询(用于管理接口)
|
||||
func (r *AppRepository) GetByAppIDUnscoped(ctx context.Context, appID string) (*model.App, error) {
|
||||
var app model.App
|
||||
err := r.db.WithContext(ctx).Where("app_id = ?", appID).First(&app).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return &app, err
|
||||
}
|
||||
|
||||
// UpdateStatus 更新应用状态
|
||||
func (r *AppRepository) UpdateStatus(ctx context.Context, appID string, status int8) error {
|
||||
return r.db.WithContext(ctx).Model(&model.App{}).
|
||||
Where("app_id = ?", appID).
|
||||
Update("status", status).Error
|
||||
}
|
||||
|
||||
// UpdateSecret 更新应用密钥
|
||||
func (r *AppRepository) UpdateSecret(ctx context.Context, appID, encSecret string) error {
|
||||
return r.db.WithContext(ctx).Model(&model.App{}).
|
||||
Where("app_id = ?", appID).
|
||||
Update("app_secret", encSecret).Error
|
||||
}
|
||||
Reference in New Issue
Block a user