Skip to content

Commit

Permalink
feat: 增加小鱼干部分内容
Browse files Browse the repository at this point in the history
  • Loading branch information
Cbgogogog committed Aug 16, 2023
1 parent 7b87967 commit 93d7577
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 8 deletions.
20 changes: 20 additions & 0 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ type ContentServerImpl struct {
PlanService service.IPlanService
}

func (s *ContentServerImpl) DonateFish(ctx context.Context, req *content.DonateFishReq) (res *content.DonateFishResp, err error) {
return s.PlanService.DonateFish(ctx, req)

}

func (s *ContentServerImpl) AddUserFish(ctx context.Context, req *content.AddUserFishReq) (res *content.AddUserFishResp, err error) {
return s.PlanService.AddUserFish(ctx, req)

}

func (s *ContentServerImpl) ListFishByPlan(ctx context.Context, req *content.ListFishByPlanReq) (res *content.ListFishByPlanResp, err error) {
return s.PlanService.ListFishByPlan(ctx, req)

}

func (s *ContentServerImpl) RetrieveUserFish(ctx context.Context, req *content.RetrieveUserFishReq) (res *content.RetrieveUserFishResp, err error) {
return s.PlanService.RetrieveUserFish(ctx, req)

}

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

Expand Down
111 changes: 108 additions & 3 deletions biz/application/service/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"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/donate"
Fish "github.com/xh-polaris/meowchat-content/biz/infrastructure/mapper/fish"
"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"
"sort"
"time"
)

Expand All @@ -20,11 +23,17 @@ type IPlanService interface {
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)
DonateFish(ctx context.Context, req *content.DonateFishReq) (*content.DonateFishResp, error)
AddUserFish(ctx context.Context, req *content.AddUserFishReq) (*content.AddUserFishResp, error)
ListFishByPlan(ctx context.Context, req *content.ListFishByPlanReq) (*content.ListFishByPlanResp, error)
RetrieveUserFish(ctx context.Context, req *content.RetrieveUserFishReq) (*content.RetrieveUserFishResp, error)
}

type PlanService struct {
PlanMongoMapper plan.IMongoMapper
PlanEsMapper plan.IEsMapper
PlanMongoMapper plan.IMongoMapper
PlanEsMapper plan.IEsMapper
DonateMongoMapper donate.IMongoMapper
FishMongoMapper Fish.IMongoMapper
}

var PlanSet = wire.NewSet(
Expand All @@ -42,7 +51,6 @@ func (s *PlanService) ListPlan(ctx context.Context, req *content.ListPlanReq) (*
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
}
Expand Down Expand Up @@ -155,3 +163,100 @@ func (s *PlanService) DeletePlan(ctx context.Context, req *content.DeletePlanReq
}
return &content.DeletePlanResp{}, nil
}

func (s *PlanService) DonateFish(ctx context.Context, req *content.DonateFishReq) (*content.DonateFishResp, error) {
data, err := s.FishMongoMapper.FindOne(ctx, req.UserId)
if err != nil {
return nil, err
}
data.FishNum = data.FishNum - req.Fish
err = s.FishMongoMapper.Update(ctx, data)
if err != nil {
return nil, err
}

err = s.DonateMongoMapper.Insert(ctx, &donate.Donate{
UserId: req.UserId,
PlanId: req.PlanId,
FishNum: req.Fish,
})
if err != nil {
return nil, err
}
res, err := s.PlanMongoMapper.FindOne(ctx, req.PlanId)
if err != nil {
return nil, err
}
res.NowFish = res.NowFish + req.Fish
err = s.PlanMongoMapper.Update(ctx, res)
if err != nil {
return nil, err
}

return &content.DonateFishResp{}, nil
}

func (s *PlanService) AddUserFish(ctx context.Context, req *content.AddUserFishReq) (*content.AddUserFishResp, error) {
data, err := s.FishMongoMapper.FindOne(ctx, req.UserId)
switch err {
case nil:
data.FishNum = data.FishNum + req.Fish
err = s.FishMongoMapper.Update(ctx, data)
if err != nil {
return nil, err
}
return &content.AddUserFishResp{}, nil
case consts.ErrNotFound:
oid, err := primitive.ObjectIDFromHex(req.UserId)
if err != nil {
return nil, err
}
err = s.FishMongoMapper.Insert(ctx, &Fish.Fish{
UserId: oid,
FishNum: req.Fish,
})
if err != nil {
return nil, err
}
return &content.AddUserFishResp{}, nil
default:
return nil, err
}
}

func (s *PlanService) ListFishByPlan(ctx context.Context, req *content.ListFishByPlanReq) (*content.ListFishByPlanResp, error) {
data, err := s.DonateMongoMapper.ListDonateByPlan(ctx, req.PlanId)
if err != nil {
return nil, err
}
fishMap := make(map[string]int64, len(data))
userIds := make([]string, 0, len(data))
for _, value := range data {
i, ok := fishMap[value.UserId]
if ok == true {
fishMap[value.UserId] = value.FishNum + i
} else {
fishMap[value.UserId] = value.FishNum
userIds = append(userIds, value.UserId)
}
}
sort.Slice(userIds, func(i, j int) bool {
return fishMap[userIds[i]] > fishMap[userIds[j]]
})
return &content.ListFishByPlanResp{
UserIds: userIds,
FishMap: fishMap,
}, nil
}

func (s *PlanService) RetrieveUserFish(ctx context.Context, req *content.RetrieveUserFishReq) (*content.RetrieveUserFishResp, error) {
data, err := s.FishMongoMapper.FindOne(ctx, req.UserId)
switch err {
case nil:
return &content.RetrieveUserFishResp{Fish: data.FishNum}, nil
case consts.ErrNotFound:
return &content.RetrieveUserFishResp{Fish: 0}, nil
default:
return nil, err
}
}
2 changes: 2 additions & 0 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ const (
StartTime = "startTime"
EndTime = "endTime"
InitiatorIds = "initiatorIds"
PlanId = "planId"
FishNum = "fishNum"
)
131 changes: 131 additions & 0 deletions biz/infrastructure/mapper/donate/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package donate

import (
"context"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"time"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/zeromicro/go-zero/core/stores/monc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)

var _ IMongoMapper = (*MongoMapper)(nil)

const prefixDonateCacheKey = "cache:donate:"
const CollectionName = "donate"

type (
IMongoMapper interface {
Insert(ctx context.Context, data *Donate) error
FindOne(ctx context.Context, id string) (*Donate, error)
Update(ctx context.Context, data *Donate) error
Delete(ctx context.Context, id string) error
ListDonateByPlan(ctx context.Context, planId string) ([]*Donate, error)
ListDonateByUser(ctx context.Context, userId string) ([]*Donate, error)
}

MongoMapper struct {
conn *monc.Model
}

Donate struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
UserId string `bson:"userId,omitempty" json:"userId,omitempty"`
PlanId string `bson:"planId,omitempty" json:"planId,omitempty"`
FishNum int64 `bson:"fishNum,omitempty" json:"fishNum,omitempty"`
}
)

func NewMongoMapper(config *config.Config) IMongoMapper {
conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.Cache)
return &MongoMapper{
conn: conn,
}
}

func (m *MongoMapper) Insert(ctx context.Context, data *Donate) error {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
data.CreateAt = time.Now()
data.UpdateAt = time.Now()
}

key := prefixDonateCacheKey + data.ID.Hex()
_, err := m.conn.InsertOne(ctx, key, data)
return err
}

func (m *MongoMapper) FindOne(ctx context.Context, id string) (*Donate, error) {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, consts.ErrInvalidObjectId
}

var data Donate
key := prefixDonateCacheKey + id
err = m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid})
switch err {
case nil:
return &data, nil
case monc.ErrNotFound:
return nil, consts.ErrNotFound
default:
return nil, err
}
}

func (m *MongoMapper) Update(ctx context.Context, data *Donate) error {
data.UpdateAt = time.Now()
key := prefixDonateCacheKey + data.ID.Hex()
_, err := m.conn.ReplaceOne(ctx, key, bson.M{consts.ID: data.ID}, data)
return err
}

func (m *MongoMapper) Delete(ctx context.Context, id string) error {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return consts.ErrInvalidObjectId
}
key := prefixDonateCacheKey + id
_, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid})
return err
}

func (m *MongoMapper) ListDonateByPlan(ctx context.Context, planId string) ([]*Donate, error) {
var data []*Donate
var err error

opts := options.FindOptions{
Sort: bson.M{consts.FishNum: -1},
}
filter := bson.M{consts.PlanId: planId}

err = m.conn.Find(ctx, &data, filter, &opts)
if err != nil {
return nil, err
} else {
return data, nil
}
}

func (m *MongoMapper) ListDonateByUser(ctx context.Context, userId string) ([]*Donate, error) {
var data []*Donate
var err error

opts := options.FindOptions{
Sort: bson.M{consts.FishNum: -1},
}
filter := bson.M{consts.UserId: userId}

err = m.conn.Find(ctx, &data, filter, &opts)
if err != nil {
return nil, err
} else {
return data, nil
}
}
88 changes: 88 additions & 0 deletions biz/infrastructure/mapper/fish/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fish

import (
"context"
"github.com/xh-polaris/meowchat-content/biz/infrastructure/consts"
"time"

"github.com/xh-polaris/meowchat-content/biz/infrastructure/config"
"github.com/zeromicro/go-zero/core/stores/monc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

var _ IMongoMapper = (*MongoMapper)(nil)

const prefixFishCacheKey = "cache:fish:"
const CollectionName = "fish"

type (
IMongoMapper interface {
Insert(ctx context.Context, data *Fish) error
FindOne(ctx context.Context, id string) (*Fish, error)
Update(ctx context.Context, data *Fish) error
Delete(ctx context.Context, id string) error
}

MongoMapper struct {
conn *monc.Model
}

Fish struct {
UserId primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
FishNum int64 `bson:"fishNum,omitempty" json:"fishNum,omitempty"`
}
)

func NewMongoMapper(config *config.Config) IMongoMapper {
conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.Cache)
return &MongoMapper{
conn: conn,
}
}

func (m *MongoMapper) Insert(ctx context.Context, data *Fish) error {
data.CreateAt = time.Now()
data.UpdateAt = time.Now()
key := prefixFishCacheKey + data.UserId.Hex()
_, err := m.conn.InsertOne(ctx, key, data)
return err
}

func (m *MongoMapper) FindOne(ctx context.Context, id string) (*Fish, error) {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, consts.ErrInvalidObjectId
}

var data Fish
key := prefixFishCacheKey + id
err = m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid})
switch err {
case nil:
return &data, nil
case monc.ErrNotFound:
return nil, consts.ErrNotFound
default:
return nil, err
}
}

func (m *MongoMapper) Update(ctx context.Context, data *Fish) error {
data.UpdateAt = time.Now()
key := prefixFishCacheKey + data.UserId.Hex()
_, err := m.conn.ReplaceOne(ctx, key, bson.M{consts.ID: data.UserId}, data)
return err
}

func (m *MongoMapper) Delete(ctx context.Context, id string) error {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return consts.ErrInvalidObjectId
}
key := prefixFishCacheKey + id
_, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid})
return err
}
Loading

0 comments on commit 93d7577

Please sign in to comment.