You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ UserModel = (*customUserModel)(nil)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// UserModel is an interface to be customized, add more methods here,
|
|
|
|
// and implement the added methods in customUserModel.
|
|
|
|
UserModel interface {
|
|
|
|
userModel
|
|
|
|
// InsertTx 插入事务
|
|
|
|
InsertTx(ctx context.Context, tx *gorm.DB, data *User) error
|
|
|
|
FindOneTx(ctx context.Context, tx *gorm.DB, id int64) (*User, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
customUserModel struct {
|
|
|
|
*defaultUserModel
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewUserModel returns a model for the database table.
|
|
|
|
func NewUserModel(conn *gorm.DB) UserModel {
|
|
|
|
return &customUserModel{
|
|
|
|
defaultUserModel: newUserModel(conn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customUserModel) InsertTx(ctx context.Context, tx *gorm.DB, data *User) error {
|
|
|
|
return withTx(ctx, m.conn, tx).Create(data).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *customUserModel) FindOneTx(ctx context.Context, tx *gorm.DB, id int64) (*User, error) {
|
|
|
|
db := withTx(ctx, m.conn, tx)
|
|
|
|
var resp User
|
|
|
|
err := db.Model(&User{}).Where("`id` = ?", id).Take(&resp).Error
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
case gormc.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|