TrackType feat: added hierarchy, seeders, factory and CRUD

This commit is contained in:
Marco Cavalli 2025-02-06 12:08:06 +01:00
parent f89f1a97ee
commit 6a595a60dd
6 changed files with 224 additions and 6 deletions

View file

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Models\TrackType;
use Illuminate\Http\Request;
class TrackTypeController extends Controller
{
/**
* Retrieve all track types
* @return string
*/
public function index(bool $parents = false) {
if ($parents) {
$trackTypes = TrackType::where('parent_id', null)->get();
} else {
$trackTypes = TrackType::all();
}
return $trackTypes->toJson();
}
/**
* Create a new track type
* @param Request $request
* @return string
*/
public function store(Request $request) {
$trackType = new TrackType();
$validated = $request->validate([
'code' => 'required|max:16|unique:track_types',
'visibility' => 'required|boolean',
'type_name' => 'required|max:64',
'description' => 'max:255',
'analyze_cue_points' => 'boolean'
]);
$trackType->fill($request->all());
$trackType->save();
return $trackType->toJson();
}
/**
* Show a specific track type
* @param TrackType $trackType
* @return string
*/
public function show(int $id) {
$trackType = TrackType::whereId($id)->first();
return $trackType->toJson();
}
/**
* Update an existing track type
* @param Request $request
* @param TrackType $trackType
* @return bool|string
*/
public function update(Request $request, TrackType $trackType) {
// Check if track type is editable (seeded track types with parent = null
// can't be modified) and if someone's trying to change the code (is a
// unique index so it can't be changed
if (!$trackType->parent() || $trackType->code != $request->code) {
return false;
}
$trackType->fill($request->all());
$trackType->save();
return $trackType->toJson();
}
/**
* Delete a track type
* @param TrackType $trackType
* @return bool
*/
public function destroy(TrackType $trackType) {
// Check if track type is deletable (seeded track types with parent = null
// can't be deleted)
if (!$trackType->parent() || $trackType->files->count() > 0) {
return false;
}
$trackType->delete();
return true;
}
}

31
app/Models/TrackType.php Normal file
View file

@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TrackType extends Model
{
use HasFactory;
protected $table = 'cc_track_types';
public $timestamps = false;
protected $fillable = [
'code',
'visibility',
'type_name',
'description',
'analyze_cue_points',
'parent_id'
];
public function parent() {
return $this->belongsTo(TrackType::class, 'parent_id');
}
public function files() {
return $this->hasMany(File::class);
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Database\Factories;
use App\Models\TrackType;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\TrackType>
*/
class TrackTypeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'code' => $this->faker->unique()->text(16),
'visibility' => $this->faker->randomElement([true, false]),
'type_name' => Str::random(20),
'description' => Str::random(255),
'analyze_cue_points' => $this->faker->randomElement([true, false]),
'parent_id' => function () {
return TrackType::count() > 3 ? TrackType::where('id', '<=', 3)->random(1)->first()->id : null;
}
];
}
}

View file

@ -0,0 +1,28 @@
<?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::table('cc_track_types', function (Blueprint $table) {
$table->foreignId('parent_id')->after('id')->nullable()->references('id')->on('cc_track_types');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('cc_track_types', function (Blueprint $table) {
$table->dropColumn('parent_id');
});
}
};

View file

@ -12,11 +12,8 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// \App\Models\User::factory(10)->create();
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call([
TrackTypeSeeder::class,
]);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class TrackTypeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('cc_track_types')->insert([
'code' => 'MUSICA',
'visibility' => false,
'type_name' => 'Musica',
'description' => 'Tracce musicali',
'analyze_cue_points' => false,
'parent_id' => null,
]);
DB::table('cc_track_types')->insert([
'code' => 'JINGLES',
'visibility' => false,
'type_name' => 'Jingles',
'description' => 'Tracce jingle',
'analyze_cue_points' => false,
'parent_id' => null,
]);
DB::table('cc_track_types')->insert([
'code' => 'SPOT',
'visibility' => false,
'type_name' => 'Spot',
'description' => 'Tracce pubblicitarie',
'analyze_cue_points' => false,
'parent_id' => null,
]);
}
}