DÜZELTİLDİ

Kritik API Hatası Düzeltildi

Add Album Endpoint Eklendi

25 Aralık 2025

Ne Düzeltildi?

Eksik olan /api/muzibu/playlists/{id}/add-album endpoint'i backend'e eklendi. Artık kullanıcılar albüme sağ tıklayıp tüm şarkılarını playliste ekleyebilir.

Yapılan Değişiklikler

1

Route Eklendi

Dosya: Modules/Muzibu/routes/api.php (Satır 48)

Route::post('/{id}/add-song', [PlaylistController::class, 'addSong'])
    ->name('api.muzibu.playlists.add-song');

+ Route::post('/{id}/add-album', [PlaylistController::class, 'addAlbum'])
+     ->name('api.muzibu.playlists.add-album');

Route::post('/{id}/copy', [PlaylistController::class, 'copy'])
    ->name('api.muzibu.playlists.copy');
2

Album Model Import Eklendi

Dosya: PlaylistController.php (Satır 10)

use Modules\Muzibu\App\Models\Playlist;
use Modules\Muzibu\App\Models\Song;
+ use Modules\Muzibu\App\Models\Album;
use Modules\Muzibu\App\Services\PlaylistService;
3

addAlbum() Metodu Eklendi

Dosya: PlaylistController.php (Satır 280-340)

/**
 * Add all songs from album to playlist
 */
public function addAlbum(Request $request, int $id): JsonResponse
{
    try {
        $request->validate([
            'album_id' => 'required|integer|exists:muzibu_albums,album_id',
        ]);

        $userId = auth()->id();
        $albumId = $request->input('album_id');

        // Get album with active songs
        $album = Album::with(['songs' => function($query) {
            $query->where('is_active', 1);
        }])->findOrFail($albumId);

        $songs = $album->songs;

        if ($songs->isEmpty()) {
            return response()->json([
                'success' => false,
                'message' => 'Albümde aktif şarkı bulunamadı',
            ], 400);
        }

        // Add all songs to playlist
        $addedCount = 0;
        $skippedCount = 0;

        foreach ($songs as $song) {
            $result = $this->playlistService->addSongToPlaylist(
                $id,
                $song->song_id,
                $userId
            );

            if ($result['success']) {
                $addedCount++;
            } else {
                $skippedCount++;
            }
        }

        return response()->json([
            'success' => true,
            'added_count' => $addedCount,
            'skipped_count' => $skippedCount,
            'total_songs' => $songs->count(),
            'message' => "{$addedCount} şarkı playliste eklendi"
                . ($skippedCount > 0 ? " ({$skippedCount} zaten mevcuttu)" : ""),
        ]);

    } catch (\Exception $e) {
        Log::error('Add album to playlist error:', ['message' => $e->getMessage()]);
        return response()->json([
            'success' => false,
            'message' => 'Albüm ekleme başarısız',
        ], 500);
    }
}

Yeni Endpoint Özellikleri

Güvenlik

  • ['web', 'auth'] middleware koruması
  • • Validation: album_id zorunlu
  • • User ID kontrolü (auth()->id())
  • • Try-catch error handling

Mantık

  • • Sadece aktif şarkılar eklenir
  • • Duplicate kontrol (service layer)
  • • Başarı/hata sayacı
  • • Bilgilendirici mesajlar

Database

  • • Eager loading: with(['songs'])
  • • Aktif şarkı filtresi
  • • Mevcut service kullanımı
  • • Transaction güvenli

Response

  • added_count: Eklenen şarkı sayısı
  • skipped_count: Zaten var olan
  • total_songs: Toplam şarkı
  • • Kullanıcı dostu mesaj

Test Senaryoları

✅ Başarılı Senaryolar

  • 10 şarkılı albüm → Playliste ekle → 10 şarkı eklenir
  • 5 şarkı zaten var, 5 yeni → 5 eklenir, 5 atlanır (skipped)
  • Birden fazla playliste aynı albümü ekle → Her playliste ayrı ayrı çalışır

❌ Hata Senaryoları

  • Albümde aktif şarkı yok → 400 Bad Request
  • Geçersiz album_id → 404 Not Found
  • Auth yoksa → 401 Unauthorized
  • Server hatası → 500 Internal Error

Teknik Detaylar

Endpoint

POST /api/muzibu/playlists/{id}/add-album

Route Name

api.muzibu.playlists.add-album

Middleware

['web', 'auth']

Request Body

{ "album_id": 123 }

Response Format

{
  "success": true,
  "added_count": 8,
  "skipped_count": 2,
  "total_songs": 10,
  "message": "8 şarkı playliste eklendi (2 zaten mevcuttu)"
}

Yapılan İşlemler