Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions app/Console/Commands/ProjectInitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Console\Commands;

use App\Models\Category;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class ProjectInitCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'init:project';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
Artisan::call('migrate:refresh', [
'--force' => true,
]);
$this->info(Artisan::output());

$this->info('Make admin user');
User::factory()->create(['username' => 'MooseS94']);

$this->info('Add random categories');
Category::factory()->count(10)->create();
}
}
25 changes: 8 additions & 17 deletions app/Http/Controllers/AdminPostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Http\Requests\Admin\AdminPostStoreRequest;
use App\Models\Post;
use Illuminate\Validation\Rule;

Expand All @@ -12,28 +13,18 @@ public function create()
return view('admin.posts.create');
}

public function store()
public function store(AdminPostStoreRequest $request)
{
$data = $request->validated();

$attributes = request()->validate([
'title' => ['required', Rule::unique('posts', 'title')],
'slug' => '',
'thumbnail' => 'required|image',
'excerpt' => 'required',
'body' => 'required',
'category_id' => ['required', Rule::exists('categories', 'id')]
]);

$title = $attributes['title'];
$attributes['slug'] = $title;
$attributes['user_id'] = auth()->id();
$attributes['thumbnail'] = request()->file('thumbnail')->store('thumbnails');
$data = collect($data)->put('slug', $data['title'])
->put('thumbnail', request()->file('thumbnail')->store('thumbnails'))->toArray();

Post::create($attributes);
auth()->user()->posts()->create($data);

flash('New product has been submitted');

return redirect('/');
return redirect()->route('posts.index');
}

public function index()
Expand Down Expand Up @@ -83,4 +74,4 @@ public function destroy(Post $post)

return back();
}
}
}
10 changes: 6 additions & 4 deletions app/Http/Middleware/MustBeAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -11,17 +12,18 @@ class MustBeAdmin
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
* @param Request $request
* @param Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
//Need to change with roles and permmisions

if (auth()->user()?->username != 'MooseS94') {
abort(Response::HTTP_FORBIDDEN);
}

return $next($request);
}
}
}
36 changes: 36 additions & 0 deletions app/Http/Requests/Admin/AdminPostStoreRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AdminPostStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => ['required', Rule::unique('posts', 'title')],
'slug' => '',
'thumbnail' => 'required|image',
'excerpt' => 'required',
'body' => 'required',
'category_id' => ['required', Rule::exists('categories', 'id')]
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/Admin/AdminUpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;

class AdminUpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}
4 changes: 3 additions & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class Category extends Model
{
use HasFactory;

protected $fillable = ['name', 'slug'];

public function posts()
{
return $this->hasMany(Post::class);
}
}
}
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
route::get('admin/posts/create', [AdminPostController::class, 'create'])->middleware('admin');
route::post('admin/posts', [AdminPostController::class, 'store'])->middleware('admin');

route::get('admin/posts', [AdminPostController::class, 'index'])->middleware('admin');
route::get('admin/posts', [AdminPostController::class, 'index'])->middleware('admin')->name('posts.index');
route::get('admin/posts/{post}/edit', [AdminPostController::class, 'edit'])->middleware('admin');
route::patch('admin/posts/{post}', [AdminPostController::class, 'update'])->middleware('admin');
route::delete('admin/posts/{post}', [AdminPostController::class, 'destroy'])->middleware('admin');


require __DIR__ . '/auth.php';
require __DIR__ . '/auth.php';