feat: 添加通知管理功能,包括服务层逻辑和HTTP API处理。

This commit is contained in:
2026-01-28 09:05:07 +08:00
parent e29c808451
commit a18564c2d8
2 changed files with 26 additions and 5 deletions

View File

@@ -185,10 +185,31 @@ func (h *NotificationHandler) DeleteNotification(c *gin.Context) {
api.NoContent(c) api.NoContent(c)
} }
// CreateNotification handles POST /api/v1/notifications
// Creates a new notification (Admin/System use)
func (h *NotificationHandler) CreateNotification(c *gin.Context) {
// TODO: Add admin authorization check
var input service.CreateNotificationInput
if err := c.ShouldBindJSON(&input); err != nil {
api.BadRequest(c, "Invalid request body: "+err.Error())
return
}
notification, err := h.notificationService.CreateNotification(input)
if err != nil {
api.InternalError(c, "Failed to create notification: "+err.Error())
return
}
api.Created(c, notification)
}
// RegisterRoutes registers all notification routes to the given router group // RegisterRoutes registers all notification routes to the given router group
func (h *NotificationHandler) RegisterRoutes(rg *gin.RouterGroup) { func (h *NotificationHandler) RegisterRoutes(rg *gin.RouterGroup) {
notifications := rg.Group("/notifications") notifications := rg.Group("/notifications")
{ {
notifications.POST("", h.CreateNotification)
notifications.GET("", h.GetNotifications) notifications.GET("", h.GetNotifications)
notifications.GET("/unread-count", h.GetUnreadCount) notifications.GET("/unread-count", h.GetUnreadCount)
notifications.PUT("/:id/read", h.MarkAsRead) notifications.PUT("/:id/read", h.MarkAsRead)

View File

@@ -25,11 +25,11 @@ func NewNotificationService(repo *repository.NotificationRepository) *Notificati
// CreateNotificationInput represents input for creating a notification // CreateNotificationInput represents input for creating a notification
type CreateNotificationInput struct { type CreateNotificationInput struct {
UserID uint UserID uint `json:"user_id" binding:"required"`
Type models.NotificationType Type models.NotificationType `json:"type" binding:"required"`
Title string Title string `json:"title" binding:"required"`
Content string Content string `json:"content" binding:"required"`
RelatedID *uint RelatedID *uint `json:"related_id"`
} }
// CreateNotification creates a new notification // CreateNotification creates a new notification