fix: 用户信息更新返回类型与关注建表缺陷
updateMeLogic 更新后重新查询并返回 User 类型,修复 converter 转换 空响应的错误。proto_string 不再丢弃含空格的字符串。userfollows ent schema 补上 created_at 的 Default(time.Now)。
This commit is contained in:
@@ -10,11 +10,13 @@ import (
|
||||
"juwan-backend/common/converter"
|
||||
"juwan-backend/common/utils/contextj"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/users/api/internal/svc"
|
||||
"juwan-backend/app/users/api/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
)
|
||||
|
||||
type UpdateMeLogic struct {
|
||||
@@ -32,12 +34,12 @@ func NewUpdateMeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMe
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserProfileReq) (resp *types.UpdateUserProfileReq, err error) {
|
||||
func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserProfileReq) (resp *types.User, err error) {
|
||||
userId, err := contextj.UserIDFrom(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
|
||||
_, err = l.svcCtx.UserRpc.UpdateUsers(l.ctx, &usercenter.UpdateUsersReq{
|
||||
Id: userId,
|
||||
Nickname: proto_string(req.Nickname),
|
||||
Avatar: proto_string(req.Avatar),
|
||||
@@ -47,16 +49,35 @@ func (l *UpdateMeLogic) UpdateMe(req *types.UpdateUserProfileReq) (resp *types.U
|
||||
if err != nil {
|
||||
return nil, errors.New("update info failed")
|
||||
}
|
||||
err = converter.StructToStruct(res, &resp)
|
||||
user, err := l.svcCtx.UserRpc.GetUsersById(l.ctx, &usercenter.GetUsersByIdReq{
|
||||
Id: userId,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("unmarshal user info failed, err:%v.", err)
|
||||
return nil, errors.New("unmarshal user info failed")
|
||||
logx.Errorf("UpdateMeLogic.GetUsersById err: %v", err)
|
||||
return nil, errors.New("get user failed")
|
||||
}
|
||||
|
||||
resp = &types.User{}
|
||||
err = converter.StructToStruct(user.Users, resp)
|
||||
if err != nil {
|
||||
logx.Errorf("struct to user info failed, err:%v.", err)
|
||||
return nil, errors.New("get user failed")
|
||||
}
|
||||
|
||||
var verificationStatus map[string]string
|
||||
err = json.Unmarshal([]byte(user.Users.VerificationStatus), &verificationStatus)
|
||||
if err != nil {
|
||||
logx.Errorf("json.Unmarshal err: %v", err)
|
||||
}
|
||||
resp.VerifiedRoles = user.Users.VerifiedRoles
|
||||
resp.VerificationStatus = verificationStatus
|
||||
resp.Role = user.Users.CurrentRole
|
||||
resp.CreatedAt = time.Unix(user.Users.CreatedAt, 0).Format(time.DateTime)
|
||||
return
|
||||
}
|
||||
|
||||
func proto_string(s string) *string {
|
||||
if len(s) == 0 || strings.Contains(s, " ") {
|
||||
if strings.TrimSpace(s) == "" {
|
||||
return nil
|
||||
}
|
||||
return &s
|
||||
|
||||
@@ -4,6 +4,7 @@ package models
|
||||
|
||||
import (
|
||||
"juwan-backend/app/users/rpc/internal/models/schema"
|
||||
"juwan-backend/app/users/rpc/internal/models/userfollows"
|
||||
"juwan-backend/app/users/rpc/internal/models/userpreferences"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
"time"
|
||||
@@ -13,6 +14,10 @@ import (
|
||||
// (default values, validators, hooks and policies) and stitches it
|
||||
// to their package variables.
|
||||
func init() {
|
||||
userfollowsFields := schema.UserFollows{}.Fields()
|
||||
_ = userfollowsFields
|
||||
userfollowsDescCreatedAt := userfollowsFields[3].Descriptor()
|
||||
userfollows.DefaultCreatedAt = userfollowsDescCreatedAt.Default.(func() time.Time)
|
||||
userpreferencesFields := schema.UserPreferences{}.Fields()
|
||||
_ = userpreferencesFields
|
||||
// userpreferencesDescNotificationOrder is the schema descriptor for notification_order field.
|
||||
|
||||
@@ -3,6 +3,7 @@ package schema
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/field"
|
||||
"time"
|
||||
)
|
||||
|
||||
// UserFollows holds the schema definition for the UserFollows entity.
|
||||
@@ -16,7 +17,7 @@ func (UserFollows) Fields() []ent.Field {
|
||||
field.Int64("id").Immutable().Unique(),
|
||||
field.Int64("follower_id"),
|
||||
field.Int64("followee_id"),
|
||||
field.Time("created_at").Immutable(),
|
||||
field.Time("created_at").Immutable().Default(time.Now),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
package userfollows
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
)
|
||||
|
||||
@@ -39,6 +41,10 @@ func ValidColumn(column string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var (
|
||||
DefaultCreatedAt func() time.Time
|
||||
)
|
||||
|
||||
// OrderOption defines the ordering options for the UserFollows queries.
|
||||
type OrderOption func(*sql.Selector)
|
||||
|
||||
|
||||
@@ -51,6 +51,7 @@ func (_c *UserFollowsCreate) Mutation() *UserFollowsMutation {
|
||||
|
||||
// Save creates the UserFollows in the database.
|
||||
func (_c *UserFollowsCreate) Save(ctx context.Context) (*UserFollows, error) {
|
||||
_c.defaults()
|
||||
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
|
||||
}
|
||||
|
||||
@@ -76,6 +77,13 @@ func (_c *UserFollowsCreate) ExecX(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (_c *UserFollowsCreate) defaults() {
|
||||
if _, ok := _c.mutation.CreatedAt(); !ok {
|
||||
v := userfollows.DefaultCreatedAt()
|
||||
_c.mutation.SetCreatedAt(v)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (_c *UserFollowsCreate) check() error {
|
||||
if _, ok := _c.mutation.FollowerID(); !ok {
|
||||
@@ -152,6 +160,7 @@ func (_c *UserFollowsCreateBulk) Save(ctx context.Context) ([]*UserFollows, erro
|
||||
for i := range _c.builders {
|
||||
func(i int, root context.Context) {
|
||||
builder := _c.builders[i]
|
||||
builder.defaults()
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*UserFollowsMutation)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user