Skip to content

Commit

Permalink
feat: 增加plan模块
Browse files Browse the repository at this point in the history
  • Loading branch information
Cbgogogog committed Aug 10, 2023
1 parent bbf22cf commit 93432f0
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 25 deletions.
29 changes: 29 additions & 0 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ type ContentServerImpl struct {
ImageService service.IImageService
MomentService service.IMomentService
PostService service.IPostService
PlanService service.IPlanService
}

func (s *ContentServerImpl) ListPlan(ctx context.Context, req *content.ListPlanReq) (res *content.ListPlanResp, err error) {
return s.PlanService.ListPlan(ctx, req)

}

func (s *ContentServerImpl) CountPlan(ctx context.Context, req *content.CountPlanReq) (res *content.CountPlanResp, err error) {
return s.PlanService.CountPlan(ctx, req)

}

func (s *ContentServerImpl) RetrievePlan(ctx context.Context, req *content.RetrievePlanReq) (res *content.RetrievePlanResp, err error) {
return s.PlanService.RetrievePlan(ctx, req)

}

func (s *ContentServerImpl) CreatePlan(ctx context.Context, req *content.CreatePlanReq) (res *content.CreatePlanResp, err error) {
return s.PlanService.CreatePlan(ctx, req)

}

func (s *ContentServerImpl) UpdatePlan(ctx context.Context, req *content.UpdatePlanReq) (res *content.UpdatePlanResp, err error) {
return s.PlanService.UpdatePlan(ctx, req)
}

func (s *ContentServerImpl) DeletePlan(ctx context.Context, req *content.DeletePlanReq) (res *content.DeletePlanResp, err error) {
return s.PlanService.DeletePlan(ctx, req)
}

func (s *ContentServerImpl) SearchCat(ctx context.Context, req *content.SearchCatReq) (res *content.SearchCatResp, err error) {
Expand Down
157 changes: 157 additions & 0 deletions biz/application/service/plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package service

import (
"context"
"github.com/google/wire"
"github.com/xh-polaris/gopkg/pagination/esp"
"github.com/xh-polaris/gopkg/pagination/mongop"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/mapper/plan"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/util/convertor"
"github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowchat/content"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)

type IPlanService interface {
ListPlan(ctx context.Context, req *content.ListPlanReq) (*content.ListPlanResp, error)
CountPlan(ctx context.Context, req *content.CountPlanReq) (*content.CountPlanResp, error)
RetrievePlan(ctx context.Context, req *content.RetrievePlanReq) (*content.RetrievePlanResp, error)
CreatePlan(ctx context.Context, req *content.CreatePlanReq) (*content.CreatePlanResp, error)
UpdatePlan(ctx context.Context, req *content.UpdatePlanReq) (*content.UpdatePlanResp, error)
DeletePlan(ctx context.Context, req *content.DeletePlanReq) (*content.DeletePlanResp, error)
}

type PlanService struct {
PlanMongoMapper plan.IMongoMapper
PlanEsMapper plan.IEsMapper
}

var PlanSet = wire.NewSet(
wire.Struct(new(PlanService), "*"),
wire.Bind(new(IPlanService), new(*PlanService)),
)

func (s *PlanService) ListPlan(ctx context.Context, req *content.ListPlanReq) (*content.ListPlanResp, error) {
resp := new(content.ListPlanResp)
var plans []*plan.Plan
var total int64
var err error

filter := convertor.ParsePlanFilter(req.FilterOptions)
p := convertor.ParsePagination(req.PaginationOptions)
if req.SearchOptions == nil {
plans, total, err = s.PlanMongoMapper.FindManyAndCount(ctx, filter, p, mongop.IdCursorType)
print(plans)
if err != nil {
return nil, err
}
} else {
switch o := req.SearchOptions.Type.(type) {
case *content.SearchOptions_AllFieldsKey:
plans, total, err = s.PlanEsMapper.Search(ctx, convertor.ConvertPlanAllFieldsSearchQuery(o), filter, p, esp.ScoreCursorType)
case *content.SearchOptions_MultiFieldsKey:
plans, total, err = s.PlanEsMapper.Search(ctx, convertor.ConvertPlanMultiFieldsSearchQuery(o), filter, p, esp.ScoreCursorType)
}
if err != nil {
return nil, err
}
}

resp.Total = total
if p.LastToken != nil {
resp.Token = *p.LastToken
}
resp.Plans = make([]*content.Plan, 0, len(plans))
for _, Plan_ := range plans {
resp.Plans = append(resp.Plans, convertor.ConvertPlan(Plan_))
}

return resp, nil
}

func (s *PlanService) CountPlan(ctx context.Context, req *content.CountPlanReq) (*content.CountPlanResp, error) {
resp := new(content.CountPlanResp)
var err error
filter := convertor.ParsePlanFilter(req.FilterOptions)
if req.SearchOptions == nil {
resp.Total, err = s.PlanMongoMapper.Count(ctx, filter)
if err != nil {
return nil, err
}
} else {
switch o := req.SearchOptions.Type.(type) {
case *content.SearchOptions_AllFieldsKey:
resp.Total, err = s.PlanEsMapper.CountWithQuery(ctx, convertor.ConvertPlanAllFieldsSearchQuery(o), filter)
case *content.SearchOptions_MultiFieldsKey:
resp.Total, err = s.PlanEsMapper.CountWithQuery(ctx, convertor.ConvertPlanMultiFieldsSearchQuery(o), filter)
}
if err != nil {
return nil, err
}
}

return resp, nil
}

func (s *PlanService) RetrievePlan(ctx context.Context, req *content.RetrievePlanReq) (*content.RetrievePlanResp, error) {
data, err := s.PlanMongoMapper.FindOne(ctx, req.PlanId)
if err != nil {
return nil, err
}
m := convertor.ConvertPlan(data)
return &content.RetrievePlanResp{Plan: m}, nil
}

func (s *PlanService) CreatePlan(ctx context.Context, req *content.CreatePlanReq) (*content.CreatePlanResp, error) {
m := req.Plan
data := &plan.Plan{
CatId: m.CatId,
PlanType: m.PlanType,
StartTime: time.Unix(m.StartTime, 0),
EndTime: time.Unix(m.EndTime, 0),
Description: m.Description,
ImageUrls: m.ImageUrls,
Name: m.Name,
InitiatorIds: m.InitiatorIds,
}

err := s.PlanMongoMapper.Insert(ctx, data)
if err != nil {
return nil, err
}

return &content.CreatePlanResp{PlanId: data.ID.Hex()}, nil
}

func (s *PlanService) UpdatePlan(ctx context.Context, req *content.UpdatePlanReq) (*content.UpdatePlanResp, error) {
m := req.Plan
PlanId, err := primitive.ObjectIDFromHex(m.Id)
if err != nil {
return nil, consts.ErrInvalidObjectId
}

err = s.PlanMongoMapper.Update(ctx, &plan.Plan{
ID: PlanId,
CatId: m.CatId,
PlanType: m.PlanType,
StartTime: time.Unix(m.StartTime, 0),
EndTime: time.Unix(m.EndTime, 0),
Description: m.Description,
ImageUrls: m.ImageUrls,
InitiatorIds: m.InitiatorIds,
})
if err != nil {
return nil, err
}

return &content.UpdatePlanResp{}, nil
}

func (s *PlanService) DeletePlan(ctx context.Context, req *content.DeletePlanReq) (*content.DeletePlanResp, error) {
err := s.PlanMongoMapper.Delete(ctx, req.PlanId)
if err != nil {
return nil, err
}
return &content.DeletePlanResp{}, nil
}
34 changes: 19 additions & 15 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package consts

const (
ID = "_id"
CatId = "catId"
CommunityId = "communityId"
Title = "title"
Text = "text"
Tags = "tags"
UserId = "userId"
Flags = "flags"
Area = "area"
Color = "color"
Details = "details"
Name = "name"
Score = "_score"
UpdateAt = "updateAt"
CreateAt = "createAt"
ID = "_id"
CatId = "catId"
CommunityId = "communityId"
Title = "title"
Text = "text"
Description = "description"
Tags = "tags"
UserId = "userId"
Flags = "flags"
Area = "area"
Color = "color"
Details = "details"
Name = "name"
Score = "_score"
UpdateAt = "updateAt"
CreateAt = "createAt"
StartTime = "startTime"
EndTime = "endTime"
InitiatorIds = "initiatorIds"
)
140 changes: 140 additions & 0 deletions biz/infrastructure/mapper/plan/es.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package plan

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/count"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/xh-polaris/gopkg/pagination"
"github.com/xh-polaris/gopkg/pagination/esp"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"log"
"net/http"
"time"

"github.com/elastic/go-elasticsearch/v8"
"github.com/mitchellh/mapstructure"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type (
IEsMapper interface {
Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Plan, int64, error)
CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error)
}

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

func NewEsMapper(config *config.Config) IEsMapper {
esClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{
Username: config.Elasticsearch.Username,
Password: config.Elasticsearch.Password,
Addresses: config.Elasticsearch.Addresses,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
})
if err != nil {
log.Fatal(err)
}
return &EsMapper{
es: esClient,
indexName: fmt.Sprintf("%s.%s-alias", config.Mongo.DB, CollectionName),
}
}

func (m *EsMapper) Search(ctx context.Context, query []types.Query, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter esp.EsCursor) ([]*Plan, int64, error) {
p := esp.NewEsPaginator(pagination.NewRawStore(sorter), popts)
s, sa, err := p.MakeSortOptions(ctx)
if err != nil {
return nil, 0, err
}
f := makeEsFilter(fopts)
res, err := m.es.Search().From(int(*popts.Offset)).Size(int(*popts.Limit)).Index(m.indexName).Request(&search.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Must: query,
Filter: f,
},
},
Sort: s,
SearchAfter: sa,
}).Do(ctx)
if err != nil {
return nil, 0, err
}

hits := res.Hits.Hits
total := res.Hits.Total.Value
datas := make([]*Plan, 0, len(hits))
for i := range hits {
hit := hits[i]
var source map[string]any
err = json.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
}
if source[consts.StartTime], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.StartTime].(string)); err != nil {
return nil, 0, err
}
if source[consts.EndTime], err = time.Parse("2006-01-02T15:04:05Z07:00", source[consts.EndTime].(string)); err != nil {
return nil, 0, err
}
data := &Plan{}
err = mapstructure.Decode(source, data)
if err != nil {
return nil, 0, err
}
oid := hit.Id_
data.ID, err = primitive.ObjectIDFromHex(oid)
if err != nil {
return nil, 0, err
}
data.Score_ = float64(hit.Score_)
datas = append(datas, data)
}
// 如果是反向查询,反转数据
if *popts.Backward {
for i := 0; i < len(datas)/2; i++ {
datas[i], datas[len(datas)-i-1] = datas[len(datas)-i-1], datas[i]
}
}
if len(datas) > 0 {
err = p.StoreCursor(ctx, datas[0], datas[len(datas)-1])
if err != nil {
return nil, 0, err
}
}
return datas, total, nil
}

func (m *EsMapper) CountWithQuery(ctx context.Context, query []types.Query, fopts *FilterOptions) (int64, error) {
f := makeEsFilter(fopts)
res, err := m.es.Count().Index(m.indexName).Request(&count.Request{
Query: &types.Query{
Bool: &types.BoolQuery{
Must: query,
Filter: f,
},
},
}).Do(ctx)
if err != nil {
return 0, err
}

return res.Count, nil
}
Loading

0 comments on commit 93432f0

Please sign in to comment.