apps.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. // AppsHandler handles app-related operations
  11. type AppsHandler struct {
  12. service *services.AppService
  13. }
  14. // NewAppsHandler creates a new AppsHandler
  15. func NewAppsHandler(service *services.AppService) *AppsHandler {
  16. return &AppsHandler{
  17. service: service,
  18. }
  19. }
  20. // ListApps returns all apps with optional filtering
  21. func (h *AppsHandler) ListApps(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. apps, err := h.service.ListApps(filter)
  29. if err != nil {
  30. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list apps: %v", err)})
  31. return
  32. }
  33. c.JSON(http.StatusOK, apps)
  34. }
  35. // CreateApp creates a new deployment app
  36. func (h *AppsHandler) CreateApp(c *gin.Context) {
  37. var app models.App
  38. if err := c.ShouldBindJSON(&app); err != nil {
  39. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  40. return
  41. }
  42. // Get the user ID from the context (set by auth middleware)
  43. userID, exists := c.Get("userID")
  44. if exists {
  45. app.CreatedBy = userID.(string)
  46. }
  47. if err := h.service.CreateApp(&app); err != nil {
  48. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create app: %v", err)})
  49. return
  50. }
  51. c.JSON(http.StatusCreated, app)
  52. }
  53. // GetApp returns a specific app
  54. func (h *AppsHandler) GetApp(c *gin.Context) {
  55. idStr := c.Param("id")
  56. id, err := strconv.ParseInt(idStr, 10, 64)
  57. if err != nil {
  58. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"})
  59. return
  60. }
  61. app, err := h.service.GetApp(id)
  62. if err != nil {
  63. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app: %v", err)})
  64. return
  65. }
  66. if app == nil {
  67. c.JSON(http.StatusNotFound, gin.H{"error": "App not found"})
  68. return
  69. }
  70. c.JSON(http.StatusOK, app)
  71. }
  72. // UpdateApp updates an app
  73. func (h *AppsHandler) UpdateApp(c *gin.Context) {
  74. idStr := c.Param("id")
  75. id, err := strconv.ParseInt(idStr, 10, 64)
  76. if err != nil {
  77. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"})
  78. return
  79. }
  80. var updatedApp models.App
  81. if err := c.ShouldBindJSON(&updatedApp); err != nil {
  82. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  83. return
  84. }
  85. // Ensure the ID matches the URL parameter
  86. updatedApp.ID = id
  87. if err := h.service.UpdateApp(&updatedApp); err != nil {
  88. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update app: %v", err)})
  89. return
  90. }
  91. c.JSON(http.StatusOK, updatedApp)
  92. }
  93. // DeleteApp deletes an app
  94. func (h *AppsHandler) DeleteApp(c *gin.Context) {
  95. idStr := c.Param("id")
  96. id, err := strconv.ParseInt(idStr, 10, 64)
  97. if err != nil {
  98. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"})
  99. return
  100. }
  101. if err := h.service.DeleteApp(id); err != nil {
  102. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete app: %v", err)})
  103. return
  104. }
  105. c.Status(http.StatusNoContent)
  106. }
  107. // GetAppDeployments returns all deployments for an app
  108. func (h *AppsHandler) GetAppDeployments(c *gin.Context) {
  109. idStr := c.Param("id")
  110. id, err := strconv.ParseInt(idStr, 10, 64)
  111. if err != nil {
  112. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"})
  113. return
  114. }
  115. deployments, err := h.service.GetAppDeployments(id)
  116. if err != nil {
  117. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app deployments: %v", err)})
  118. return
  119. }
  120. c.JSON(http.StatusOK, deployments)
  121. }
  122. // GetAppByVersion handles retrieval of an app by name and version
  123. func (h *AppsHandler) GetAppByVersion(c *gin.Context) {
  124. name := c.Query("name")
  125. version := c.Query("version")
  126. if name == "" || version == "" {
  127. c.JSON(http.StatusBadRequest, gin.H{"error": "Both name and version parameters are required"})
  128. return
  129. }
  130. app, err := h.service.GetAppByVersion(name, version)
  131. if err != nil {
  132. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app: %v", err)})
  133. return
  134. }
  135. if app == nil {
  136. c.JSON(http.StatusNotFound, gin.H{"error": "App not found"})
  137. return
  138. }
  139. c.JSON(http.StatusOK, app)
  140. }