50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
|
package repo
|
||
|
|
||
|
import (
|
||
|
"admin/apps/game/domain/entity"
|
||
|
"admin/apps/game/model"
|
||
|
"admin/internal/errcode"
|
||
|
"errors"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type ICDKeyRepo interface {
|
||
|
GetByID(id int) (*entity.CDKey, bool, error)
|
||
|
AddCount(id int, delta int) (*entity.CDKey, error)
|
||
|
}
|
||
|
|
||
|
func NewCDKeyRepo(db *gorm.DB) ICDKeyRepo {
|
||
|
return &cdKeyRepoImpl{db: db}
|
||
|
}
|
||
|
|
||
|
type cdKeyRepoImpl struct {
|
||
|
db *gorm.DB
|
||
|
}
|
||
|
|
||
|
func (impl *cdKeyRepoImpl) AddCount(id int, delta int) (*entity.CDKey, error) {
|
||
|
po := new(model.CDKey)
|
||
|
err := impl.db.Where("id = ?", id).First(po).Error
|
||
|
if err != nil {
|
||
|
return nil, errcode.New(errcode.ParamsInvalid, "not found cdkey conf:%v", id)
|
||
|
}
|
||
|
et := entity.NewCDKey(po)
|
||
|
et.AddCount(delta)
|
||
|
err = impl.db.Where("id = ?", id).Updates(po).Error
|
||
|
if err != nil {
|
||
|
return nil, errcode.New(errcode.DBError, "update data:%+v error:%v", po, id)
|
||
|
}
|
||
|
return et, nil
|
||
|
}
|
||
|
|
||
|
func (impl *cdKeyRepoImpl) GetByID(id int) (*entity.CDKey, bool, error) {
|
||
|
po := new(model.CDKey)
|
||
|
err := impl.db.Where("id = ?", id).First(po).Error
|
||
|
if err != nil {
|
||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||
|
return nil, false, nil
|
||
|
}
|
||
|
return nil, false, errcode.New(errcode.ParamsInvalid, "not found cdkey conf:%v", id)
|
||
|
}
|
||
|
return entity.NewCDKey(po), true, nil
|
||
|
}
|