package handlers import ( "fmt" "net/http" "strconv" "git.linuxforward.com/byop/byop-engine/models" "git.linuxforward.com/byop/byop-engine/services" "github.com/gin-gonic/gin" ) // AppsHandler handles app-related operations type AppsHandler struct { service *services.AppService } // NewAppsHandler creates a new AppsHandler func NewAppsHandler(service *services.AppService) *AppsHandler { return &AppsHandler{ service: service, } } // ListApps returns all apps with optional filtering func (h *AppsHandler) ListApps(c *gin.Context) { filter := make(map[string]interface{}) // Attempt to bind query parameters, but allow empty filters if err := c.ShouldBindQuery(&filter); err != nil && len(filter) > 0 { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid query parameters"}) return } apps, err := h.service.ListApps(filter) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to list apps: %v", err)}) return } c.JSON(http.StatusOK, apps) } // CreateApp creates a new deployment app func (h *AppsHandler) CreateApp(c *gin.Context) { var app models.App if err := c.ShouldBindJSON(&app); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)}) return } // Get the user ID from the context (set by auth middleware) userID, exists := c.Get("userID") if exists { app.CreatedBy = userID.(string) } if err := h.service.CreateApp(&app); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to create app: %v", err)}) return } c.JSON(http.StatusCreated, app) } // GetApp returns a specific app func (h *AppsHandler) GetApp(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"}) return } app, err := h.service.GetApp(id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app: %v", err)}) return } if app == nil { c.JSON(http.StatusNotFound, gin.H{"error": "App not found"}) return } c.JSON(http.StatusOK, app) } // UpdateApp updates an app func (h *AppsHandler) UpdateApp(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"}) return } var updatedApp models.App if err := c.ShouldBindJSON(&updatedApp); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid request body: %v", err)}) return } // Ensure the ID matches the URL parameter updatedApp.ID = id if err := h.service.UpdateApp(&updatedApp); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to update app: %v", err)}) return } c.JSON(http.StatusOK, updatedApp) } // DeleteApp deletes an app func (h *AppsHandler) DeleteApp(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"}) return } if err := h.service.DeleteApp(id); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to delete app: %v", err)}) return } c.Status(http.StatusNoContent) } // GetAppDeployments returns all deployments for an app func (h *AppsHandler) GetAppDeployments(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid app ID"}) return } deployments, err := h.service.GetAppDeployments(id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app deployments: %v", err)}) return } c.JSON(http.StatusOK, deployments) } // GetAppByVersion handles retrieval of an app by name and version func (h *AppsHandler) GetAppByVersion(c *gin.Context) { name := c.Query("name") version := c.Query("version") if name == "" || version == "" { c.JSON(http.StatusBadRequest, gin.H{"error": "Both name and version parameters are required"}) return } app, err := h.service.GetAppByVersion(name, version) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to fetch app: %v", err)}) return } if app == nil { c.JSON(http.StatusNotFound, gin.H{"error": "App not found"}) return } c.JSON(http.StatusOK, app) }