2024-12-01 18:04:02 +08:00
|
|
|
package impl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-12-08 11:04:25 +08:00
|
|
|
"github.com/infraboard/mcube/v2/ioc/config/datasource"
|
2024-12-01 18:04:02 +08:00
|
|
|
"gitlab.com/go-course-project/go17/vblog/apps/user"
|
2024-12-08 11:04:25 +08:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2024-12-01 18:04:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var UserService user.Service = &UserServiceImpl{}
|
|
|
|
|
|
|
|
// 定义一个struct, 用于实现 UserService就是刚才定义的接口
|
|
|
|
// 怎么才能判断这个结构体没有实现这个接口
|
|
|
|
type UserServiceImpl struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// DescribeUser implements user.Service.
|
2024-12-08 11:04:25 +08:00
|
|
|
func (u *UserServiceImpl) DescribeUser(ctx context.Context, in *user.DescribeUserRequest) (*user.User, error) {
|
|
|
|
query := datasource.DBFromCtx(ctx)
|
|
|
|
switch in.DescribeBy {
|
|
|
|
case user.DESCRIBE_BY_ID:
|
|
|
|
query = query.Where("id = ?", in.Value)
|
|
|
|
case user.DESCRIBE_BY_USERNAME:
|
|
|
|
query = query.Where("username = ?", in.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
ins := &user.User{}
|
|
|
|
if err := query.Take(ins).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ins, nil
|
2024-12-01 18:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registry implements user.Service.
|
2024-12-08 10:10:37 +08:00
|
|
|
func (u *UserServiceImpl) Registry(ctx context.Context, in *user.RegistryRequest) (*user.User, error) {
|
|
|
|
ins, err := user.New(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-08 11:04:25 +08:00
|
|
|
// 明文密码保持到数据库,是不安全
|
|
|
|
// 对称加密/非对称, 解密
|
|
|
|
// 消息摘要, 无法还原
|
|
|
|
// 怎么知道用户的密码 比对hash 123 -> (xxx)
|
|
|
|
// md5 sha1/256/512, hmac, ...
|
|
|
|
// 结果固定
|
|
|
|
hashPass, err := bcrypt.GenerateFromPassword([]byte(in.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ins.Password = string(hashPass)
|
|
|
|
|
|
|
|
if err := datasource.DBFromCtx(ctx).Create(ins).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// context.WithValue()
|
|
|
|
|
2024-12-08 10:10:37 +08:00
|
|
|
// 无事务的模式
|
|
|
|
// 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
|
2024-12-01 18:04:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResetPassword implements user.Service.
|
|
|
|
func (u *UserServiceImpl) ResetPassword(context.Context, *user.ResetPasswordRequest) error {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnRegistry implements user.Service.
|
2024-12-08 10:10:37 +08:00
|
|
|
func (u *UserServiceImpl) UnRegistry(ctx context.Context, in *user.UnRegistryRequest) {
|
|
|
|
// datasource.GetTransactionFromCtx(ctx)
|
|
|
|
// datasource.DB()
|
|
|
|
// datasource.DBFromCtx()
|
2024-12-01 18:04:02 +08:00
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePassword implements user.Service.
|
|
|
|
func (u *UserServiceImpl) UpdatePassword(context.Context, *user.UpdatePasswordRequest) error {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateProfile implements user.Service.
|
|
|
|
func (u *UserServiceImpl) UpdateProfile(context.Context, *user.UpdateProfileRequest) (*user.User, error) {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUserStatus implements user.Service.
|
|
|
|
func (u *UserServiceImpl) UpdateUserStatus(context.Context, *user.UpdateUserStatusRequest) (*user.User, error) {
|
|
|
|
panic("unimplemented")
|
|
|
|
}
|