added tracks and perfomers tables, fixed queries
This commit is contained in:
@@ -9,12 +9,12 @@ class PlaylistLoader
|
||||
public function getPlaylist(): array
|
||||
{
|
||||
$data = Track::select(
|
||||
"Track.Name as Track",
|
||||
"Performer.Name as Performer"
|
||||
)->join('Performer', function ($join) {
|
||||
$join->on('Performer.Id', "=", "Track.Performer")
|
||||
->where('Performer.DeleteDate', NULL);
|
||||
})->orderBy('Performer')->get();
|
||||
"tracks.name as Track",
|
||||
"performers.name as Performer"
|
||||
)->join('performers', function ($join) {
|
||||
$join->on('performers.id', "=", "tracks.performer_id")
|
||||
->where('performers.deleted_at', NULL);
|
||||
})->orderBy('performer_id')->get();
|
||||
|
||||
return $data ? $data->toArray() : [];
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ class Track extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
const DELETED_AT = 'DeleteDate';
|
||||
const UPDATED_AT = 'UpdatedDate';
|
||||
const CREATED_AT = 'DateOfCreation';
|
||||
const DELETED_AT = 'deleted_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
const CREATED_AT = 'created_at';
|
||||
|
||||
protected $primaryKey = 'Id';
|
||||
protected $table = 'Track';
|
||||
protected $fillable = ['Name', 'Length'];
|
||||
protected $table = 'tracks';
|
||||
protected $fillable = [ 'name', 'length' ];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('performers', function (Blueprint $table) {
|
||||
$table->id(); // id (bigint unsigned, auto-increment)
|
||||
$table->string('name'); // name (имя исполнителя / название группы)
|
||||
$table->timestamps(); // created_at и updated_at
|
||||
$table->softDeletes(); // deleted_at (для мягкого удаления)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('performers');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('tracks', function (Blueprint $table) {
|
||||
$table->id(); // id (bigint unsigned, auto-increment)
|
||||
$table->string('name'); // name (varchar 255)
|
||||
$table->unsignedInteger('length'); // length (длительность трека в секундах)
|
||||
|
||||
// performer_id (внешний ключ, связываем с таблицей performers)
|
||||
$table->foreignId('performer_id')
|
||||
->constrained('performers')
|
||||
->onDelete('cascade'); // если удалить исполнителя, удалятся и его треки
|
||||
|
||||
$table->timestamps(); // created_at и updated_at
|
||||
$table->softDeletes(); // deleted_at (для мягкого удаления)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('tracks');
|
||||
}
|
||||
};
|
||||
@@ -25,7 +25,9 @@ class DatabaseSeeder extends Seeder
|
||||
$this->call([
|
||||
UserSeeder::class,
|
||||
EventSeeder::class,
|
||||
PlaceSeeder::class
|
||||
PlaceSeeder::class,
|
||||
PerformerSeeder::class,
|
||||
TrackSeeder::class
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class PerformerSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// Массив исполнителей для вставки
|
||||
$performers = [
|
||||
['name' => 'Deep Purple'],
|
||||
['name' => 'Rainbow'],
|
||||
['name' => 'DIO'],
|
||||
['name' => 'Black Sabbath'],
|
||||
];
|
||||
|
||||
// Добавляем к каждой записи метки времени, чтобы поля не были пустыми
|
||||
$now = Carbon::now();
|
||||
foreach ($performers as &$performer) {
|
||||
$performer['created_at'] = $now;
|
||||
$performer['updated_at'] = $now;
|
||||
}
|
||||
|
||||
// Чистим таблицу перед заполнением (опционально, чтобы не дублировать)
|
||||
// DB::table('performers')->truncate();
|
||||
|
||||
// Массовая вставка в базу данных
|
||||
DB::table('performers')->insert($performers);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class TrackSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// SQL-запрос для проверки существования таблицы
|
||||
$tableExists = DB::select("SHOW TABLES LIKE 'performers'");
|
||||
|
||||
if (empty($tableExists)) {
|
||||
$this->command->error("Таблица 'performers' отсутствует в базе данных!");
|
||||
}
|
||||
|
||||
// Очищаем таблицу перед заполнением
|
||||
DB::table('tracks')->truncate();
|
||||
|
||||
// 1. Получаем ID исполнителей из базы по их именам
|
||||
$rainbowId = DB::table('performers')->where('name', 'Rainbow')->value('id');
|
||||
$blackSabbathId = DB::table('performers')->where('name', 'Black Sabbath')->value('id');
|
||||
$dioId = DB::table('performers')->where('name', 'DIO')->value('id');
|
||||
|
||||
// Если сидер запускается отдельно и исполнителей нет, остановим процесс
|
||||
if (!$rainbowId || !$blackSabbathId || !$dioId) {
|
||||
$this->command->error('Исполнители не найдены в базе данных. Сначала запустите PerformerSeeder!');
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Список треков со связями
|
||||
$tracksData = [
|
||||
// Rainbow
|
||||
['name' => 'Gates Of Babylon', 'performer_id' => $rainbowId],
|
||||
['name' => 'Lady Of The Lake', 'performer_id' => $rainbowId],
|
||||
['name' => 'Tarot Woman', 'performer_id' => $rainbowId],
|
||||
['name' => 'Long Live Rock\'n\'Roll', 'performer_id' => $rainbowId],
|
||||
['name' => 'Man On The Silver Mountain', 'performer_id' => $rainbowId],
|
||||
['name' => 'Kill The King', 'performer_id' => $rainbowId],
|
||||
['name' => 'Can\'t Let You Go', 'performer_id' => $rainbowId],
|
||||
['name' => 'Rainbow\'s Eyes', 'performer_id' => $rainbowId],
|
||||
['name' => 'Sixteen Century Greensleeves', 'performer_id' => $rainbowId],
|
||||
|
||||
// Black Sabbath
|
||||
['name' => 'Neon Knights', 'performer_id' => $blackSabbathId],
|
||||
['name' => 'Children Of The Sea', 'performer_id' => $blackSabbathId],
|
||||
['name' => 'Heaven And Hell', 'performer_id' => $blackSabbathId],
|
||||
|
||||
// DIO
|
||||
['name' => 'Holy Diver', 'performer_id' => $dioId],
|
||||
['name' => 'Last In Line', 'performer_id' => $dioId],
|
||||
['name' => 'All The Fools Sailed Away', 'performer_id' => $dioId],
|
||||
['name' => 'One Night In The City', 'performer_id' => $dioId],
|
||||
['name' => 'Straight Through The Heart', 'performer_id' => $dioId],
|
||||
];
|
||||
|
||||
// 3. Подготовка данных (добавляем случайную длину и метки времени)
|
||||
$now = Carbon::now();
|
||||
foreach ($tracksData as &$track) {
|
||||
$track['length'] = rand(180, 420); // Генерируем длину трека от 3 до 7 минут (в секундах)
|
||||
$track['created_at'] = $now;
|
||||
$track['updated_at'] = $now;
|
||||
}
|
||||
|
||||
// 4. Массовая вставка в базу данных
|
||||
DB::table('tracks')->insert($tracksData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user