-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Vitaliy1995/develop/hw-6.4
Домашняя работа 6.4
- Loading branch information
Showing
18 changed files
with
415 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Article; | ||
use App\Http\Requests\ArticlePostRequest; | ||
use App\Services\TagsSynchronizer; | ||
use App\Tag; | ||
use Illuminate\Http\Request; | ||
|
||
class AdminPostsController extends Controller | ||
{ | ||
private $tagsSynchronizer; | ||
|
||
public function __construct(TagsSynchronizer $tagsSynchronizer) | ||
{ | ||
$this->middleware('auth'); | ||
$this->middleware('can:admin-panel'); | ||
|
||
$this->tagsSynchronizer = $tagsSynchronizer; | ||
} | ||
|
||
public function index() | ||
{ | ||
$articles = Article::with('tags') | ||
->latest("updated_at") | ||
->get(); | ||
|
||
return view("admin.posts.index", compact('articles')); | ||
} | ||
|
||
public function create() | ||
{ | ||
return view('admin.posts.create'); | ||
} | ||
|
||
public function store(ArticlePostRequest $request) | ||
{ | ||
$validatedData = $request->validated(); | ||
|
||
if (\request()->has('published')) { | ||
$validatedData['published'] = true; | ||
} | ||
|
||
$validatedData['owner_id'] = auth()->id(); | ||
|
||
$article = Article::create($validatedData); | ||
|
||
$this->tagsSynchronizer->sync($article, \request('tags')); | ||
|
||
flash('Статья создана успешно!'); | ||
|
||
return redirect(route('admin.posts.index')); | ||
} | ||
|
||
public function show(Article $article) | ||
{ | ||
return view("admin.posts.detail", compact('article')); | ||
} | ||
|
||
public function edit(Article $article) | ||
{ | ||
return view("admin.posts.edit", compact('article')); | ||
} | ||
|
||
public function update(Article $article, ArticlePostRequest $request) | ||
{ | ||
$validatedData = $request->validated(); | ||
|
||
$validatedData['published'] = \request()->has('published'); | ||
|
||
$article->update($validatedData); | ||
|
||
$this->tagsSynchronizer->sync($article, \request('tags')); | ||
|
||
flash('Статья успешно обновлена!'); | ||
|
||
return redirect(route('admin.posts.index')); | ||
} | ||
|
||
public function destroy(Article $article) | ||
{ | ||
$article->delete(); | ||
|
||
flash('Статья успешно удалена!', 'warning'); | ||
|
||
return redirect(route('admin.posts.index')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Role extends Model | ||
{ | ||
const ADMIN = 1; | ||
const BASE = 2; | ||
|
||
protected $guarded = []; | ||
|
||
public function users() | ||
{ | ||
return $this->belongsToMany(User::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
|
||
namespace App\Services; | ||
|
||
|
||
use App\Article; | ||
use App\Tag; | ||
use Illuminate\Support\Collection; | ||
|
||
class TagsSynchronizer | ||
{ | ||
public function sync(Article $article, $tags) | ||
{ | ||
/** @var Collection $articleTags */ | ||
$articleTags = $article->tags->keyBy('name'); | ||
$formTags = collect(explode(",", $tags))->keyBy(function ($item) {return $item;}); | ||
|
||
$syncIds = $articleTags->intersectByKeys($formTags)->pluck('id')->toArray(); | ||
|
||
$tagsToAttach = $formTags->diffKeys($articleTags); | ||
foreach ($tagsToAttach as $tag) { | ||
$tag = Tag::firstOrCreate(['name' => $tag]); | ||
$syncIds[] = $tag->id; | ||
} | ||
|
||
$article->tags()->sync($syncIds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
database/migrations/2021_10_18_094319_create_roles_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateRolesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('roles', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name')->unique(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('roles'); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/2021_10_18_094445_create_role_user_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateRoleUserTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('role_user', function (Blueprint $table) { | ||
$table->unsignedBigInteger('user_id'); | ||
$table->unsignedBigInteger('role_id'); | ||
$table->primary(['user_id', 'role_id']); | ||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); | ||
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('role_user'); | ||
} | ||
} |
Oops, something went wrong.