123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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)
- }
|