feat: 新增路由配置、通知和公告功能,并初始化相关服务和仓库。
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"accounting-app/internal/models"
|
||||
"accounting-app/internal/repository"
|
||||
"accounting-app/internal/service"
|
||||
"accounting-app/pkg/api"
|
||||
|
||||
@@ -14,15 +15,72 @@ import (
|
||||
// NotificationHandler handles HTTP requests for notification operations
|
||||
type NotificationHandler struct {
|
||||
notificationService *service.NotificationService
|
||||
userRepo *repository.UserRepository
|
||||
announcementRepo *repository.AnnouncementRepository
|
||||
}
|
||||
|
||||
// NewNotificationHandler creates a new NotificationHandler instance
|
||||
func NewNotificationHandler(notificationService *service.NotificationService) *NotificationHandler {
|
||||
func NewNotificationHandler(
|
||||
notificationService *service.NotificationService,
|
||||
userRepo *repository.UserRepository,
|
||||
announcementRepo *repository.AnnouncementRepository,
|
||||
) *NotificationHandler {
|
||||
return &NotificationHandler{
|
||||
notificationService: notificationService,
|
||||
userRepo: userRepo,
|
||||
announcementRepo: announcementRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastNotification handles POST /api/v1/notifications/broadcast
|
||||
// Sends a notification to all users and records it in history
|
||||
func (h *NotificationHandler) BroadcastNotification(c *gin.Context) {
|
||||
// TODO: Add proper admin authorization check here when RBAC is implemented
|
||||
// For now, checks if user is authenticated at least
|
||||
adminID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
api.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
var input service.BroadcastInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
api.BadRequest(c, "Invalid request body: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Set admin ID from context
|
||||
input.AdminID = adminID.(uint)
|
||||
|
||||
err := h.notificationService.Broadcast(input, h.userRepo, h.announcementRepo)
|
||||
if err != nil {
|
||||
api.InternalError(c, "Failed to broadcast notification: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, gin.H{"message": "Broadcast sent successfully"})
|
||||
}
|
||||
|
||||
// GetAnnouncementHistory handles GET /api/v1/notifications/announcements
|
||||
// Returns the history of announcements
|
||||
func (h *NotificationHandler) GetAnnouncementHistory(c *gin.Context) {
|
||||
// TODO: Add admin check
|
||||
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
||||
offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0"))
|
||||
|
||||
result, err := h.notificationService.GetAnnouncementHistory(limit, offset, h.announcementRepo)
|
||||
if err != nil {
|
||||
api.InternalError(c, "Failed to get announcement history: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
api.Success(c, gin.H{
|
||||
"announcements": result.Announcements,
|
||||
"total": result.Total,
|
||||
})
|
||||
}
|
||||
|
||||
// GetNotifications handles GET /api/v1/notifications
|
||||
// Returns a list of notifications with pagination and filtering
|
||||
func (h *NotificationHandler) GetNotifications(c *gin.Context) {
|
||||
@@ -222,5 +280,7 @@ func (h *NotificationHandler) RegisterAdminRoutes(rg *gin.RouterGroup) {
|
||||
notifications := rg.Group("/notifications")
|
||||
{
|
||||
notifications.POST("", h.CreateNotification)
|
||||
notifications.POST("/broadcast", h.BroadcastNotification)
|
||||
notifications.GET("/announcements", h.GetAnnouncementHistory)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user