64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
|
|
"management/internal/erpserver/model/system"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UserStore 定义了 user 模块在 store 层所实现的方法.
|
|
type UserStore interface {
|
|
Create(ctx context.Context, obj *system.User) error
|
|
Update(ctx context.Context, obj *system.User) error
|
|
Get(ctx context.Context, id int32) (*system.User, error)
|
|
GetByEmail(ctx context.Context, email string) (*system.User, error)
|
|
|
|
UserExpansion
|
|
}
|
|
|
|
// UserExpansion 定义了用户操作的附加方法.
|
|
type UserExpansion interface{}
|
|
|
|
// userStore 是 UserStore 接口的实现.
|
|
type userStore struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// 确保 userStore 实现了 UserStore 接口.
|
|
var _ UserStore = (*userStore)(nil)
|
|
|
|
// NewUserStore 创建 userStore 的实例.
|
|
func NewUserStore(db *gorm.DB) *userStore {
|
|
return &userStore{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (s *userStore) Create(ctx context.Context, obj *system.User) error {
|
|
return s.db.WithContext(ctx).Create(obj).Error
|
|
}
|
|
|
|
func (s *userStore) Update(ctx context.Context, obj *system.User) error {
|
|
return s.db.WithContext(ctx).Save(obj).Error
|
|
}
|
|
|
|
func (s *userStore) Get(ctx context.Context, id int32) (*system.User, error) {
|
|
var user system.User
|
|
err := s.db.WithContext(ctx).Where("id = ?", id).First(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (s *userStore) GetByEmail(ctx context.Context, email string) (*system.User, error) {
|
|
var user system.User
|
|
err := s.db.WithContext(ctx).Where("email = ?", email).First(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|