apps.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // AppHandler handles application-related operations
  10. type AppHandler struct {
  11. service *services.AppService
  12. }
  13. // NewAppHandler creates a new AppHandler
  14. func NewAppHandler(service *services.AppService) *AppHandler {
  15. return &AppHandler{
  16. service: service,
  17. }
  18. }
  19. // RegisterRoutes registers routes for app operations
  20. func (h *AppHandler) RegisterRoutes(r *gin.RouterGroup) {
  21. r.GET("/", h.ListApps)
  22. r.POST("/", h.CreateApp)
  23. r.GET("/:id", h.GetApp)
  24. r.PUT("/:id", h.UpdateApp)
  25. r.DELETE("/:id", h.DeleteApp)
  26. r.GET("/:id/deployments", h.GetAppDeployments)
  27. }
  28. // ListApps returns all applications with optional filtering
  29. func (h *AppHandler) ListApps(c *gin.Context) {
  30. filter := make(map[string]interface{})
  31. // Attempt to bind query parameters, but allow empty filters
  32. if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 {
  33. c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"})
  34. return
  35. }
  36. apps, err := h.service.ListApps(filter)
  37. if err != nil {
  38. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list applications: %v", err)})
  39. return
  40. }
  41. c.JSON(http.StatusOK, apps)
  42. }
  43. // CreateApp creates a new application
  44. func (h *AppHandler) CreateApp(c *gin.Context) {
  45. var app models.App
  46. if err := c.ShouldBindJSON(&app); err != nil {
  47. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  48. return
  49. }
  50. // Get the user ID from the context (set by auth middleware)
  51. userID, exists := c.Get("userID")
  52. if exists {
  53. app.CreatedBy = userID.(string)
  54. }
  55. if err := h.service.CreateApp(&app); err != nil {
  56. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create application: %v", err)})
  57. return
  58. }
  59. c.JSON(http.StatusCreated, app)
  60. }
  61. // GetApp returns a specific application
  62. func (h *AppHandler) GetApp(c *gin.Context) {
  63. id := c.Param("id")
  64. app, err := h.service.GetApp(id)
  65. if err != nil {
  66. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch application: %v", err)})
  67. return
  68. }
  69. if app == nil {
  70. c.JSON(http.StatusNotFound, gin.H{"error": "Application not found"})
  71. return
  72. }
  73. c.JSON(http.StatusOK, app)
  74. }
  75. // UpdateApp updates an application
  76. func (h *AppHandler) UpdateApp(c *gin.Context) {
  77. id := c.Param("id")
  78. var updatedApp models.App
  79. if err := c.ShouldBindJSON(&updatedApp); err != nil {
  80. c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)})
  81. return
  82. }
  83. // Ensure the ID matches the URL parameter
  84. updatedApp.ID = id
  85. if err := h.service.UpdateApp(&updatedApp); err != nil {
  86. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update application: %v", err)})
  87. return
  88. }
  89. c.JSON(http.StatusOK, updatedApp)
  90. }
  91. // DeleteApp deletes an application
  92. func (h *AppHandler) DeleteApp(c *gin.Context) {
  93. id := c.Param("id")
  94. if err := h.service.DeleteApp(id); err != nil {
  95. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete application: %v", err)})
  96. return
  97. }
  98. c.Status(http.StatusNoContent)
  99. }
  100. // GetAppDeployments returns all deployments for an application
  101. func (h *AppHandler) GetAppDeployments(c *gin.Context) {
  102. id := c.Param("id")
  103. deployments, err := h.service.GetAppDeployments(id)
  104. if err != nil {
  105. c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch application deployments: %v", err)})
  106. return
  107. }
  108. c.JSON(http.StatusOK, deployments)
  109. }