Skip to content

Commit

Permalink
feat: 添加批量创建通知 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Jan 28, 2024
1 parent d45c6d9 commit 400671c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
8 changes: 2 additions & 6 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ func (s *SystemServerImpl) GetNotificationCount(ctx context.Context, req *system
return s.SystemService.GetNotificationCount(ctx, req)
}

func (s *SystemServerImpl) ReadNotification(ctx context.Context, req *system.ReadNotificationReq) (res *system.ReadNotificationResp, err error) {
return s.SystemService.ReadNotification(ctx, req)
}

func (s *SystemServerImpl) CreateNotification(ctx context.Context, req *system.CreateNotificationReq) (res *system.CreateNotificationResp, err error) {
return s.SystemService.CreateNotification(ctx, req)
func (s *SystemServerImpl) CreateNotifications(ctx context.Context, req *system.CreateNotificationsReq) (res *system.CreateNotificationsResp, err error) {
return s.SystemService.CreateNotifications(ctx, req)
}
20 changes: 7 additions & 13 deletions biz/application/service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
)

type SystemService interface {
ReadNotification(ctx context.Context, req *gensystem.ReadNotificationReq) (resp *gensystem.ReadNotificationResp, err error)
GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error)
GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error)
CreateNotification(ctx context.Context, req *gensystem.CreateNotificationReq) (resp *gensystem.CreateNotificationResp, err error)
CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error)
ReadNotifications(ctx context.Context, req *gensystem.ReadNotificationsReq) (resp *gensystem.ReadNotificationsResp, err error)
CleanNotification(ctx context.Context, req *gensystem.CleanNotificationReq) (resp *gensystem.CleanNotificationResp, err error)
DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error)
Expand Down Expand Up @@ -72,14 +71,6 @@ func (s *SystemServiceImpl) GetSliders(ctx context.Context, req *gensystem.GetSl
return resp, nil
}

func (s *SystemServiceImpl) ReadNotification(ctx context.Context, req *gensystem.ReadNotificationReq) (resp *gensystem.ReadNotificationResp, err error) {
resp = new(gensystem.ReadNotificationResp)
if err = s.NotificationMongoMapper.ReadNotification(ctx, req.NotificationId); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error) {
resp = new(gensystem.GetNotificationsResp)
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
Expand Down Expand Up @@ -114,9 +105,12 @@ func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensy
return resp, nil
}

func (s *SystemServiceImpl) CreateNotification(ctx context.Context, req *gensystem.CreateNotificationReq) (resp *gensystem.CreateNotificationResp, err error) {
resp = new(gensystem.CreateNotificationResp)
if err = s.NotificationMongoMapper.InsertOne(ctx, convertor.NotificationToNotificationMapper(req.Notification)); err != nil {
func (s *SystemServiceImpl) CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error) {
resp = new(gensystem.CreateNotificationsResp)
notifications := lo.Map[*gensystem.Notification, *notificationmapper.Notification](req.Notifications, func(item *gensystem.Notification, _ int) *notificationmapper.Notification {
return convertor.NotificationToNotificationMapper(item)
})
if err = s.NotificationMongoMapper.InsertMany(ctx, notifications); err != nil {
return resp, err
}
return resp, nil
Expand Down
20 changes: 11 additions & 9 deletions biz/infrastructure/mapper/notification/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type (
ReadNotification(ctx context.Context, id string) error
Count(ctx context.Context, fopts *FilterOptions) (int64, error)
ReadNotifications(ctx context.Context, fopts *FilterOptions) error
InsertOne(ctx context.Context, data *Notification) error
InsertMany(ctx context.Context, data []*Notification) error
GetNotificationsAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error)
}
Notification struct {
Expand Down Expand Up @@ -97,14 +97,16 @@ func (m *MongoMapper) GetNotificationsAndCount(ctx context.Context, fopts *Filte

return data, count, nil
}
func (m *MongoMapper) InsertOne(ctx context.Context, data *Notification) error {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
}
data.CreateAt = time.Now()
data.UpdateAt = time.Now()
key := prefixNotificationCacheKey + data.ID.Hex()
_, err := m.conn.InsertOne(ctx, key, data)
func (m *MongoMapper) InsertMany(ctx context.Context, datas []*Notification) error {
lo.ForEach(datas, func(data *Notification, _ int) {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
}
data.CreateAt = time.Now()
data.UpdateAt = time.Now()
})

_, err := m.conn.InsertMany(ctx, lo.ToAnySlice(datas))
return err
}
func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error) {
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/CloudStriver/go-pkg v0.0.0-20240117111745-b4ba57a38f44
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240126122141-e60927663187
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240127152507-ef2faaa085cd
github.com/cloudwego/kitex v0.8.0
github.com/google/wire v0.5.0
github.com/kitex-contrib/obs-opentelemetry v0.2.5
Expand All @@ -14,6 +14,10 @@ require (
google.golang.org/grpc v1.61.0
)

//replace (
// github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go
//)

require (
github.com/apache/thrift v0.16.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/CloudStriver/go-pkg v0.0.0-20240117111745-b4ba57a38f44 h1:7exnHSz2LCe
github.com/CloudStriver/go-pkg v0.0.0-20240117111745-b4ba57a38f44/go.mod h1:SsAxWs5EIcaDE/0e5buoFOWsM4lTvFZhySkV68+RT3g=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240126122141-e60927663187 h1:x4nwoY+7PbBe7GpMeY4svfiz1YepvjIcB6N5L2oyvjg=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240126122141-e60927663187/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240127152507-ef2faaa085cd h1:sC6f5hFtiUN51BCIbkfgteLn9UjRpWnjgl77fo7MzFw=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240127152507-ef2faaa085cd/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down

0 comments on commit 400671c

Please sign in to comment.