feat: Implement notification service and integrate with user interactions
- Added notification service to handle sending and listing notifications. - Integrated notification sending on user follow and order payment events. - Updated user service to include fetching followed tenants. - Enhanced content service to manage access control for content assets. - Implemented logic for listing content topics based on genre. - Updated creator service to manage content updates and pricing. - Improved order service to include detailed order information and notifications. - Added tests for notification CRUD operations and order details.
This commit is contained in:
91
backend/app/services/notification.go
Normal file
91
backend/app/services/notification.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
user_dto "quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type notification struct{}
|
||||
|
||||
func (s *notification) List(ctx context.Context, page int, typeArg string) (*requests.Pager, error) {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
|
||||
tbl, q := models.NotificationQuery.QueryContext(ctx)
|
||||
q = q.Where(tbl.UserID.Eq(uid))
|
||||
|
||||
if typeArg != "" && typeArg != "all" {
|
||||
q = q.Where(tbl.Type.Eq(typeArg))
|
||||
}
|
||||
|
||||
q = q.Order(tbl.CreatedAt.Desc())
|
||||
|
||||
p := requests.Pagination{Page: int64(page), Limit: 20}
|
||||
total, err := q.Count()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
list, err := q.Offset(int(p.Offset())).Limit(int(p.Limit)).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
data := make([]user_dto.Notification, len(list))
|
||||
for i, v := range list {
|
||||
data[i] = user_dto.Notification{
|
||||
ID: cast.ToString(v.ID),
|
||||
Type: v.Type,
|
||||
Title: v.Title,
|
||||
Content: v.Content,
|
||||
Read: v.IsRead,
|
||||
Time: v.CreatedAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: requests.Pagination{Page: p.Page, Limit: p.Limit},
|
||||
Total: total,
|
||||
Items: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *notification) MarkRead(ctx context.Context, id string) error {
|
||||
userID := ctx.Value(consts.CtxKeyUser)
|
||||
if userID == nil {
|
||||
return errorx.ErrUnauthorized
|
||||
}
|
||||
uid := cast.ToInt64(userID)
|
||||
nid := cast.ToInt64(id)
|
||||
|
||||
_, err := models.NotificationQuery.WithContext(ctx).
|
||||
Where(models.NotificationQuery.ID.Eq(nid), models.NotificationQuery.UserID.Eq(uid)).
|
||||
UpdateSimple(models.NotificationQuery.IsRead.Value(true))
|
||||
if err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *notification) Send(ctx context.Context, userID int64, typ string, title, content string) error {
|
||||
n := &models.Notification{
|
||||
UserID: userID,
|
||||
Type: typ,
|
||||
Title: title,
|
||||
Content: content,
|
||||
IsRead: false,
|
||||
}
|
||||
return models.NotificationQuery.WithContext(ctx).Create(n)
|
||||
}
|
||||
Reference in New Issue
Block a user