feat: 实现用户通知系统,包含通知模型、数据操作、业务逻辑及相关API接口。

This commit is contained in:
2026-01-28 08:08:14 +08:00
parent f1a390a1ef
commit 8e9c0e9024
5 changed files with 325 additions and 0 deletions

View 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)
}