-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
131 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.