Skip to content

Commit

Permalink
feat:更新cloudmind-content
Browse files Browse the repository at this point in the history
  • Loading branch information
Love-YeLin committed Jan 15, 2024
2 parents c418298 + 1614d94 commit 360744f
Show file tree
Hide file tree
Showing 18 changed files with 453 additions and 151 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/.idea
/data
/output
etc
<<<<<<< HEAD
etc
=======
>>>>>>> 1614d94c8a1344a30c74ad9847dc271f8381a8a4
25 changes: 25 additions & 0 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@ type ContentServerImpl struct {
FileService service.IFileService
PostService service.IPostService
LabelService service.ILabelService
UserService service.UserService
}

func (s *ContentServerImpl) DeleteUser(ctx context.Context, req *content.DeleteUserReq) (resp *content.DeleteUserResp, err error) {
return s.UserService.DeleteUser(ctx, req)
}

func (s *ContentServerImpl) UpdateUser(ctx context.Context, req *content.UpdateUserReq) (resp *content.UpdateUserResp, err error) {
return s.UserService.UpdateUser(ctx, req)
}

func (s *ContentServerImpl) GetUser(ctx context.Context, req *content.GetUserReq) (resp *content.GetUserResp, err error) {
return s.UserService.GetUser(ctx, req)
}

func (s *ContentServerImpl) SearchUser(ctx context.Context, req *content.SearchUserReq) (resp *content.SearchUserResp, err error) {
return s.UserService.SearchUser(ctx, req)
}

func (s *ContentServerImpl) CreateUser(ctx context.Context, req *content.CreateUserReq) (resp *content.CreateUserResp, err error) {
return s.UserService.CreateUser(ctx, req)
}

func (s *ContentServerImpl) GetUserDetail(ctx context.Context, req *content.GetUserDetailReq) (resp *content.GetUserDetailResp, err error) {
return s.UserService.GetUserDetail(ctx, req)
}

func (s *ContentServerImpl) GetFolderSize(ctx context.Context, req *content.GetFolderSizeReq) (*content.GetFolderSizeResp, error) {
Expand Down
122 changes: 122 additions & 0 deletions biz/application/service/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package service

import (
"context"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/convertor"
usermapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/user"
"github.com/CloudStriver/go-pkg/utils/pagination/esp"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
"github.com/CloudStriver/go-pkg/utils/util/log"
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content"
"github.com/google/wire"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type UserService interface {
GetUser(ctx context.Context, req *gencontent.GetUserReq) (resp *gencontent.GetUserResp, err error)
CreateUser(ctx context.Context, req *gencontent.CreateUserReq) (resp *gencontent.CreateUserResp, err error)
UpdateUser(ctx context.Context, req *gencontent.UpdateUserReq) (resp *gencontent.UpdateUserResp, err error)
GetUserDetail(ctx context.Context, req *gencontent.GetUserDetailReq) (resp *gencontent.GetUserDetailResp, err error)
SearchUser(ctx context.Context, req *gencontent.SearchUserReq) (resp *gencontent.SearchUserResp, err error)
DeleteUser(ctx context.Context, req *gencontent.DeleteUserReq) (resp *gencontent.DeleteUserResp, err error)
}

type UserServiceImpl struct {
Config *config.Config
UserMongoMapper usermapper.UserMongoMapper
UserEsMapper usermapper.UserEsMapper
Redis *redis.Redis
}

var UserSet = wire.NewSet(
wire.Struct(new(UserServiceImpl), "*"),
wire.Bind(new(UserService), new(*UserServiceImpl)),
)

func (s *UserServiceImpl) DeleteUser(ctx context.Context, req *gencontent.DeleteUserReq) (resp *gencontent.DeleteUserResp, err error) {
resp = new(gencontent.DeleteUserResp)
_, err = s.UserMongoMapper.Delete(ctx, req.UserId)
if err != nil {
log.CtxError(ctx, "删除用户信息异常[%v]\n", err)
return resp, err
}
return resp, nil
}

func (s *UserServiceImpl) SearchUser(ctx context.Context, req *gencontent.SearchUserReq) (resp *gencontent.SearchUserResp, err error) {
resp = new(gencontent.SearchUserResp)
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
user, total, err := s.UserEsMapper.Search(ctx, req.Keyword, p, esp.ScoreCursorType)
if err != nil {
log.CtxError(ctx, "搜索用户信息异常[%v]\n", err)
return resp, err
}

if p.LastToken != nil {
resp.LastToken = *p.LastToken
}
resp.Total = total
resp.Users = make([]*gencontent.User, 0, len(user))
for _, u := range user {
resp.Users = append(resp.Users, convertor.UserMapperToUser(u))
}

return resp, nil
}

func (s *UserServiceImpl) GetUserDetail(ctx context.Context, req *gencontent.GetUserDetailReq) (resp *gencontent.GetUserDetailResp, err error) {
resp = new(gencontent.GetUserDetailResp)
var user *usermapper.User
user, err = s.UserMongoMapper.FindOne(ctx, req.UserId)
if err != nil {
log.CtxError(ctx, "查询用户信息异常[%v]\n", err)
return resp, err
}

resp.UserDetail = convertor.UserMapperToUserDetail(user)
return resp, nil
}

func (s *UserServiceImpl) GetUser(ctx context.Context, req *gencontent.GetUserReq) (resp *gencontent.GetUserResp, err error) {
resp = new(gencontent.GetUserResp)
var user *usermapper.User
user, err = s.UserMongoMapper.FindOne(ctx, req.UserId)
if err != nil {
log.CtxError(ctx, "查询用户信息异常[%v]\n", err)
return resp, err
}

resp.User = convertor.UserMapperToUser(user)
return resp, nil
}

func (s *UserServiceImpl) CreateUser(ctx context.Context, req *gencontent.CreateUserReq) (resp *gencontent.CreateUserResp, err error) {
resp = new(gencontent.CreateUserResp)
ID, err := primitive.ObjectIDFromHex(req.UserInfo.UserId)
if err != nil {
return resp, consts.ErrInvalidObjectId
}
if _, err = s.UserMongoMapper.Insert(ctx, &usermapper.User{
ID: ID,
Name: req.UserInfo.Name,
Sex: int32(req.UserInfo.Sex),
Description: consts.DefaultDescription,
Url: consts.DefaultAvatarUrl,
}); err != nil {
log.CtxError(ctx, "插入用户信息异常[%v]\n", err)
return resp, err
}
return resp, nil
}

func (s *UserServiceImpl) UpdateUser(ctx context.Context, req *gencontent.UpdateUserReq) (resp *gencontent.UpdateUserResp, err error) {
resp = new(gencontent.UpdateUserResp)
if _, err = s.UserMongoMapper.Update(ctx, convertor.UserDetailToUserMapper(req.UserDetailInfo)); err != nil {
log.CtxError(ctx, "修改用户信息异常[%v]\n", err)
return resp, err
}
return resp, nil
}
7 changes: 7 additions & 0 deletions biz/infrastructure/consts/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package consts

import (
"errors"
"google.golang.org/grpc/status"
)

Expand All @@ -11,4 +12,10 @@ var (
ErrFileIsNotDir = status.Error(10104, "目标文件不是文件夹")
ErrNotFound = status.Error(10105, "数据不存在")
ErrInvalidDeleteType = status.Error(10106, "删除类型无效")
ErrDataBase = status.Error(10001, "数据库异常")
ErrEsMapper = status.Error(10002, "Es异常")
)

var (
ErrInvalidObjectId = errors.New("invalid objectId")
)
33 changes: 19 additions & 14 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package consts

const (
ID = "_id"
UserId = "userId"
Name = "name"
Type = "type"
Path = "path"
FatherId = "fatherId"
Size = "size"
FileMd5 = "fileMd5"
IsDel = "isDel"
Tag = "tag"
Description = "description"
CreateAt = "createAt"
UpdateAt = "updateAt"
DeletedAt = "deletedAt"
ID = "_id"
UserId = "userId"
Name = "name"
Type = "type"
Path = "path"
FatherId = "fatherId"
Size = "size"
FileMd5 = "fileMd5"
IsDel = "isDel"
Tag = "tag"
Description = "description"
CreateAt = "createAt"
UpdateAt = "updateAt"
DeletedAt = "deletedAt"
TargetId = "targetId"
TargetType = "targetType"
RelationType = "relationType"
DefaultAvatarUrl = "d2042520dce2223751906a11e547d43e.png"
DefaultDescription = "点击添加描述,让大家更好的了解你..."
)
42 changes: 42 additions & 0 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package convertor

import (
usermapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/user"
gencontent "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/content"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func UserMapperToUserDetail(in *usermapper.User) *gencontent.UserDetail {
return &gencontent.UserDetail{
Name: in.Name,
Sex: in.Sex,
FullName: in.FullName,
IdCard: in.IdCard,
CreatedAt: in.CreateAt.UnixMilli(),
UpdatedAt: in.UpdateAt.UnixMilli(),
Description: in.Description,
Url: in.Url,
UserId: in.ID.Hex(),
}
}

func UserDetailToUserMapper(in *gencontent.UserDetailInfo) *usermapper.User {
ID, _ := primitive.ObjectIDFromHex(in.UserId)
return &usermapper.User{
ID: ID,
Name: in.Name,
Sex: int32(in.GetSex()),
FullName: in.FullName,
IdCard: in.IdCard,
Description: in.Description,
Url: in.Url,
}
}

func UserMapperToUser(in *usermapper.User) *gencontent.User {
return &gencontent.User{
UserId: in.ID.Hex(),
Name: in.Name,
Url: in.Url,
}
}
112 changes: 112 additions & 0 deletions biz/infrastructure/mapper/user/es.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package user

import (
"context"
"fmt"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts"
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pagination/esp"
"github.com/bytedance/sonic"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/mitchellh/mapstructure"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/logx"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)

type (
UserEsMapper interface {
Search(ctx context.Context, keyword string, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*User, int32, error)
}

EsMapper struct {
es *elasticsearch.TypedClient
IndexName string
}
)

func (e *EsMapper) Search(ctx context.Context, keyword string, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*User, int32, error) {
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
return nil, 0, err
}
res, err := e.es.Search().Index(e.IndexName).Request(&search.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Must: []types.Query{
{
MultiMatch: &types.MultiMatchQuery{
Fields: []string{consts.Name, consts.ID},
Query: keyword,
},
},
},
},
},
Sort: s,
SearchAfter: sa,
Size: lo.ToPtr(int(*popts.Limit)),
}).Do(ctx)
if err != nil {
logx.Errorf("es查询异常[%v]\n", err)
return nil, 0, err
}

total := res.Hits.Total.Value
users := make([]*User, 0, len(res.Hits.Hits))
for _, hit := range res.Hits.Hits {
user := &User{}
source := make(map[string]any)
err = sonic.Unmarshal(hit.Source_, &source)
if err != nil {
return nil, 0, err
}
if source[consts.CreateAt], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.CreateAt].(string)); err != nil {
return nil, 0, err
}
if source[consts.UpdateAt], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.UpdateAt].(string)); err != nil {
return nil, 0, err
}
err = mapstructure.Decode(source, user)
if err != nil {
return nil, 0, err
}

oid := hit.Id_
user.ID, err = primitive.ObjectIDFromHex(oid)
if err != nil {
return nil, 0, err
}
user.Score_ = float64(hit.Score_)
users = append(users, user)
}

if *popts.Backward {
users = lo.Reverse(users)
}

// 更新游标
if len(users) > 0 {
err = p.StoreCursor(ctx, users[0], users[len(users)-1])
if err != nil {
return nil, 0, err
}
}
return users, int32(total), nil
}

func NewEsMapper(config *config.Config) UserEsMapper {
es, err := elasticsearch.NewTypedClient(elasticsearch.Config{Addresses: config.Elasticsearch.Addresses, Username: config.Elasticsearch.Username, Password: config.Elasticsearch.Password})
if err != nil {
logx.Errorf("elasticsearch连接异常[%v]\n", err)
}
return &EsMapper{
es: es,
IndexName: fmt.Sprintf("%s.%s", config.Mongo.DB, CollectionName),
}
}
Loading

0 comments on commit 360744f

Please sign in to comment.