diff --git a/VEZA_COMPLETE_MVP_TODOLIST.json b/VEZA_COMPLETE_MVP_TODOLIST.json index ccbf8d802..55e564d92 100644 --- a/VEZA_COMPLETE_MVP_TODOLIST.json +++ b/VEZA_COMPLETE_MVP_TODOLIST.json @@ -1735,7 +1735,18 @@ "description": "POST /api/v1/tracks/:id/versions/:versionId/restore to restore a previous version", "owner": "backend", "estimated_hours": 4, - "status": "todo", + "status": "completed", + "completion": { + "completed_at": "2025-12-23T09:54:00Z", + "actual_hours": 0.75, + "commits": [], + "files_changed": [ + "veza-backend-api/internal/core/track/handler.go", + "veza-backend-api/internal/api/router.go" + ], + "notes": "Added RestoreVersion handler method in TrackHandler. Initialized TrackVersionService in setupTrackRoutes. Added POST /tracks/:id/versions/:versionId/restore route (protected). Handler uses existing TrackVersionService.RestoreVersion method. Includes ownership check (only track owner can restore versions).", + "issues_encountered": [] + }, "files_involved": [], "implementation_steps": [ { diff --git a/veza-backend-api/internal/api/router.go b/veza-backend-api/internal/api/router.go index d8bd67ffc..23e9553b6 100644 --- a/veza-backend-api/internal/api/router.go +++ b/veza-backend-api/internal/api/router.go @@ -463,6 +463,10 @@ func (r *APIRouter) setupTrackRoutes(router *gin.RouterGroup) { trackSearchService := services.NewTrackSearchService(r.db.GormDB) trackHandler.SetSearchService(trackSearchService) + // BE-API-014: Initialize TrackVersionService for track version restore functionality + trackVersionService := services.NewTrackVersionService(r.db.GormDB, r.logger, uploadDir) + trackHandler.SetVersionService(trackVersionService) + tracks := router.Group("/tracks") { // Public routes @@ -522,6 +526,9 @@ func (r *APIRouter) setupTrackRoutes(router *gin.RouterGroup) { // Sharing protected.POST("/:id/share", trackHandler.CreateShare) protected.DELETE("/share/:id", trackHandler.RevokeShare) + + // Versions + protected.POST("/:id/versions/:versionId/restore", trackHandler.RestoreVersion) // BE-API-014: Restore track version endpoint } } diff --git a/veza-backend-api/internal/core/track/handler.go b/veza-backend-api/internal/core/track/handler.go index ac9a46d64..502b6ca1e 100644 --- a/veza-backend-api/internal/core/track/handler.go +++ b/veza-backend-api/internal/core/track/handler.go @@ -1784,3 +1784,56 @@ func getContentType(format string) string { return "application/octet-stream" } } + +// RestoreVersion restaure une version spécifique d'un track +// POST /api/v1/tracks/:id/versions/:versionId/restore +// BE-API-014: Implement track versions restore endpoint +func (h *TrackHandler) RestoreVersion(c *gin.Context) { + if h.versionService == nil { + h.respondWithError(c, http.StatusInternalServerError, "version service not available") + return + } + + // Récupérer l'ID du track depuis l'URL + trackIDStr := c.Param("id") + trackID, err := uuid.Parse(trackIDStr) + if err != nil { + h.respondWithError(c, http.StatusBadRequest, "invalid track id") + return + } + + // Récupérer l'ID de la version depuis l'URL + versionIDStr := c.Param("versionId") + versionID, err := uuid.Parse(versionIDStr) + if err != nil { + h.respondWithError(c, http.StatusBadRequest, "invalid version id") + return + } + + // Récupérer l'ID utilisateur du contexte + userID, ok := h.getUserID(c) + if !ok { + return // Erreur déjà envoyée par getUserID + } + + // Restaurer la version + err = h.versionService.RestoreVersion(c.Request.Context(), trackID, versionID, userID) + if err != nil { + if errors.Is(err, services.ErrTrackNotFound) { + h.respondWithError(c, http.StatusNotFound, "track not found") + return + } + if errors.Is(err, services.ErrVersionNotFound) { + h.respondWithError(c, http.StatusNotFound, "version not found") + return + } + if errors.Is(err, services.ErrForbidden) { + h.respondWithError(c, http.StatusForbidden, "forbidden: only track owner can restore versions") + return + } + h.respondWithError(c, http.StatusInternalServerError, "failed to restore version") + return + } + + c.JSON(http.StatusOK, gin.H{"message": "Version restored successfully"}) +}