feat: 添加通知微服务,支持站内通知已读状态

This commit is contained in:
zetaloop
2026-04-24 12:44:59 +08:00
parent 95f2f10f9f
commit b557bfcc2e
52 changed files with 7035 additions and 30 deletions
@@ -0,0 +1,61 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/notification/rpc/internal/svc"
"juwan-backend/app/notification/rpc/pb"
"juwan-backend/app/snowflake/rpc/snowflake"
"github.com/zeromicro/go-zero/core/logx"
)
type AddNotificationsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddNotificationsLogic {
return &AddNotificationsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *AddNotificationsLogic) AddNotifications(in *pb.AddNotificationsReq) (*pb.AddNotificationsResp, error) {
if in.GetUserId() <= 0 {
return nil, errors.New("userId is required")
}
if in.GetType() == "" {
return nil, errors.New("type is required")
}
if in.GetTitle() == "" {
return nil, errors.New("title is required")
}
if in.GetContent() == "" {
return nil, errors.New("content is required")
}
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
if err != nil {
return nil, errors.New("create notification id failed")
}
created, err := l.svcCtx.NotificationModelRW.Notifications.Create().
SetID(idResp.Id).
SetUserID(in.GetUserId()).
SetType(in.GetType()).
SetTitle(in.GetTitle()).
SetContent(in.GetContent()).
SetNillableLink(in.Link).
Save(l.ctx)
if err != nil {
logx.Errorf("addNotifications err: %v", err)
return nil, errors.New("add notification failed")
}
return &pb.AddNotificationsResp{Id: created.ID}, nil
}
@@ -0,0 +1,34 @@
package logic
import (
"context"
"juwan-backend/app/notification/rpc/internal/svc"
"juwan-backend/app/notification/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type DelNotificationsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDelNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelNotificationsLogic {
return &DelNotificationsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DelNotificationsLogic) DelNotifications(in *pb.DelNotificationsReq) (*pb.DelNotificationsResp, error) {
err := l.svcCtx.NotificationModelRW.Notifications.DeleteOneID(in.GetId()).Exec(l.ctx)
if err != nil {
logx.Errorf("delNotifications err: %v", err)
return nil, err
}
return &pb.DelNotificationsResp{}, nil
}
@@ -0,0 +1,33 @@
package logic
import (
"context"
"juwan-backend/app/notification/rpc/internal/svc"
"juwan-backend/app/notification/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetNotificationsByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetNotificationsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetNotificationsByIdLogic {
return &GetNotificationsByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetNotificationsByIdLogic) GetNotificationsById(in *pb.GetNotificationsByIdReq) (*pb.GetNotificationsByIdResp, error) {
n, err := l.svcCtx.NotificationModelRO.Notifications.Get(l.ctx, in.GetId())
if err != nil {
return nil, err
}
return &pb.GetNotificationsByIdResp{Notifications: entNotificationToPb(n)}, nil
}
@@ -0,0 +1,23 @@
package logic
import (
"juwan-backend/app/notification/rpc/internal/models"
"juwan-backend/app/notification/rpc/pb"
)
func entNotificationToPb(n *models.Notifications) *pb.Notifications {
out := &pb.Notifications{
Id: n.ID,
UserId: n.UserID,
Type: n.Type,
Title: n.Title,
Content: n.Content,
Read: n.Read,
CreatedAt: n.CreatedAt.Unix(),
UpdatedAt: n.UpdatedAt.Unix(),
}
if n.Link != nil {
out.Link = *n.Link
}
return out
}
@@ -0,0 +1,72 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/notification/rpc/internal/models/notifications"
"juwan-backend/app/notification/rpc/internal/svc"
"juwan-backend/app/notification/rpc/pb"
"entgo.io/ent/dialect/sql"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchNotificationsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSearchNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchNotificationsLogic {
return &SearchNotificationsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SearchNotificationsLogic) SearchNotifications(in *pb.SearchNotificationsReq) (*pb.SearchNotificationsResp, error) {
limit := in.GetLimit()
if limit <= 0 {
limit = 20
}
if limit > 100 {
return nil, errors.New("limit too large")
}
offset := in.GetOffset()
if offset < 0 {
offset = 0
}
query := l.svcCtx.NotificationModelRO.Notifications.Query()
if in.Id != nil {
query = query.Where(notifications.IDEQ(in.GetId()))
}
if in.UserId != nil {
query = query.Where(notifications.UserIDEQ(in.GetUserId()))
}
if in.Type != nil {
query = query.Where(notifications.TypeEQ(in.GetType()))
}
if in.Read != nil {
query = query.Where(notifications.ReadEQ(in.GetRead()))
}
list, err := query.
Order(notifications.ByCreatedAt(sql.OrderDesc()), notifications.ByID(sql.OrderDesc())).
Offset(int(offset)).
Limit(int(limit)).
All(l.ctx)
if err != nil {
logx.Errorf("searchNotifications err: %v", err)
return nil, errors.New("search notifications failed")
}
out := make([]*pb.Notifications, len(list))
for i, n := range list {
out[i] = entNotificationToPb(n)
}
return &pb.SearchNotificationsResp{Notifications: out}, nil
}
@@ -0,0 +1,55 @@
package logic
import (
"context"
"time"
"juwan-backend/app/notification/rpc/internal/svc"
"juwan-backend/app/notification/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateNotificationsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateNotificationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateNotificationsLogic {
return &UpdateNotificationsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateNotificationsLogic) UpdateNotifications(in *pb.UpdateNotificationsReq) (*pb.UpdateNotificationsResp, error) {
updater := l.svcCtx.NotificationModelRW.Notifications.UpdateOneID(in.GetId())
if in.Read != nil {
updater = updater.SetRead(in.GetRead())
}
if in.Type != nil {
updater = updater.SetType(in.GetType())
}
if in.Title != nil {
updater = updater.SetTitle(in.GetTitle())
}
if in.Content != nil {
updater = updater.SetContent(in.GetContent())
}
if in.Link != nil {
updater = updater.SetLink(in.GetLink())
}
if in.UpdatedAt != nil {
updater = updater.SetUpdatedAt(time.Unix(in.GetUpdatedAt(), 0))
}
_, err := updater.Save(l.ctx)
if err != nil {
logx.Errorf("updateNotifications err: %v", err)
return nil, err
}
return &pb.UpdateNotificationsResp{}, nil
}