feat: 实现用户通知系统,包含通知模型、数据操作、业务逻辑及相关API接口。
This commit is contained in:
47
internal/service/notification_service.go
Normal file
47
internal/service/notification_service.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"accounting-app/internal/models"
|
||||
"accounting-app/internal/repository"
|
||||
)
|
||||
|
||||
type NotificationService struct {
|
||||
repo *repository.NotificationRepository
|
||||
}
|
||||
|
||||
func NewNotificationService(repo *repository.NotificationRepository) *NotificationService {
|
||||
return &NotificationService{repo: repo}
|
||||
}
|
||||
|
||||
func (s *NotificationService) CreateNotification(userID uint, title, content string, notifType models.NotificationType, link string) error {
|
||||
notification := &models.Notification{
|
||||
UserID: userID,
|
||||
Title: title,
|
||||
Content: content,
|
||||
Type: notifType,
|
||||
Link: link,
|
||||
}
|
||||
return s.repo.Create(notification)
|
||||
}
|
||||
|
||||
func (s *NotificationService) GetNotifications(userID uint, page, limit int, isRead *bool) ([]models.Notification, int64, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
return s.repo.FindAll(userID, page, limit, isRead)
|
||||
}
|
||||
|
||||
func (s *NotificationService) GetUnreadCount(userID uint) (int64, error) {
|
||||
return s.repo.CountUnread(userID)
|
||||
}
|
||||
|
||||
func (s *NotificationService) MarkAsRead(id uint, userID uint) error {
|
||||
return s.repo.MarkAsRead(id, userID)
|
||||
}
|
||||
|
||||
func (s *NotificationService) MarkAllAsRead(userID uint) error {
|
||||
return s.repo.MarkAllAsRead(userID)
|
||||
}
|
||||
Reference in New Issue
Block a user