clients.go 3.4 KB

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