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.

50 lines
1.2 KiB
Go

3 years ago
package model
3 years ago
import (
"context"
"git.noahlan.cn/northlan/ntools-go/gorm-zero/gormc"
3 years ago
"gorm.io/gorm"
)
3 years ago
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)
3 years ago
}
customUserModel struct {
*defaultUserModel
}
)
// NewUserModel returns a model for the database table.
3 years ago
func NewUserModel(conn *gorm.DB) UserModel {
3 years ago
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
}
}