components.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package handlers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "git.linuxforward.com/byop/byop-engine/models"
  6. "git.linuxforward.com/byop/byop-engine/services"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // ComponentHandler handles component-related operations
  10. type ComponentHandler struct {
  11. service *services.ComponentService
  12. }
  13. // NewComponentHandler creates a new ComponentHandler
  14. func NewComponentHandler(service *services.ComponentService) *ComponentHandler {
  15. return &ComponentHandler{
  16. service: service,
  17. }
  18. }
  19. // RegisterRoutes registers routes for component operations
  20. func (h *ComponentHandler) RegisterRoutes(r *gin.RouterGroup) {
  21. r.GET("/", h.ListComponents)
  22. r.POST("/", h.CreateComponent)
  23. r.GET("/:id", h.GetComponent)
  24. r.PUT("/:id", h.UpdateComponent)
  25. r.DELETE("/:id", h.DeleteComponent)
  26. r.GET("/:id/deployments", h.GetComponentDeployments)
  27. }
  28. // ListComponents returns all components with optional filtering
  29. func (h *ComponentHandler) ListComponents(c *gin.Context) {
  30. filter := make(map[string]interface{})
  31. // Attempt to bind query parameters, but allow empty filters
  32. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  33. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
  34. return
  35. }
  36. components, err := h.service.ListComponents(filter)
  37. if err != nil {
  38. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list components: %v", err)})
  39. return
  40. }
  41. c.JSON(http.StatusOK, components)
  42. }
  43. // CreateComponent creates a new component
  44. func (h *ComponentHandler) CreateComponent(c *gin.Context) {
  45. var component models.Component
  46. if err := c.ShouldBindJSON(&component); err != nil {
  47. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  48. return
  49. }
  50. // Get the user ID from the context (set by auth middleware)
  51. userID, exists := c.Get("userID")
  52. if exists {
  53. component.CreatedBy = userID.(string)
  54. }
  55. if err := h.service.CreateComponent(&component); err != nil {
  56. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create component: %v", err)})
  57. return
  58. }
  59. c.JSON(http.StatusCreated, component)
  60. }
  61. // GetComponent returns a specific component
  62. func (h *ComponentHandler) GetComponent(c *gin.Context) {
  63. id := c.Param("id")
  64. component, err := h.service.GetComponent(id)
  65. if err != nil {
  66. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch component: %v", err)})
  67. return
  68. }
  69. if component == nil {
  70. c.JSON(http.StatusNotFound, gin.H{"error": "Component not found"})
  71. return
  72. }
  73. c.JSON(http.StatusOK, component)
  74. }
  75. // UpdateComponent updates a component
  76. func (h *ComponentHandler) UpdateComponent(c *gin.Context) {
  77. id := c.Param("id")
  78. var updatedComponent models.Component
  79. if err := c.ShouldBindJSON(&updatedComponent); err != nil {
  80. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  81. return
  82. }
  83. // Ensure the ID matches the URL parameter
  84. updatedComponent.ID = id
  85. if err := h.service.UpdateComponent(&updatedComponent); err != nil {
  86. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update component: %v", err)})
  87. return
  88. }
  89. c.JSON(http.StatusOK, updatedComponent)
  90. }
  91. // DeleteComponent deletes a component
  92. func (h *ComponentHandler) DeleteComponent(c *gin.Context) {
  93. id := c.Param("id")
  94. if err := h.service.DeleteComponent(id); err != nil {
  95. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete component: %v", err)})
  96. return
  97. }
  98. c.Status(http.StatusNoContent)
  99. }
  100. // GetComponentDeployments returns all deployments for a component
  101. func (h *ComponentHandler) GetComponentDeployments(c *gin.Context) {
  102. id := c.Param("id")
  103. deployments, err := h.service.GetComponentDeployments(id)
  104. if err != nil {
  105. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch component deployments: %v", err)})
  106. return
  107. }
  108. c.JSON(http.StatusOK, deployments)
  109. }