ab6eb3b3ed
- Updated the Send method in NotificationHandler to send notifications asynchronously using goroutines, improving responsiveness and performance. - Removed synchronous error handling for notification sending, allowing the API to return a success response immediately while processing the notification in the background.
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package notification
|
|
|
|
import (
|
|
"github.com/asaskevich/govalidator"
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"tm/pkg/logger"
|
|
"tm/pkg/response"
|
|
)
|
|
|
|
// NotificationHandler handles HTTP requests for notification operations
|
|
type NotificationHandler struct {
|
|
service Service
|
|
logger logger.Logger
|
|
}
|
|
|
|
// NewNotificationHandler creates a new notification handler
|
|
func NewHandler(service Service, logger logger.Logger) *NotificationHandler {
|
|
return &NotificationHandler{
|
|
service: service,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// CreateNotification creates a new notification
|
|
// @Summary Create a new notification
|
|
// @Description Create a new notification with the provided information
|
|
// @Tags Admin-Notification
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param notification body NotificationRequest true "Notification information"
|
|
// @Success 201 {object} response.APIResponse "Notification created successfully"
|
|
// @Failure 400 {object} response.APIResponse
|
|
// @Failure 500 {object} response.APIResponse
|
|
// @Router /admin/v1/notifications [post]
|
|
func (h *NotificationHandler) Send(c echo.Context) error {
|
|
var req NotificationRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return response.BadRequest(c, "Invalid request format", err.Error())
|
|
}
|
|
|
|
// Validate request
|
|
if valid, err := govalidator.ValidateStruct(req); !valid {
|
|
return response.ValidationError(c, "Validation failed", err.Error())
|
|
}
|
|
|
|
// Send notification
|
|
go h.service.Send(c.Request().Context(), &req)
|
|
|
|
return response.Created(c, nil, "Notification sent successfully")
|
|
}
|