Skip to content

Commit

Permalink
fix: 修改通知功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx committed Mar 16, 2024
1 parent 13980b3 commit 0ce7c26
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 38 deletions.
4 changes: 4 additions & 0 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ type SystemServerImpl struct {
SystemService service.SystemService
}

func (s *SystemServerImpl) CreateNotificationCount(ctx context.Context, req *system.CreateNotificationCountReq) (res *system.CreateNotificationCountResp, err error) {
return s.SystemService.CreateNotificationCount(ctx, req)
}

// func (s *SystemServerImpl) UpdateNotifications(ctx context.Context, req *system.UpdateNotificationsReq) (resp *system.UpdateNotificationsResp, err error) {
// return s.SystemService.UpdateNotifications(ctx, req)
// }
Expand Down
42 changes: 35 additions & 7 deletions biz/application/service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package service

import (
"context"
"fmt"
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts"
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/convertor"
notificationmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notification"
notificationcountmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notificationCount"
slidermapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
Expand All @@ -23,12 +25,23 @@ 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)
CreateNotificationCount(ctx context.Context, req *gensystem.CreateNotificationCountReq) (resp *gensystem.CreateNotificationCountResp, err error)
}

type SystemServiceImpl struct {
NotificationMongoMapper notificationmapper.INotificationMongoMapper
SliderMongoMapper slidermapper.ISliderMongoMapper
Redis *redis.Redis
NotificationMongoMapper notificationmapper.INotificationMongoMapper
NotificationCountMongoMapper notificationcountmapper.INotificationCountMongoMapper
SliderMongoMapper slidermapper.ISliderMongoMapper
Redis *redis.Redis
}

func (s *SystemServiceImpl) CreateNotificationCount(ctx context.Context, req *gensystem.CreateNotificationCountReq) (resp *gensystem.CreateNotificationCountResp, err error) {
uid, _ := primitive.ObjectIDFromHex(req.UserId)
err = s.NotificationCountMongoMapper.CreateNotificationCount(ctx, &notificationcountmapper.NotificationCount{
ID: uid,
Read: 0,
})
return resp, err
}

func (s *SystemServiceImpl) DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error) {
Expand Down Expand Up @@ -90,9 +103,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, err := s.NotificationMongoMapper.GetNotifications(ctx, &notificationmapper.FilterOptions{
OnlyUserId: lo.ToPtr(req.UserId),
OnlyType: req.OnlyType,
notifications, cnt, err := s.NotificationMongoMapper.GetNotificationsAndCount(ctx, &notificationmapper.FilterOptions{
OnlyUserIds: []string{req.UserId, consts.NotificationSystemKey},
OnlyType: req.OnlyType,
}, p, mongop.IdCursorType)
if err != nil {
return resp, err
Expand All @@ -104,6 +117,15 @@ func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem
if p.LastToken != nil {
resp.Token = *p.LastToken
}

uid, _ := primitive.ObjectIDFromHex(req.UserId)
if err = s.NotificationCountMongoMapper.UpdateNotificationCount(ctx, &notificationcountmapper.NotificationCount{
ID: uid,
Read: cnt,
}); err != nil {
return resp, err
}

return resp, nil
}

Expand All @@ -115,8 +137,14 @@ func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensy
return resp, err
}

read, err := s.NotificationCountMongoMapper.GetNotificationCount(ctx, req.UserId)
if err != nil {
fmt.Println(err)

return resp, err
}
return &gensystem.GetNotificationCountResp{
Total: cnt,
Total: cnt - read,
}, nil
}

Expand Down
26 changes: 12 additions & 14 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package consts

const (
ID = "_id"
CreateAt = "createAt"
TargetUserId = "targetUserId"
IsRead = "isRead"
UpdateAt = "updateAt"
Type = "type"
TargetType = "targetType"
ImageUrl = "imageUrl"
Sum = "sum"
Read = "read"
IsPublic = "isPublic"
Status = "status"
//NotificationReadCountKey = "NotificationReadCount"
NotificationCount = "NotificationCount"
ID = "_id"
CreateAt = "createAt"
TargetUserId = "targetUserId"
IsRead = "isRead"
UpdateAt = "updateAt"
Type = "type"
TargetType = "targetType"
ImageUrl = "imageUrl"
Sum = "sum"
Read = "read"
IsPublic = "isPublic"
Status = "status"
NotificationSystemKey = "system"
//NotificationAll = "all"
)
3 changes: 2 additions & 1 deletion biz/infrastructure/mapper/notification/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func MakeBsonFilter(options *FilterOptions) bson.M {
func (f *MongoFilter) toBson() bson.M {
f.CheckOnlyUserId()
f.CheckOnlyType()
f.CheckOnlyUserIds()
return f.m
}

Expand All @@ -44,7 +45,7 @@ func (f *MongoFilter) CheckOnlyType() {
func (f *MongoFilter) CheckOnlyUserIds() {
if f.OnlyUserIds != nil {
f.m[consts.TargetUserId] = bson.M{
"in": f.OnlyUserIds,
"$in": f.OnlyUserIds,
}
}
}
12 changes: 0 additions & 12 deletions biz/infrastructure/mapper/notification/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ 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"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"time"

"github.com/zeromicro/go-zero/core/stores/monc"

"github.com/CloudStriver/cloudmind-system/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts"
)

const (
Expand All @@ -28,7 +25,6 @@ type (
INotificationMongoMapper interface {
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) error
DeleteNotifications(ctx context.Context, fopts *FilterOptions) error
InsertOne(ctx context.Context, data *Notification) error
GetNotificationsAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Notification, int64, error)
Expand Down Expand Up @@ -119,14 +115,6 @@ func (m *MongoMapper) GetNotifications(ctx context.Context, fopts *FilterOptions
return data, nil
}

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.Status: int64(gensystem.NotificationStatus_Read), consts.UpdateAt: time.Now()}}); err != nil {
return err
}
return nil
}

func (m *MongoMapper) Count(ctx context.Context, fopts *FilterOptions) (int64, error) {
f := MakeBsonFilter(fopts)
return m.conn.CountDocuments(ctx, f)
Expand Down
68 changes: 68 additions & 0 deletions biz/infrastructure/mapper/notificationCount/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package notification

import (
"context"
"errors"
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts"
"github.com/zeromicro/go-zero/core/stores/monc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"

"github.com/CloudStriver/cloudmind-system/biz/infrastructure/config"
)

const (
CollectionName = "notificationCount"
NotificationCountKey = "cache:NotificationCount:"
)

var _ INotificationCountMongoMapper = (*MongoMapper)(nil)

type (
INotificationCountMongoMapper interface {
GetNotificationCount(ctx context.Context, userId string) (int64, error)
UpdateNotificationCount(ctx context.Context, data *NotificationCount) error
CreateNotificationCount(ctx context.Context, data *NotificationCount) error
}
NotificationCount struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
Read int64 `bson:"read,omitempty" json:"read,omitempty"`
}
MongoMapper struct {
conn *monc.Model
}
)

func (m MongoMapper) CreateNotificationCount(ctx context.Context, data *NotificationCount) error {
key := NotificationCountKey + data.ID.Hex()
_, err := m.conn.InsertOne(ctx, key, data)
return err
}

func (m MongoMapper) GetNotificationCount(ctx context.Context, userId string) (int64, error) {
key := NotificationCountKey + userId
var data *NotificationCount
uid, _ := primitive.ObjectIDFromHex(userId)
err := m.conn.FindOne(ctx, key, &data, bson.M{"_id": uid})
switch {
case errors.Is(err, monc.ErrNotFound):
return 0, consts.ErrNotFound
case err == nil:
return data.Read, err
default:
return 0, err
}
}

func (m MongoMapper) UpdateNotificationCount(ctx context.Context, data *NotificationCount) error {
key := NotificationCountKey + data.ID.Hex()
_, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: data.ID}, bson.M{"$set": data})
return err
}

func NewNotificationCountModel(config *config.Config) INotificationCountMongoMapper {
conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.CacheConf)
return &MongoMapper{
conn: conn,
}
}
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-20240312020955-691e500d8edb
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240316123600-5ec7ab682e84
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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020023-d19062f3547f h1
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020023-d19062f3547f/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020955-691e500d8edb h1:YRJbAIyNLuWVDqCa9/4z4Qf5lfEA413o4B66DgHaIA4=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240312020955-691e500d8edb/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240316123600-5ec7ab682e84/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
2 changes: 2 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
notificationcountmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notificationCount"
slidermapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider"
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/store/redis"
"github.com/google/wire"
Expand All @@ -27,5 +28,6 @@ var InfrastructureSet = wire.NewSet(

var MapperSet = wire.NewSet(
notificationmapper.NewNotificationModel,
notificationcountmapper.NewNotificationCountModel,
slidermapper.NewSliderModel,
)
9 changes: 6 additions & 3 deletions provider/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0ce7c26

Please sign in to comment.