Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修改通知的创建,修改,获取 #7

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/cloudmind-system.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

4 changes: 0 additions & 4 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func (s *SystemServerImpl) UpdateNotifications(ctx context.Context, req *system.
return s.SystemService.UpdateNotifications(ctx, req)
}

func (s *SystemServerImpl) DeleteNotifications(ctx context.Context, req *system.DeleteNotificationsReq) (resp *system.DeleteNotificationsResp, err error) {
return s.SystemService.DeleteNotifications(ctx, req)
}

func (s *SystemServerImpl) GetSliders(ctx context.Context, req *system.GetSlidersReq) (resp *system.GetSlidersResp, err error) {
return s.SystemService.GetSliders(ctx, req)
}
Expand Down
51 changes: 20 additions & 31 deletions biz/application/service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import (
)

type SystemService interface {
GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error)
GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error)
CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error)
DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error)
UpdateSlider(ctx context.Context, req *gensystem.UpdateSliderReq) (resp *gensystem.UpdateSliderResp, err error)
CreateSlider(ctx context.Context, req *gensystem.CreateSliderReq) (resp *gensystem.CreateSliderResp, err error)
GetSliders(ctx context.Context, req *gensystem.GetSlidersReq) (resp *gensystem.GetSlidersResp, err error)
DeleteNotifications(ctx context.Context, req *gensystem.DeleteNotificationsReq) (resp *gensystem.DeleteNotificationsResp, 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)
CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error)
UpdateNotifications(ctx context.Context, req *gensystem.UpdateNotificationsReq) (resp *gensystem.UpdateNotificationsResp, err error)
}

Expand All @@ -30,25 +29,12 @@ type SystemServiceImpl struct {
SliderMongoMapper slidermapper.ISliderMongoMapper
}

func (s *SystemServiceImpl) DeleteNotifications(ctx context.Context, req *gensystem.DeleteNotificationsReq) (resp *gensystem.DeleteNotificationsResp, err error) {
if err = s.NotificationMongoMapper.DeleteNotifications(ctx, &notificationmapper.FilterOptions{
OnlyUserId: req.OnlyUserId,
OnlyType: req.OnlyType,
OnlyIsRead: req.OnlyIsRead,
OnlyNotificationIds: req.OnlyNotificationIds,
}); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) UpdateNotifications(ctx context.Context, req *gensystem.UpdateNotificationsReq) (resp *gensystem.UpdateNotificationsResp, err error) {
if err = s.NotificationMongoMapper.UpdateNotifications(ctx, &notificationmapper.FilterOptions{
OnlyUserId: req.OnlyUserId,
OnlyType: req.OnlyType,
OnlyIsRead: req.OnlyIsRead,
OnlyNotificationIds: req.OnlyNotificationIds,
}, req.IsRead); err != nil {
OnlyUserId: lo.ToPtr(req.UserId),
OnlyType: req.OnlyType,
OnlyStatus: lo.ToPtr(int64(gensystem.NotificationStatus_NotRead)),
}); err != nil {
return resp, err
}
return resp, nil
Expand Down Expand Up @@ -113,10 +99,9 @@ func (s *SystemServiceImpl) GetSliders(ctx context.Context, req *gensystem.GetSl
func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error) {
resp = new(gensystem.GetNotificationsResp)
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
notifications, total, err := s.NotificationMongoMapper.GetNotificationsAndCount(ctx, &notificationmapper.FilterOptions{
OnlyUserId: req.OnlyUserId,
notifications, err := s.NotificationMongoMapper.GetNotifications(ctx, &notificationmapper.FilterOptions{
OnlyUserId: lo.ToPtr(req.UserId),
OnlyType: req.OnlyType,
OnlyIsRead: req.OnlyIsRead,
}, p, mongop.IdCursorType)
if err != nil {
return resp, err
Expand All @@ -128,27 +113,31 @@ func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem
if p.LastToken != nil {
resp.Token = *p.LastToken
}
resp.Total = total
return resp, nil
}

func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error) {
resp = new(gensystem.GetNotificationCountResp)
if resp.Total, err = s.NotificationMongoMapper.Count(ctx, &notificationmapper.FilterOptions{
OnlyUserId: req.OnlyUserId,
OnlyUserId: lo.ToPtr(req.UserId),
OnlyType: req.OnlyType,
OnlyIsRead: req.OnlyIsRead,
OnlyStatus: lo.ToPtr(int64(gensystem.NotificationStatus_NotRead)),
}); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) CreateNotifications(ctx context.Context, req *gensystem.CreateNotificationsReq) (resp *gensystem.CreateNotificationsResp, err error) {
notifications := lo.Map[*gensystem.NotificationInfo, *notificationmapper.Notification](req.Notifications, func(item *gensystem.NotificationInfo, _ int) *notificationmapper.Notification {
return convertor.NotificationInfoToNotificationMapper(item)
})
if err = s.NotificationMongoMapper.InsertMany(ctx, notifications); err != nil {
if err = s.NotificationMongoMapper.InsertOne(ctx, &notificationmapper.Notification{
TargetUserId: req.TargetUserId,
SourceUserId: req.SourceUserId,
SourceContentId: req.SourceContentId,
Type: req.Type,
TargetType: req.TargetType,
Text: req.Text,
Status: int64(gensystem.NotificationStatus_NotRead),
}); err != nil {
return resp, err
}
return resp, nil
Expand Down
1 change: 1 addition & 0 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const (
ImageUrl = "imageUrl"
LinkUrl = "linkUrl"
IsPublic = "isPublic"
Status = "status"
)
14 changes: 1 addition & 13 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ import (
gensystem "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/system"
)

func NotificationInfoToNotificationMapper(in *gensystem.NotificationInfo) *notificationmapper.Notification {
return &notificationmapper.Notification{
TargetUserId: in.TargetUserId,
SourceUserId: in.SourceUserId,
SourceContentId: in.SourceContentId,
Type: in.Type,
TargetType: in.TargetType,
Text: in.Text,
IsRead: in.IsRead,
}
}

func NotificationMapperToNotification(in *notificationmapper.Notification) *gensystem.Notification {
return &gensystem.Notification{
NotificationId: in.ID.Hex(),
Expand All @@ -28,7 +16,7 @@ func NotificationMapperToNotification(in *notificationmapper.Notification) *gens
Type: in.Type,
Text: in.Text,
CreateTime: in.CreateAt.UnixMilli(),
IsRead: in.IsRead,
Status: in.Status,
}
}

Expand Down
24 changes: 5 additions & 19 deletions biz/infrastructure/mapper/notification/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package notification

import (
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts"
"github.com/samber/lo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type FilterOptions struct {
OnlyUserId *string
OnlyType *int64
OnlyIsRead *bool
OnlyNotificationIds []string
OnlyUserId *string
OnlyType *int64
OnlyStatus *int64
}

type MongoFilter struct {
Expand All @@ -30,20 +27,9 @@ func (f *MongoFilter) toBson() bson.M {
f.CheckOnlyUserId()
f.CheckOnlyType()
f.CheckOnlyIsRead()
f.CheckOnlyNotificationIds()
return f.m
}

func (f *MongoFilter) CheckOnlyNotificationIds() {
if f.OnlyNotificationIds != nil {
f.m[consts.ID] = bson.M{"$in": lo.Map[string, primitive.ObjectID](f.OnlyNotificationIds, func(item string, index int) primitive.ObjectID {
oid, _ := primitive.ObjectIDFromHex(item)
return oid
}),
}
}
}

func (f *MongoFilter) CheckOnlyUserId() {
if f.OnlyUserId != nil {
f.m[consts.TargetUserId] = *f.OnlyUserId
Expand All @@ -57,7 +43,7 @@ func (f *MongoFilter) CheckOnlyType() {
}

func (f *MongoFilter) CheckOnlyIsRead() {
if f.OnlyIsRead != nil {
f.m[consts.IsRead] = bson.M{"$exists": *f.OnlyIsRead}
if f.OnlyStatus != nil {
f.m[consts.Status] = *f.OnlyStatus
}
}
79 changes: 24 additions & 55 deletions biz/infrastructure/mapper/notification/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
gensystem "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/system"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/mr"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -25,11 +26,11 @@ var _ INotificationMongoMapper = (*MongoMapper)(nil)

type (
INotificationMongoMapper interface {
GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error)
GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, error)
Count(ctx context.Context, fopts *FilterOptions) (int64, error)
UpdateNotifications(ctx context.Context, fopts *FilterOptions, isRead bool) error
UpdateNotifications(ctx context.Context, fopts *FilterOptions) error
DeleteNotifications(ctx context.Context, fopts *FilterOptions) error
InsertMany(ctx context.Context, data []*Notification) error
InsertOne(ctx context.Context, data *Notification) error
GetNotificationsAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error)
}
Notification struct {
Expand All @@ -42,7 +43,7 @@ type (
Text string `bson:"text,omitempty" json:"text,omitempty"`
CreateAt time.Time `bson:"createAt,omitempty" json:"createAt,omitempty"`
UpdateAt time.Time `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
IsRead bool `bson:"isRead,omitempty" json:"isRead,omitempty"`
Status int64 `bson:"status,omitempty" json:"status,omitempty"`
}
MongoMapper struct {
conn *monc.Model
Expand All @@ -61,99 +62,67 @@ func (m *MongoMapper) GetNotificationsAndCount(ctx context.Context, fopts *Filte
count int64
err1, err2 error
)
p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts)

filter := MakeBsonFilter(fopts)
sort, err := p.MakeSortOptions(ctx, filter)
if err != nil {
return nil, 0, err
}

if err = mr.Finish(func() error {
count, err1 = m.conn.CountDocuments(ctx, filter)
if err := mr.Finish(func() error {
count, err1 = m.Count(ctx, fopts)
if err1 != nil {
return err1
}
return nil
}, func() error {
if err2 = m.conn.Find(ctx, &data, filter, &options.FindOptions{
Sort: sort,
Limit: popts.Limit,
Skip: popts.Offset,
}); err2 != nil {
data, err2 = m.GetNotifications(ctx, fopts, popts, sorter)
if err2 != nil {
return err2
}
// 如果是反向查询,反转数据
if *popts.Backward {
lo.Reverse(data)
}
if len(data) > 0 {
err2 = p.StoreCursor(ctx, data[0], data[len(data)-1])
if err2 != nil {
return err2
}
}
return nil
}); err != nil {
return nil, 0, err
}

return data, count, nil
}
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()
})
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()

_, err := m.conn.InsertMany(ctx, lo.ToAnySlice(datas))
_, err := m.conn.InsertOneNoCache(ctx, data)
return err
}
func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error) {
func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, error) {
var data []*Notification
p := mongop.NewMongoPaginator(pagination.NewRawStore(sorter), popts)

filter := MakeBsonFilter(fopts)
sort, err := p.MakeSortOptions(ctx, filter)
if err != nil {
return nil, 0, err
return nil, err
}

if err = m.conn.Find(ctx, &data, filter, &options.FindOptions{
Sort: sort,
Limit: popts.Limit,
Skip: popts.Offset,
}); err != nil {
return nil, 0, err
return nil, err
}

// 如果是反向查询,反转数据
if *popts.Backward {
for i := 0; i < len(data)/2; i++ {
data[i], data[len(data)-i-1] = data[len(data)-i-1], data[i]
}
lo.Reverse(data)
}
if len(data) > 0 {
err = p.StoreCursor(ctx, data[0], data[len(data)-1])
if err != nil {
return nil, 0, err
return nil, err
}
}

count, err := m.conn.CountDocuments(ctx, filter)
if err != nil {
return nil, 0, err
}

return data, count, nil
return data, nil
}

func (m *MongoMapper) UpdateNotifications(ctx context.Context, fopts *FilterOptions, isRead bool) error {
func (m *MongoMapper) UpdateNotifications(ctx context.Context, fopts *FilterOptions) error {
filter := MakeBsonFilter(fopts)
if _, err := m.conn.UpdateManyNoCache(ctx, filter, bson.M{"$set": bson.M{consts.IsRead: isRead, consts.UpdateAt: time.Now()}}); err != nil {
if _, err := m.conn.UpdateManyNoCache(ctx, filter, bson.M{"$set": bson.M{consts.Status: int64(gensystem.NotificationStatus_Read), consts.UpdateAt: time.Now()}}); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 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-20240207103956-4877398ec51e
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240213134058-123dd3968487
github.com/cloudwego/kitex v0.8.0
github.com/google/wire v0.5.0
github.com/kitex-contrib/obs-opentelemetry v0.2.5
Expand Down
Loading
Loading