package handlers import ( "net/http" "strconv" "github.com/TracksApp/tracks/internal/middleware" "github.com/TracksApp/tracks/internal/models" "github.com/TracksApp/tracks/internal/services" "github.com/gin-gonic/gin" ) // ProjectHandler handles project endpoints type ProjectHandler struct { projectService *services.ProjectService } // NewProjectHandler creates a new ProjectHandler func NewProjectHandler(projectService *services.ProjectService) *ProjectHandler { return &ProjectHandler{ projectService: projectService, } } // ListProjects handles GET /api/projects func (h *ProjectHandler) ListProjects(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } state := models.ProjectState(c.Query("state")) projects, err := h.projectService.GetProjects(userID, state) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, projects) } // GetProject handles GET /api/projects/:id func (h *ProjectHandler) GetProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } project, err := h.projectService.GetProject(userID, uint(projectID)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, project) } // CreateProject handles POST /api/projects func (h *ProjectHandler) CreateProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } var req services.CreateProjectRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } project, err := h.projectService.CreateProject(userID, req) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, project) } // UpdateProject handles PUT /api/projects/:id func (h *ProjectHandler) UpdateProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } var req services.UpdateProjectRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } project, err := h.projectService.UpdateProject(userID, uint(projectID), req) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, project) } // DeleteProject handles DELETE /api/projects/:id func (h *ProjectHandler) DeleteProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } if err := h.projectService.DeleteProject(userID, uint(projectID)); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "Project deleted"}) } // CompleteProject handles POST /api/projects/:id/complete func (h *ProjectHandler) CompleteProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } project, err := h.projectService.CompleteProject(userID, uint(projectID)) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, project) } // ActivateProject handles POST /api/projects/:id/activate func (h *ProjectHandler) ActivateProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } project, err := h.projectService.ActivateProject(userID, uint(projectID)) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, project) } // HideProject handles POST /api/projects/:id/hide func (h *ProjectHandler) HideProject(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } project, err := h.projectService.HideProject(userID, uint(projectID)) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, project) } // MarkReviewed handles POST /api/projects/:id/review func (h *ProjectHandler) MarkReviewed(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } project, err := h.projectService.MarkProjectReviewed(userID, uint(projectID)) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, project) } // GetProjectStats handles GET /api/projects/:id/stats func (h *ProjectHandler) GetProjectStats(c *gin.Context) { userID, err := middleware.GetCurrentUserID(c) if err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "Not authenticated"}) return } projectID, err := strconv.ParseUint(c.Param("id"), 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"}) return } stats, err := h.projectService.GetProjectStats(userID, uint(projectID)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, stats) }