Files
tm_back/internal/notification/handler.go
T
n.nakhostin a06dc5097e Integrate Notification Management into Tender Management System
- Added a new notification domain, including entities, services, handlers, and repositories to manage notifications effectively.
- Implemented CRUD operations for notifications, allowing for creation, retrieval, updating, and deletion of notifications via the API.
- Enhanced API documentation with Swagger comments for new notification endpoints, ensuring clarity for API consumers.
- Updated routes to include notification management, improving administrative capabilities within the web panel.
- Introduced validation for notification requests and responses, ensuring data integrity and proper error handling.
- Updated the go.mod file to include the latest version of the go-redis library, ensuring compatibility with the notification service.
2025-09-17 16:01:49 +03:30

189 lines
6.4 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 NotificationService
logger logger.Logger
}
// NewNotificationHandler creates a new notification handler
func NewHandler(service NotificationService, 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) Create(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())
}
// Create notification
resp, err := h.service.Create(c.Request().Context(), &req)
if err != nil {
return response.InternalServerError(c, "Failed to create notification")
}
return response.Created(c, resp, "Notification created successfully")
}
// UpdateNotification updates a notification
// @Summary Update notification
// @Description Update an existing notification
// @Tags Admin-Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Param notification body NotificationRequest true "Updated notification information"
// @Success 200 {object} response.APIResponse "Notification updated successfully"
// @Failure 400 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/notifications/{id} [put]
func (h *NotificationHandler) UpdateNotification(c echo.Context) error {
id := c.Param("id")
if id == "" {
return response.BadRequest(c, "Notification ID is required", "")
}
var req NotificationRequest
if err := c.Bind(&req); err != nil {
return response.BadRequest(c, "Invalid request format", err.Error())
}
// Update notification
_, err := h.service.Update(c.Request().Context(), id, &req)
if err != nil {
if err.Error() == "notification not found" {
return response.NotFound(c, "Notification not found")
}
return response.BadRequest(c, "Failed to update notification", err.Error())
}
return response.Success(c, nil, "Notification updated successfully")
}
// GetNotifications retrieves notifications for a user
// @Summary Get user notifications
// @Description Retrieve notifications for a specific user with filtering and pagination
// @Tags Admin-Notification
// @Accept json
// @Produce json
// @Param recipient query string true "Recipient"
// @Param type query string false "Notification type (info, warning, alert, reject)"
// @Param priority query string false "Notification priority (low, medium, important)"
// @Param status query string false "Delivery status (pending, sent, failed)"
// @Param is_read query bool false "Read status"
// @Param page query int false "Page number" default(1)
// @Param limit query int false "Items per page" default(20)
// @Param sort_by query string false "Sort field" default(created_at)
// @Param sort_order query string false "Sort order (asc, desc)" default(desc)
// @Success 200 {object} response.APIResponse{data=NotificationsResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/notifications [get]
func (h *NotificationHandler) Search(c echo.Context) error {
req, err := response.Parse[SearchForm](c)
if err != nil {
return response.ValidationError(c, "Invalid request data", err.Error())
}
pagination := response.NewPagination(c)
// Validate request
if valid, err := govalidator.ValidateStruct(req); !valid {
return response.ValidationError(c, "Validation failed", err.Error())
}
// Get notifications
resp, err := h.service.Search(c.Request().Context(), req, pagination)
if err != nil {
return response.InternalServerError(c, "Failed to get notifications")
}
return response.Success(c, resp, "Notifications retrieved successfully")
}
// Get retrieves a notification by ID
// @Summary Get notification by ID
// @Description Retrieve a specific notification by its ID
// @Tags Admin-Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Success 200 {object} response.APIResponse{data=NotificationResponse}
// @Failure 400 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/notifications/{id} [get]
func (h *NotificationHandler) Get(c echo.Context) error {
id := c.Param("id")
if id == "" {
return response.BadRequest(c, "Notification ID is required", "")
}
// Get notification
resp, err := h.service.Get(c.Request().Context(), id)
if err != nil {
if err.Error() == "notification not found" {
return response.NotFound(c, "Notification not found")
}
return response.InternalServerError(c, "Failed to get notification")
}
return response.Success(c, resp, "Notification retrieved successfully")
}
// DeleteNotification deletes a notification
// @Summary Delete notification
// @Description Delete a specific notification
// @Tags Admin-Notification
// @Accept json
// @Produce json
// @Param id path string true "Notification ID"
// @Success 200 {object} response.APIResponse "Notification deleted successfully"
// @Failure 400 {object} response.APIResponse
// @Failure 404 {object} response.APIResponse
// @Failure 500 {object} response.APIResponse
// @Router /admin/v1/notifications/{id} [delete]
func (h *NotificationHandler) DeleteNotification(c echo.Context) error {
id := c.Param("id")
if id == "" {
return response.BadRequest(c, "Notification ID is required", "")
}
// Delete notification
err := h.service.Delete(c.Request().Context(), id)
if err != nil {
return response.InternalServerError(c, "Failed to delete notification")
}
return response.Success(c, nil, "Notification deleted successfully")
}