clients.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package handlers
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  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 and contains integrated service logic
  11. type ClientHandler struct {
  12. store *dbstore.SQLiteStore
  13. }
  14. // NewClientHandler creates a new ClientHandler
  15. func NewClientHandler(store *dbstore.SQLiteStore) *ClientHandler {
  16. return &ClientHandler{
  17. store: store,
  18. }
  19. }
  20. // ListClients returns all clients
  21. func (h *ClientHandler) ListClients(c *gin.Context) {
  22. filter := make(map[string]interface{})
  23. ctx := c.Request.Context()
  24. // Attempt to bind query parameters, but allow empty filters
  25. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  26. appErr := models.NewErrValidation("invalid_query_params", nil, err)
  27. models.RespondWithError(c, appErr)
  28. return
  29. }
  30. clients, err := h.store.GetAllClients(ctx)
  31. if err != nil {
  32. appErr := models.NewErrInternalServer("failed_fetch_clients", fmt.Errorf("Failed to fetch clients: %w", err))
  33. models.RespondWithError(c, appErr)
  34. return
  35. }
  36. c.JSON(http.StatusOK, clients)
  37. }
  38. // CreateClient creates a new client
  39. func (h *ClientHandler) CreateClient(c *gin.Context) {
  40. var client models.Client
  41. ctx := c.Request.Context()
  42. if err := c.ShouldBindJSON(&client); err != nil {
  43. appErr := models.NewErrValidation("invalid_request_body", nil, err)
  44. models.RespondWithError(c, appErr)
  45. return
  46. }
  47. // Validate client data
  48. if client.Name == "" {
  49. validationErrors := map[string]string{"name": "Client name is required"}
  50. appErr := models.NewErrValidation("client_name_required", validationErrors, nil)
  51. models.RespondWithError(c, appErr)
  52. return
  53. }
  54. id, err := h.store.CreateClient(ctx, client)
  55. if err != nil {
  56. appErr := models.NewErrInternalServer("failed_create_client", fmt.Errorf("Failed to create client: %w", err))
  57. models.RespondWithError(c, appErr)
  58. return
  59. }
  60. // Set the generated ID
  61. client.ID = id
  62. c.JSON(http.StatusCreated, client)
  63. }
  64. // GetClient returns a specific client
  65. func (h *ClientHandler) GetClient(c *gin.Context) {
  66. idStr := c.Param("id")
  67. ctx := c.Request.Context()
  68. id, err := strconv.ParseInt(idStr, 10, 64)
  69. if err != nil {
  70. appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
  71. models.RespondWithError(c, appErr)
  72. return
  73. }
  74. client, err := h.store.GetClientByID(ctx, int(id))
  75. if err != nil {
  76. models.RespondWithError(c, err)
  77. return
  78. }
  79. if client.ID == 0 {
  80. appErr := models.NewErrNotFound("client_not_found", fmt.Errorf("Client with ID %d not found", id))
  81. models.RespondWithError(c, appErr)
  82. return
  83. }
  84. c.JSON(http.StatusOK, client)
  85. }
  86. // UpdateClient updates a client
  87. func (h *ClientHandler) UpdateClient(c *gin.Context) {
  88. idStr := c.Param("id")
  89. ctx := c.Request.Context()
  90. id, err := strconv.ParseInt(idStr, 10, 64)
  91. if err != nil {
  92. appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
  93. models.RespondWithError(c, appErr)
  94. return
  95. }
  96. var updatedClient models.Client
  97. if err := c.ShouldBindJSON(&updatedClient); err != nil {
  98. appErr := models.NewErrValidation("invalid_request_body", nil, err)
  99. models.RespondWithError(c, appErr)
  100. return
  101. }
  102. updatedClient.ID = int(id)
  103. // Validate client data
  104. if updatedClient.Name == "" {
  105. validationErrors := map[string]string{"name": "Client name is required"}
  106. appErr := models.NewErrValidation("client_name_required", validationErrors, nil)
  107. models.RespondWithError(c, appErr)
  108. return
  109. }
  110. if err := h.store.UpdateClient(ctx, updatedClient); err != nil {
  111. models.RespondWithError(c, err)
  112. return
  113. }
  114. c.JSON(http.StatusOK, updatedClient)
  115. }
  116. // DeleteClient deletes a client
  117. func (h *ClientHandler) DeleteClient(c *gin.Context) {
  118. idStr := c.Param("id")
  119. ctx := c.Request.Context()
  120. id, err := strconv.ParseInt(idStr, 10, 64)
  121. if err != nil {
  122. appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
  123. models.RespondWithError(c, appErr)
  124. return
  125. }
  126. if err := h.store.DeleteClient(ctx, int(id)); err != nil {
  127. models.RespondWithError(c, err)
  128. return
  129. }
  130. c.Status(http.StatusNoContent)
  131. }
  132. // GetClientDeployments returns all deployments for a client
  133. func (h *ClientHandler) GetClientDeployments(c *gin.Context) {
  134. idStr := c.Param("id")
  135. ctx := c.Request.Context()
  136. id, err := strconv.ParseInt(idStr, 10, 64)
  137. if err != nil {
  138. appErr := models.NewErrValidation("invalid_client_id_format", map[string]string{"id": "Invalid client ID format"}, err)
  139. models.RespondWithError(c, appErr)
  140. return
  141. }
  142. client, err := h.store.GetClientByID(ctx, int(id))
  143. if err != nil {
  144. models.RespondWithError(c, err)
  145. return
  146. }
  147. if client.ID == 0 {
  148. appErr := models.NewErrNotFound("client_not_found_for_deployments", fmt.Errorf("Client with ID %d not found", id))
  149. models.RespondWithError(c, appErr)
  150. return
  151. }
  152. deployments := []models.Deployment{}
  153. c.JSON(http.StatusOK, deployments)
  154. }