[BE-API-014] be-api: Implement track versions restore endpoint

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

Phase: PHASE-2
Priority: P2
Progress: 23/267 (8.6%)
This commit is contained in:
senke 2025-12-24 11:20:38 +01:00
parent 980a8cc30c
commit 3eef8e0366
3 changed files with 72 additions and 1 deletions

View file

@ -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": [
{

View file

@ -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
}
}

View file

@ -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"})
}