programs
This commit is contained in:
parent
494fe698b0
commit
a3bebf3976
8 changed files with 203 additions and 0 deletions
51
app/Http/Controllers/ProgramController.php
Normal file
51
app/Http/Controllers/ProgramController.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ProgramRequest;
|
||||
use App\Models\Program;
|
||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||
|
||||
class ProgramController extends Controller
|
||||
{
|
||||
use AuthorizesRequests;
|
||||
|
||||
public function index()
|
||||
{
|
||||
$this->authorize('viewAny', Program::class);
|
||||
|
||||
return Program::all();
|
||||
}
|
||||
|
||||
public function store(ProgramRequest $request)
|
||||
{
|
||||
$this->authorize('create', Program::class);
|
||||
|
||||
return Program::create($request->validated());
|
||||
}
|
||||
|
||||
public function show(Program $program)
|
||||
{
|
||||
$this->authorize('view', $program);
|
||||
|
||||
return $program;
|
||||
}
|
||||
|
||||
public function update(ProgramRequest $request, Program $program)
|
||||
{
|
||||
$this->authorize('update', $program);
|
||||
|
||||
$program->update($request->validated());
|
||||
|
||||
return $program;
|
||||
}
|
||||
|
||||
public function destroy(Program $program)
|
||||
{
|
||||
$this->authorize('delete', $program);
|
||||
|
||||
$program->delete();
|
||||
|
||||
return response()->json();
|
||||
}
|
||||
}
|
23
app/Http/Requests/ProgramRequest.php
Normal file
23
app/Http/Requests/ProgramRequest.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ProgramRequest extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required'],
|
||||
'category_id' => ['required', 'exists:categories'],
|
||||
'description' => ['nullable'],
|
||||
'website' => ['nullable'],
|
||||
];
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Category extends Model
|
||||
|
@ -13,4 +14,9 @@ class Category extends Model
|
|||
protected $fillable = [
|
||||
'name',
|
||||
];
|
||||
|
||||
public function programs(): HasMany
|
||||
{
|
||||
return $this->hasMany(Program::class);
|
||||
}
|
||||
}
|
||||
|
|
26
app/Models/Program.php
Normal file
26
app/Models/Program.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Program extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'category_id',
|
||||
'description',
|
||||
'website',
|
||||
];
|
||||
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
}
|
41
app/Policies/ProgramPolicy.php
Normal file
41
app/Policies/ProgramPolicy.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Program;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class ProgramPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function view(User $user, Program $program): bool
|
||||
{
|
||||
}
|
||||
|
||||
public function create(User $user): bool
|
||||
{
|
||||
}
|
||||
|
||||
public function update(User $user, Program $program): bool
|
||||
{
|
||||
}
|
||||
|
||||
public function delete(User $user, Program $program): bool
|
||||
{
|
||||
}
|
||||
|
||||
public function restore(User $user, Program $program): bool
|
||||
{
|
||||
}
|
||||
|
||||
public function forceDelete(User $user, Program $program): bool
|
||||
{
|
||||
}
|
||||
}
|
23
database/factories/ProgramFactory.php
Normal file
23
database/factories/ProgramFactory.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\Program;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class ProgramFactory extends Factory
|
||||
{
|
||||
protected $model = Program::class;
|
||||
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->faker->word(),
|
||||
'description' => $this->faker->text(),
|
||||
'website' => $this->faker->url(),
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Category;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('programs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->foreignIdFor(Category::class)->constrained('categories');
|
||||
$table->text('description')->nullable();
|
||||
$table->string('website')->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('programs');
|
||||
}
|
||||
};
|
|
@ -3,6 +3,7 @@
|
|||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Program;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
|
@ -24,5 +25,10 @@ public function run(): void
|
|||
]);
|
||||
|
||||
Category::factory(20)->create();
|
||||
Category::factory(10)->trashed()->create();
|
||||
|
||||
$categories = Category::all();
|
||||
Program::factory(40)->recycle($categories)->create();
|
||||
Program::factory(20)->recycle($categories)->trashed()->create();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue