clients.go 4.3 KB

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