Backend: - F141: GET /discover/playlists/editorial for editorial playlists - F143: GET /playlists/shared/:token (public, no auth) - F145: POST /playlists/import (JSON), GET /playlists/:id/export/m3u - F136: GET /playlists/favoris (creates Favoris playlist if needed) - Repo: GetFavorisByUserID, service GetOrCreateFavorisPlaylist Frontend: - SharedPlaylistPage at /playlists/shared/:token (public route) - Editorial playlists section in DiscoverPage - Export M3U in ExportPlaylistButton dropdown - Import JSON via ImportPlaylistButton (PlaylistListPage) - Favoris sidebar link, FavorisRedirectPage, AddToFavorisButton on tracks Roadmap: v0.10.4 marked DONE
251 lines
8.3 KiB
Go
251 lines
8.3 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
|
|
"veza-backend-api/internal/models"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// PlaylistRepository définit l'interface pour les opérations sur les playlists
|
|
type PlaylistRepository interface {
|
|
// Create crée une nouvelle playlist
|
|
Create(ctx context.Context, playlist *models.Playlist) error
|
|
|
|
// GetByID récupère une playlist par son ID
|
|
GetByID(ctx context.Context, id uuid.UUID) (*models.Playlist, error)
|
|
|
|
// GetByUserID récupère les playlists d'un utilisateur
|
|
GetByUserID(ctx context.Context, userID uuid.UUID, limit, offset int) ([]*models.Playlist, int64, error)
|
|
|
|
// Update met à jour une playlist
|
|
Update(ctx context.Context, playlist *models.Playlist) error
|
|
|
|
// Delete supprime une playlist
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
|
|
// List récupère une liste de playlists avec pagination
|
|
List(ctx context.Context, filterUserID *uuid.UUID, viewerID *uuid.UUID, isPublic *bool, limit, offset int) ([]*models.Playlist, int64, error)
|
|
|
|
// Exists vérifie si une playlist existe
|
|
Exists(ctx context.Context, id uuid.UUID) (bool, error)
|
|
|
|
// GetByIDWithTracks récupère une playlist avec ses tracks
|
|
// T0501: Create Playlist Performance Optimization
|
|
GetByIDWithTracks(ctx context.Context, id uuid.UUID) (*models.Playlist, error)
|
|
|
|
// Search recherche des playlists selon des critères
|
|
// T0496: Create Playlist Search Backend
|
|
Search(ctx context.Context, query string, filterUserID *uuid.UUID, isPublic *bool, limit, offset int) ([]*models.Playlist, int64, error)
|
|
|
|
// GetFavorisByUserID returns the user's Favoris playlist (is_default_favorites=true). v0.10.4 F136
|
|
GetFavorisByUserID(ctx context.Context, userID uuid.UUID) (*models.Playlist, error)
|
|
}
|
|
|
|
// playlistRepository implémente PlaylistRepository avec GORM
|
|
type playlistRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewPlaylistRepository crée une nouvelle instance de PlaylistRepository
|
|
func NewPlaylistRepository(db *gorm.DB) PlaylistRepository {
|
|
return &playlistRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// Create crée une nouvelle playlist
|
|
func (r *playlistRepository) Create(ctx context.Context, playlist *models.Playlist) error {
|
|
return r.db.WithContext(ctx).Create(playlist).Error
|
|
}
|
|
|
|
// GetByID récupère une playlist par son ID
|
|
// T0501: Optimisé avec lazy loading des tracks
|
|
func (r *playlistRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.Playlist, error) {
|
|
var playlist models.Playlist
|
|
// T0501: Ne pas charger les tracks par défaut (lazy loading)
|
|
// Les tracks seront chargés à la demande via GetTracks si nécessaire
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("User").
|
|
First(&playlist, "id = ?", id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &playlist, nil
|
|
}
|
|
|
|
// GetByIDWithTracks récupère une playlist avec ses tracks (pour les cas où on en a besoin)
|
|
// T0501: Méthode séparée pour charger les tracks à la demande
|
|
func (r *playlistRepository) GetByIDWithTracks(ctx context.Context, id uuid.UUID) (*models.Playlist, error) {
|
|
var playlist models.Playlist
|
|
if err := r.db.WithContext(ctx).
|
|
Preload("User").
|
|
Preload("Tracks").
|
|
Preload("Tracks.Track").
|
|
First(&playlist, "id = ?", id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &playlist, nil
|
|
}
|
|
|
|
// GetByUserID récupère les playlists d'un utilisateur
|
|
// MIGRATION UUID: userID migré vers uuid.UUID
|
|
func (r *playlistRepository) GetByUserID(ctx context.Context, userID uuid.UUID, limit, offset int) ([]*models.Playlist, int64, error) {
|
|
var playlists []*models.Playlist
|
|
var total int64
|
|
|
|
query := r.db.WithContext(ctx).Model(&models.Playlist{}).Where("user_id = ?", userID)
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := query.Preload("User").
|
|
Order("created_at DESC").
|
|
Offset(offset).
|
|
Limit(limit).
|
|
Find(&playlists).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return playlists, total, nil
|
|
}
|
|
|
|
// Update met à jour une playlist
|
|
func (r *playlistRepository) Update(ctx context.Context, playlist *models.Playlist) error {
|
|
return r.db.WithContext(ctx).Save(playlist).Error
|
|
}
|
|
|
|
// Delete supprime une playlist (soft delete)
|
|
// BE-DB-003: Add soft delete support to all models
|
|
func (r *playlistRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Delete(&models.Playlist{}, "id = ?", id).Error
|
|
}
|
|
|
|
// Restore restaure une playlist supprimée (soft delete)
|
|
// BE-DB-003: Add soft delete support to all models
|
|
func (r *playlistRepository) Restore(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Unscoped().Model(&models.Playlist{}).Where("id = ?", id).Update("deleted_at", nil).Error
|
|
}
|
|
|
|
// HardDelete supprime définitivement une playlist (hard delete)
|
|
// BE-DB-003: Add soft delete support to all models
|
|
func (r *playlistRepository) HardDelete(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Unscoped().Delete(&models.Playlist{}, "id = ?", id).Error
|
|
}
|
|
|
|
// List récupère une liste de playlists avec pagination
|
|
// MIGRATION UUID: filterUserID migré vers *uuid.UUID
|
|
// MOD: Added viewerID to support mixed visibility (Public OR Owned) in SQL
|
|
func (r *playlistRepository) List(ctx context.Context, filterUserID *uuid.UUID, viewerID *uuid.UUID, isPublic *bool, limit, offset int) ([]*models.Playlist, int64, error) {
|
|
var playlists []*models.Playlist
|
|
var total int64
|
|
|
|
query := r.db.WithContext(ctx).Model(&models.Playlist{})
|
|
|
|
// Filtrage intelligent
|
|
if filterUserID != nil {
|
|
// Cas 1: Profil utilisateur spécifique
|
|
// ... (rest of filtering)
|
|
query = query.Where("user_id = ?", *filterUserID)
|
|
|
|
if isPublic != nil {
|
|
query = query.Where("is_public = ?", *isPublic)
|
|
} else if viewerID != nil && *viewerID == *filterUserID {
|
|
// Le propriétaire voit tout (is_public ignoré)
|
|
} else {
|
|
// Visiteur voit seulement public
|
|
query = query.Where("is_public = ?", true)
|
|
}
|
|
} else {
|
|
// Cas 2: Liste globale / Feed
|
|
if isPublic != nil {
|
|
// Filtre strict (ex: seulement publiques)
|
|
query = query.Where("is_public = ?", *isPublic)
|
|
} else if viewerID != nil {
|
|
// Mixte : Publiques OU Mes playlists
|
|
query = query.Where("is_public = ? OR user_id = ?", true, *viewerID)
|
|
} else {
|
|
// Anonyme : Seulement publiques
|
|
query = query.Where("is_public = ?", true)
|
|
}
|
|
}
|
|
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if err := query.Preload("User").
|
|
Order("created_at DESC").
|
|
Offset(offset).
|
|
Limit(limit).
|
|
Find(&playlists).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return playlists, total, nil
|
|
}
|
|
|
|
// Exists vérifie si une playlist existe
|
|
func (r *playlistRepository) Exists(ctx context.Context, id uuid.UUID) (bool, error) {
|
|
var count int64
|
|
err := r.db.WithContext(ctx).Model(&models.Playlist{}).Where("id = ?", id).Count(&count).Error
|
|
return count > 0, err
|
|
}
|
|
|
|
// Search recherche des playlists selon des critères
|
|
// T0496: Create Playlist Search Backend
|
|
// MIGRATION UUID: filterUserID migré vers *uuid.UUID
|
|
func (r *playlistRepository) Search(ctx context.Context, query string, filterUserID *uuid.UUID, isPublic *bool, limit, offset int) ([]*models.Playlist, int64, error) {
|
|
var playlists []*models.Playlist
|
|
var total int64
|
|
|
|
dbQuery := r.db.WithContext(ctx).Model(&models.Playlist{})
|
|
|
|
// Recherche par titre ou description
|
|
if query != "" {
|
|
searchPattern := "%" + query + "%"
|
|
// Title field is mapped to 'name' column in database
|
|
dbQuery = dbQuery.Where("(name LIKE ? OR description LIKE ?)", searchPattern, searchPattern)
|
|
}
|
|
|
|
// Filtrer par utilisateur
|
|
if filterUserID != nil {
|
|
dbQuery = dbQuery.Where("user_id = ?", *filterUserID)
|
|
}
|
|
|
|
// Filtrer par statut public/privé
|
|
if isPublic != nil {
|
|
dbQuery = dbQuery.Where("is_public = ?", *isPublic)
|
|
}
|
|
|
|
// Compter le total
|
|
if err := dbQuery.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// Récupérer les playlists avec pagination
|
|
if err := dbQuery.
|
|
Preload("User").
|
|
Order("created_at DESC").
|
|
Offset(offset).
|
|
Limit(limit).
|
|
Find(&playlists).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return playlists, total, nil
|
|
}
|
|
|
|
// GetFavorisByUserID returns the user's Favoris playlist (is_default_favorites=true). v0.10.4 F136
|
|
func (r *playlistRepository) GetFavorisByUserID(ctx context.Context, userID uuid.UUID) (*models.Playlist, error) {
|
|
var playlist models.Playlist
|
|
if err := r.db.WithContext(ctx).
|
|
Where("user_id = ? AND is_default_favorites = ?", userID, true).
|
|
First(&playlist).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &playlist, nil
|
|
}
|