-
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 #23 from pashaapsky/dev
Dev
- Loading branch information
Showing
56 changed files
with
1,748 additions
and
330 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,20 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Comment extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'comments'; | ||
|
||
protected $guarded = ['']; | ||
|
||
public function commentable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Post; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class PostUpdated | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public Post $post; | ||
|
||
public function __construct(Post $post) | ||
{ | ||
$this->post = $post; | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class History extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'histories'; | ||
|
||
protected $guarded = ['']; | ||
} |
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,46 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Comment; | ||
use App\News; | ||
use App\Post; | ||
use Illuminate\Http\Request; | ||
|
||
class CommentsController extends Controller | ||
{ | ||
public function newsCommentStore(Request $request, News $new) | ||
{ | ||
$values = $request->validate([ | ||
'text' => 'required' | ||
]); | ||
|
||
$new->comments()->create($values); | ||
|
||
flash('Comment create successfully'); | ||
|
||
return back(); | ||
} | ||
|
||
public function postsCommentStore(Request $request, Post $post) | ||
{ | ||
$values = $request->validate([ | ||
'text' => 'required' | ||
]); | ||
|
||
$post->comments()->create($values); | ||
|
||
flash('Comment create successfully'); | ||
|
||
return back(); | ||
} | ||
|
||
public function destroy(Comment $comment) | ||
{ | ||
$comment->delete(); | ||
|
||
flash('Comment delete successfully'); | ||
|
||
return back(); | ||
} | ||
} |
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,93 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\News; | ||
use App\NewTag; | ||
use App\Services\TagsCreatorService; | ||
use App\Tag; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
|
||
class NewsController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth'); | ||
$this->middleware('role:admin')->except(['index', 'show']); | ||
} | ||
|
||
public function validateRequest($request, $new) | ||
{ | ||
return $request->validate([ | ||
'name' => ['required', 'max:100', Rule::unique('news')->ignore($new->id)], | ||
'text' => 'required' | ||
]); | ||
} | ||
|
||
public function index() | ||
{ | ||
$news = News::with(['tags', 'comments'])->latest()->get(); | ||
|
||
return view('news.index', compact('news')); | ||
} | ||
|
||
public function create() | ||
{ | ||
return view('news.create'); | ||
} | ||
|
||
public function store(Request $request) | ||
{ | ||
$values = $this->validateRequest($request, new News()); | ||
|
||
$new = News::create($values); | ||
|
||
if (!is_null($request['tags'])) { | ||
$requestTags = explode(', ', $request['tags']); | ||
foreach ($requestTags as $tag) { | ||
$tag = Tag::firstOrCreate(['name' => $tag]); | ||
$new->tags()->attach($tag); | ||
} | ||
} | ||
|
||
flash('New created successfully'); | ||
|
||
return back(); | ||
} | ||
|
||
public function show(News $new) | ||
{ | ||
return view('news.show', compact('new')); | ||
} | ||
|
||
public function edit(News $new) | ||
{ | ||
return view('news.edit', compact('new')); | ||
} | ||
|
||
public function update(Request $request, News $new) | ||
{ | ||
$values = $this->validateRequest($request, $new); | ||
|
||
$new->update($values); | ||
|
||
$updater = new TagsCreatorService($new, $request); | ||
$updater->updateTags(); | ||
|
||
// updateTags($new, $request); | ||
|
||
flash( 'New updated successfully'); | ||
|
||
return back(); | ||
} | ||
|
||
public function destroy(News $new) | ||
{ | ||
$new->delete(); | ||
|
||
flash( 'New deleted successfully'); | ||
|
||
return redirect('/admin/news'); | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\PostUpdated; | ||
|
||
class AddHistoryOnUpdatePost | ||
{ | ||
public function __construct() | ||
{ | ||
|
||
} | ||
|
||
public function handle(PostUpdated $event) | ||
{ | ||
$result = ''; | ||
|
||
$dirtyValues = $event->post->getChanges(); | ||
$freshValues = $event->post->getOriginal(); | ||
unset($dirtyValues['updated_at']); | ||
|
||
if ($dirtyValues['published'] === true && $freshValues['published'] === 1) { | ||
unset($dirtyValues['published']); | ||
} | ||
|
||
foreach ($dirtyValues as $key => $field) { | ||
$result .= 'Изменено поле: "' . $key . '". Было: "' . $freshValues[$key] . '". Стало: "' . $field . '"' . PHP_EOL; | ||
} | ||
|
||
$values = [ | ||
'user_email' => auth()->user()->email, | ||
'text' => $result | ||
]; | ||
|
||
$event->post->history()->create($values); | ||
} | ||
} |
Oops, something went wrong.