veza/veza-backend-api/internal/handlers/announcement_handler.go

79 lines
2.2 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"veza-backend-api/internal/services"
)
// AnnouncementHandler handles announcement endpoints (v0.803 ADM1-04)
type AnnouncementHandler struct {
svc *services.AnnouncementService
}
// NewAnnouncementHandler creates a new AnnouncementHandler
func NewAnnouncementHandler(svc *services.AnnouncementService) *AnnouncementHandler {
return &AnnouncementHandler{svc: svc}
}
// List returns all announcements (admin)
func (h *AnnouncementHandler) List(c *gin.Context) {
list, err := h.svc.List(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list announcements"})
return
}
c.JSON(http.StatusOK, gin.H{"announcements": list})
}
// Create creates an announcement (admin)
func (h *AnnouncementHandler) Create(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok || userID == uuid.Nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"})
return
}
var req services.CreateAnnouncementRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "title and content are required"})
return
}
a, err := h.svc.Create(c.Request.Context(), userID, req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create announcement"})
return
}
c.JSON(http.StatusCreated, a)
}
// Delete deletes an announcement (admin)
func (h *AnnouncementHandler) Delete(c *gin.Context) {
idStr := c.Param("id")
id, err := uuid.Parse(idStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid announcement ID"})
return
}
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Announcement deleted"})
}
// GetActive returns active announcements (public)
func (h *AnnouncementHandler) GetActive(c *gin.Context) {
list, err := h.svc.GetActive(c.Request.Context())
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get announcements"})
return
}
c.JSON(http.StatusOK, gin.H{"announcements": list})
}