clients.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // ClientHandler handles client-related operations
  11. type ClientHandler struct {
  12. service *services.ClientService
  13. }
  14. // NewClientHandler creates a new ClientHandler
  15. func NewClientHandler(service *services.ClientService) *ClientHandler {
  16. return &ClientHandler{
  17. service: service,
  18. }
  19. }
  20. // ListClients returns all clients
  21. func (h *ClientHandler) ListClients(c *gin.Context) {
  22. filter := make(map[string]interface{})
  23. // Attempt to bind query parameters, but allow empty filters
  24. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  25. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
  26. return
  27. }
  28. clients, err := h.service.ListClients(nil)
  29. if err != nil {
  30. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch clients: %v", err)})
  31. return
  32. }
  33. c.JSON(http.StatusOK, clients)
  34. }
  35. // CreateClient creates a new client
  36. func (h *ClientHandler) CreateClient(c *gin.Context) {
  37. var client models.Client
  38. if err := c.ShouldBindJSON(&client); err != nil {
  39. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
  40. return
  41. }
  42. if err := h.service.CreateClient(&client); err != nil {
  43. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create client: %v", err)})
  44. return
  45. }
  46. c.JSON(http.StatusCreated, client)
  47. }
  48. // GetClient returns a specific client
  49. func (h *ClientHandler) GetClient(c *gin.Context) {
  50. idStr := c.Param("id")
  51. id, err := strconv.ParseInt(idStr, 10, 64)
  52. if err != nil {
  53. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid client ID"})
  54. return
  55. }
  56. client, err := h.service.GetClient(id)
  57. if err != nil {
  58. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch client: %v", err)})
  59. return
  60. }
  61. if client == nil {
  62. c.JSON(http.StatusNotFound, gin.H{"error": "Client not found"})
  63. return
  64. }
  65. c.JSON(http.StatusOK, client)
  66. }
  67. // UpdateClient updates a client
  68. func (h *ClientHandler) UpdateClient(c *gin.Context) {
  69. idStr := c.Param("id")
  70. id, err := strconv.ParseInt(idStr, 10, 64)
  71. if err != nil {
  72. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid client ID"})
  73. return
  74. }
  75. // Parse updated client data
  76. var updatedClient models.Client
  77. if err := c.ShouldBindJSON(&updatedClient); err != nil {
  78. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
  79. return
  80. }
  81. // Set the ID to ensure it matches the URL parameter
  82. updatedClient.ID = id
  83. if err := h.service.UpdateClient(&updatedClient); err != nil {
  84. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update client: %v", err)})
  85. return
  86. }
  87. c.JSON(http.StatusOK, updatedClient)
  88. }
  89. // DeleteClient deletes a client
  90. func (h *ClientHandler) DeleteClient(c *gin.Context) {
  91. idStr := c.Param("id")
  92. id, err := strconv.ParseInt(idStr, 10, 64)
  93. if err != nil {
  94. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid client ID"})
  95. return
  96. }
  97. if err := h.service.DeleteClient(id); err != nil {
  98. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete client: %v", err)})
  99. return
  100. }
  101. c.Status(http.StatusNoContent)
  102. }
  103. // GetClientDeployments returns all deployments for a client
  104. func (h *ClientHandler) GetClientDeployments(c *gin.Context) {
  105. idStr := c.Param("id")
  106. id, err := strconv.ParseInt(idStr, 10, 64)
  107. if err != nil {
  108. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid client ID"})
  109. return
  110. }
  111. // Check if client exists
  112. client, err := h.service.GetClient(id)
  113. if err != nil {
  114. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch client: %v", err)})
  115. return
  116. }
  117. if client == nil {
  118. c.JSON(http.StatusNotFound, gin.H{"error": "Client not found"})
  119. return
  120. }
  121. // TODO: Retrieve deployments - this likely requires a separate repository or service
  122. // For now, return an empty list
  123. deployments := []models.Deployment{}
  124. c.JSON(http.StatusOK, deployments)
  125. }