components.go 4.5 KB

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