diff --git a/app/Http/Controllers/JoinedController.php b/app/Http/Controllers/JoinedController.php index 3f38697..fdaf961 100644 --- a/app/Http/Controllers/JoinedController.php +++ b/app/Http/Controllers/JoinedController.php @@ -4,7 +4,6 @@ use App\Http\Requests\StoreJoinedRequest; use App\Http\Requests\UpdateJoinedRequest; -use App\Models\Category; use App\Models\Joined; class JoinedController extends Controller @@ -26,15 +25,7 @@ public function create() public function store(StoreJoinedRequest $request) { - $validated = $request->safe()->only([ - 'categories', - 'url', - 'subject', - 'image', - 'approved', - ]); - - Joined::store($validated); + Joined::store($request->validated()); return redirect()->route('admin.joined.index')->with('success', 'Fanlisting added.'); } @@ -46,6 +37,9 @@ public function edit(Joined $joined) public function update(UpdateJoinedRequest $request, Joined $joined) { + $joined->patch($request->validated()); + + return redirect()->route('admin.joined.index')->with('success', 'Fanlisting updated.'); } public function destroy(Joined $joined) diff --git a/app/Http/Requests/StoreJoinedRequest.php b/app/Http/Requests/StoreJoinedRequest.php index 10325cc..37b0313 100644 --- a/app/Http/Requests/StoreJoinedRequest.php +++ b/app/Http/Requests/StoreJoinedRequest.php @@ -4,7 +4,6 @@ use App\Models\Joined; use Illuminate\Foundation\Http\FormRequest; -use Illuminate\Support\Facades\Auth; class StoreJoinedRequest extends FormRequest { @@ -27,11 +26,11 @@ public function rules() { return [ 'categories' => ['required', 'array'], - 'categories.*' => [ 'numeric', 'exists:categories,id'], - 'url' => ['required', 'url'], - 'subject' => ['required', 'string'], - 'image' => ['nullable', 'image'], - 'approved' => ['nullable', 'boolean'], + 'categories.*' => ['numeric', 'exists:categories,id'], + 'url' => ['required', 'url'], + 'subject' => ['required', 'string'], + 'image' => ['nullable', 'image'], + 'approved' => ['nullable', 'boolean'], ]; } -} \ No newline at end of file +} diff --git a/app/Http/Requests/UpdateJoinedRequest.php b/app/Http/Requests/UpdateJoinedRequest.php index 75a2737..37d2c18 100644 --- a/app/Http/Requests/UpdateJoinedRequest.php +++ b/app/Http/Requests/UpdateJoinedRequest.php @@ -8,23 +8,24 @@ class UpdateJoinedRequest extends FormRequest { /** * Determine if the user is authorized to make this request. - * - * @return bool */ - public function authorize() + public function authorize() : bool { - return false; + return $this->user()->can('update', $this->route('joined')); } /** * Get the validation rules that apply to the request. - * - * @return array */ - public function rules() + public function rules() : array { return [ - // + 'categories' => ['required', 'array'], + 'categories.*' => ['numeric', 'exists:categories,id'], + 'url' => ['required', 'url'], + 'subject' => ['required', 'string'], + 'image' => ['nullable', 'image'], + 'approved' => ['nullable', 'boolean'], ]; } } diff --git a/app/Models/Joined.php b/app/Models/Joined.php index e24dd6f..29916d6 100644 --- a/app/Models/Joined.php +++ b/app/Models/Joined.php @@ -3,44 +3,81 @@ namespace App\Models; use App\Traits\Categorizable; +use App\Traits\Imageable; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Facades\Storage; class Joined extends Model { - use HasFactory, Categorizable; + use HasFactory; + use Categorizable; + use Imageable; - protected $table = 'joined'; + protected $table = 'joined'; - protected $casts = [ - 'approved' => 'boolean', - ]; + protected $fillable = [ + 'url', + 'subject', + 'approved', + 'image', + ]; -/* --------------------------------------------------------------------------- relationships ---- */ + protected $casts = [ + 'approved' => 'boolean', + ]; - public function collective() - { - return $this->belongsTo(Collective::class); - } + /* ----------------------------------------------------------------------- relationships ---- */ - // injected by trait: categories (morph many-to-many) + public function collective() + { + return $this->belongsTo(Collective::class); + } -/* ---------------------------------------------------------------------------------- joined ---- */ + // injected by trait: categories (morph many-to-many) - public static function store($request) : Joined - { - $joined = new Joined(); - $joined->url = $request['url']; - $joined->subject = $request['subject']; - $joined->image = Storage::putFile('joined', $request['image']); - $joined->approved = $request['approved'] ?? false; + /* -------------------------------------------------------------------------- attributes ---- */ - $collective = auth_collective(); - $collective->joined()->save($joined); + protected function approved() : Attribute + { + return Attribute::make( + set: fn ($value) => isset($value) ? $value = $value : $value = false, + ); + } - $joined->categories()->sync($request['categories']); + /* ------------------------------------------------------------------------------ joined ---- */ - return $joined; - } + public static function store(array $validated) : Joined + { + $validated['image'] = $validated['image'] ?? null; + $validated['image'] = self::imagePath($validated['image']); + $validated['approved'] = $validated['approved'] ?? false; + + $joined = auth_collective()->joined()->create($validated); + $joined->categories()->sync($validated['categories']); + + return $joined; + } + + /* ------------------------------------------------------------------------------- patch ---- */ + + public function patch(array $validated) : Joined + { + $validated['image'] = $validated['image'] ?? null; + + $this->url = $validated['url']; + $this->subject = $validated['subject']; + $this->image = $this->updateImage($validated['image']); + $this->approved = $validated['approved'] ?? false; + + if ($this->isDirty()) { + $this->save(); + } + + if (isset($validated['categories'])) { + $this->categories()->sync($validated['categories']); + } + + return $this; + } } diff --git a/app/Traits/Imageable.php b/app/Traits/Imageable.php new file mode 100644 index 0000000..4e0e02b --- /dev/null +++ b/app/Traits/Imageable.php @@ -0,0 +1,29 @@ +image) && isset($image)) { + Storage::delete($this->image); + self::imagePath($image); + } else { + return null; + } + } +} diff --git a/database/migrations/10_create_joined_table.php b/database/migrations/10_create_joined_table.php index 4b93b44..2fbeacb 100644 --- a/database/migrations/10_create_joined_table.php +++ b/database/migrations/10_create_joined_table.php @@ -4,33 +4,28 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class() extends Migration { /** * Run the migrations. - * - * @return void */ public function up() { Schema::create('joined', function (Blueprint $table) { $table->id(); $table->timestamps(); - $table->foreignId('collective_id') - ->constrained('collectives') - ->onUpdate('cascade') - ->onDelete('cascade'); - $table->string('url'); - $table->string('subject'); - $table->string('image'); - $table->boolean('approved'); + $table->foreignId('collective_id') + ->constrained('collectives') + ->onUpdate('cascade') + ->onDelete('cascade'); + $table->string('url'); + $table->string('subject'); + $table->string('image')->nullable(); + $table->boolean('approved'); }); } /** * Reverse the migrations. - * - * @return void */ public function down() { diff --git a/database/seeders/JoinedSeeder.php b/database/seeders/JoinedSeeder.php index 11e461a..397d4b7 100644 --- a/database/seeders/JoinedSeeder.php +++ b/database/seeders/JoinedSeeder.php @@ -4,21 +4,45 @@ use App\Models\Category; use App\Models\Joined; -use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; +use Illuminate\Support\Facades\DB; class JoinedSeeder extends Seeder { /** * Run the database seeds. - * - * @return void */ public function run() { Joined::factory() - ->count(50) - ->hasAttached(Category::inRandomOrder()->limit(rand(0,10))) - ->create(); + ->count(50) + ->create(); + + $pivots = []; + $cats = Category::inRandomOrder()->select('id')->get(); + + $i = 1; + + while ($i <= 50) { + $pivots[] = [ + 'category_id' => $cats->random()->id, + 'categorizable_id' => $i, + 'categorizable_type' => 'joined', + ]; + ++$i; + } + + $i = 1; + + while ($i <= rand(20, 100)) { + $pivots[] = [ + 'category_id' => $cats->random()->id, + 'categorizable_id' => rand(1, 50), + 'categorizable_type' => 'joined', + ]; + ++$i; + } + + DB::table('categorizables')->insert($pivots); } } diff --git a/resources/views/admin/joined/edit.blade.php b/resources/views/admin/joined/edit.blade.php index 553eff3..5454f7c 100644 --- a/resources/views/admin/joined/edit.blade.php +++ b/resources/views/admin/joined/edit.blade.php @@ -8,7 +8,7 @@ @section('content') -