diff --git a/vblog/README.md b/vblog/README.md index b1fba96..fc8bc35 100644 --- a/vblog/README.md +++ b/vblog/README.md @@ -83,4 +83,6 @@ WORKSPACE_DIR="/Users/xxxx/Projects/go-course/go17/vblog" ### 业务模块的实现 -TDD (Test Drive Development) \ No newline at end of file +TDD (Test Drive Development) + ++ 用户模块 \ No newline at end of file diff --git a/vblog/apps/user/impl/impl.go b/vblog/apps/user/impl/impl.go index 52d3fe2..fd58948 100644 --- a/vblog/apps/user/impl/impl.go +++ b/vblog/apps/user/impl/impl.go @@ -19,8 +19,24 @@ func (u *UserServiceImpl) DescribeUser(context.Context, *user.DescribeUserReques } // Registry implements user.Service. -func (u *UserServiceImpl) Registry(context.Context, *user.RegistryRequest) (*user.User, error) { - panic("unimplemented") +func (u *UserServiceImpl) Registry(ctx context.Context, in *user.RegistryRequest) (*user.User, error) { + ins, err := user.New(in) + if err != nil { + return nil, err + } + + // 无事务的模式 + // datasource.DB().Transaction(func(tx *gorm.DB) error { + // ctx := datasource.WithTransactionCtx(ctx) + // // 1. + // svcA.Call(ctx) + // // 2. + // svcB.Call(ctx) + // // 3. + // svcC.Call(ctx) + // }) + + return ins, nil } // ResetPassword implements user.Service. @@ -29,7 +45,10 @@ func (u *UserServiceImpl) ResetPassword(context.Context, *user.ResetPasswordRequ } // UnRegistry implements user.Service. -func (u *UserServiceImpl) UnRegistry(context.Context, *user.UnRegistryRequest) { +func (u *UserServiceImpl) UnRegistry(ctx context.Context, in *user.UnRegistryRequest) { + // datasource.GetTransactionFromCtx(ctx) + // datasource.DB() + // datasource.DBFromCtx() panic("unimplemented") } diff --git a/vblog/apps/user/model.go b/vblog/apps/user/model.go index 4197f12..cb6e53f 100644 --- a/vblog/apps/user/model.go +++ b/vblog/apps/user/model.go @@ -3,9 +3,21 @@ package user import ( "time" + "github.com/infraboard/mcube/v2/exception" + "github.com/infraboard/mcube/v2/ioc/config/validator" "gitlab.com/go-course-project/go17/vblog/utils" ) +func New(in *RegistryRequest) (*User, error) { + if err := in.Validate(); err != nil { + return nil, exception.NewBadRequest("参数校验失败: %s", err) + } + return &User{ + ResourceMeta: *utils.NewResourceMeta(), + RegistryRequest: *in, + }, nil +} + type User struct { // 存放到数据里的对象的远数据信息 utils.ResourceMeta @@ -15,15 +27,19 @@ type User struct { type RegistryRequest struct { // 用户名 - Username string `json:"username" gorm:"column:username;unique;index"` + Username string `json:"username" gorm:"column:username;unique;index" validate:"required"` // 密码 - Password string `json:"password" gorm:"column:username;type:varchar(255)"` + Password string `json:"password" gorm:"column:username;type:varchar(255)" validate:"required"` // Profile 信息 Profile // 用户状态 Status } +func (r *RegistryRequest) Validate() error { + return validator.Validate(r) +} + type Profile struct { // 头像 Avatar string `json:"avatar" gorm:"column:avatar;type:varchar(255)"` diff --git a/vblog/docs/transation.drawio b/vblog/docs/transation.drawio new file mode 100644 index 0000000..d5a4a23 --- /dev/null +++ b/vblog/docs/transation.drawio @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/vblog/utils/resource.go b/vblog/utils/resource.go index 23bc14a..e74209c 100644 --- a/vblog/utils/resource.go +++ b/vblog/utils/resource.go @@ -2,6 +2,12 @@ package utils import "time" +func NewResourceMeta() *ResourceMeta { + return &ResourceMeta{ + CreatedAt: time.Now(), + } +} + type ResourceMeta struct { // 资源Id Id uint `json:"id" gorm:"primaryKey;column:id"`