补充事务说明

This commit is contained in:
yumaojun03 2024-12-08 10:10:37 +08:00
parent 6df56cbe96
commit 33699fad93
5 changed files with 86 additions and 6 deletions

View File

@ -84,3 +84,5 @@ WORKSPACE_DIR="/Users/xxxx/Projects/go-course/go17/vblog"
### 业务模块的实现 ### 业务模块的实现
TDD (Test Drive Development) TDD (Test Drive Development)
+ 用户模块

View File

@ -19,8 +19,24 @@ func (u *UserServiceImpl) DescribeUser(context.Context, *user.DescribeUserReques
} }
// Registry implements user.Service. // Registry implements user.Service.
func (u *UserServiceImpl) Registry(context.Context, *user.RegistryRequest) (*user.User, error) { func (u *UserServiceImpl) Registry(ctx context.Context, in *user.RegistryRequest) (*user.User, error) {
panic("unimplemented") 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. // ResetPassword implements user.Service.
@ -29,7 +45,10 @@ func (u *UserServiceImpl) ResetPassword(context.Context, *user.ResetPasswordRequ
} }
// UnRegistry implements user.Service. // 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") panic("unimplemented")
} }

View File

@ -3,9 +3,21 @@ package user
import ( import (
"time" "time"
"github.com/infraboard/mcube/v2/exception"
"github.com/infraboard/mcube/v2/ioc/config/validator"
"gitlab.com/go-course-project/go17/vblog/utils" "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 { type User struct {
// 存放到数据里的对象的远数据信息 // 存放到数据里的对象的远数据信息
utils.ResourceMeta utils.ResourceMeta
@ -15,15 +27,19 @@ type User struct {
type RegistryRequest 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 信息
Profile Profile
// 用户状态 // 用户状态
Status Status
} }
func (r *RegistryRequest) Validate() error {
return validator.Validate(r)
}
type Profile struct { type Profile struct {
// 头像 // 头像
Avatar string `json:"avatar" gorm:"column:avatar;type:varchar(255)"` Avatar string `json:"avatar" gorm:"column:avatar;type:varchar(255)"`

View File

@ -0,0 +1,37 @@
<mxfile host="65bd71144e">
<diagram id="u9_ob_htgBlG7QGFx-zW" name="第 1 页">
<mxGraphModel dx="1147" dy="647" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" value="&amp;nbsp;事务" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="120" y="150" width="430" height="60" as="geometry"/>
</mxCell>
<mxCell id="3" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="120" y="280" width="120" height="240" as="geometry"/>
</mxCell>
<mxCell id="11" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="4" target="3">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="4" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="270" y="280" width="120" height="240" as="geometry"/>
</mxCell>
<mxCell id="10" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="5" target="4">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="5" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="430" y="280" width="120" height="240" as="geometry"/>
</mxCell>
<mxCell id="6" value="A" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="120" y="250" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="8" value="B" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;" vertex="1" parent="1">
<mxGeometry x="270" y="250" width="30" height="30" as="geometry"/>
</mxCell>
<mxCell id="9" value="C" style="text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;" vertex="1" parent="1">
<mxGeometry x="430" y="250" width="30" height="30" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

View File

@ -2,6 +2,12 @@ package utils
import "time" import "time"
func NewResourceMeta() *ResourceMeta {
return &ResourceMeta{
CreatedAt: time.Now(),
}
}
type ResourceMeta struct { type ResourceMeta struct {
// 资源Id // 资源Id
Id uint `json:"id" gorm:"primaryKey;column:id"` Id uint `json:"id" gorm:"primaryKey;column:id"`