clients.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package handlers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "git.linuxforward.com/byop/byop-engine/dbmanager"
  6. "git.linuxforward.com/byop/byop-engine/dbstore"
  7. "git.linuxforward.com/byop/byop-engine/models"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // ClientHandler handles client-related operations
  11. type ClientHandler struct {
  12. store *dbstore.Store[*models.Client]
  13. }
  14. // NewClientHandler creates a new ClientHandler
  15. func NewClientHandler(db dbmanager.DbManager[*models.Client]) *ClientHandler {
  16. // Initialize the store for client operations
  17. store := dbstore.NewStore(db, "clients")
  18. return &ClientHandler{
  19. store: store,
  20. }
  21. }
  22. // RegisterRoutes registers routes for client operations
  23. func (h *ClientHandler) RegisterRoutes(r *gin.RouterGroup) {
  24. r.GET("/", h.ListClients)
  25. r.POST("/", h.CreateClient)
  26. r.GET("/:id", h.GetClient)
  27. r.PUT("/:id", h.UpdateClient)
  28. r.DELETE("/:id", h.DeleteClient)
  29. r.GET("/:id/deployments", h.GetClientDeployments)
  30. }
  31. // ListClients returns all clients
  32. func (h *ClientHandler) ListClients(c *gin.Context) {
  33. clients, err := h.store.List(nil)
  34. if err != nil {
  35. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch clients: %v", err)})
  36. return
  37. }
  38. c.JSON(http.StatusOK, clients)
  39. }
  40. // CreateClient creates a new client
  41. func (h *ClientHandler) CreateClient(c *gin.Context) {
  42. var client models.Client
  43. if err := c.ShouldBindJSON(&client); err != nil {
  44. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
  45. return
  46. }
  47. // Create a pointer to the client for the repository
  48. clientPtr := &client
  49. if err := h.store.Create(clientPtr); err != nil {
  50. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create client: %v", err)})
  51. return
  52. }
  53. c.JSON(http.StatusCreated, client)
  54. }
  55. // GetClient returns a specific client
  56. func (h *ClientHandler) GetClient(c *gin.Context) {
  57. id := c.Param("id")
  58. client, err := h.store.GetByID(id)
  59. if err != nil {
  60. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch client: %v", err)})
  61. return
  62. }
  63. if client == nil {
  64. c.JSON(http.StatusNotFound, gin.H{"error": "Client not found"})
  65. return
  66. }
  67. c.JSON(http.StatusOK, client)
  68. }
  69. // UpdateClient updates a client
  70. func (h *ClientHandler) UpdateClient(c *gin.Context) {
  71. id := c.Param("id")
  72. // Check if client exists
  73. existingClient, err := h.store.GetByID(id)
  74. if err != nil {
  75. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch client: %v", err)})
  76. return
  77. }
  78. if existingClient == nil {
  79. c.JSON(http.StatusNotFound, gin.H{"error": "Client not found"})
  80. return
  81. }
  82. // Parse updated client data
  83. var updatedClient models.Client
  84. if err := c.ShouldBindJSON(&updatedClient); err != nil {
  85. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
  86. return
  87. }
  88. // Set the ID to ensure it matches the URL parameter
  89. updatedClient.ID = id
  90. clientPtr := &updatedClient
  91. if err := h.store.Update(clientPtr); err != nil {
  92. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update client: %v", err)})
  93. return
  94. }
  95. c.JSON(http.StatusOK, updatedClient)
  96. }
  97. // DeleteClient deletes a client
  98. func (h *ClientHandler) DeleteClient(c *gin.Context) {
  99. id := c.Param("id")
  100. if err := h.store.Delete(id); err != nil {
  101. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete client: %v", err)})
  102. return
  103. }
  104. c.Status(http.StatusNoContent)
  105. }
  106. // GetClientDeployments returns all deployments for a client
  107. func (h *ClientHandler) GetClientDeployments(c *gin.Context) {
  108. id := c.Param("id")
  109. // Check if client exists
  110. client, err := h.store.GetByID(id)
  111. if err != nil {
  112. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch client: %v", err)})
  113. return
  114. }
  115. if client == nil {
  116. c.JSON(http.StatusNotFound, gin.H{"error": "Client not found"})
  117. return
  118. }
  119. // TODO: Retrieve deployments - this likely requires a separate repository or service
  120. // For now, return an empty list
  121. deployments := []models.Deployment{}
  122. c.JSON(http.StatusOK, deployments)
  123. }