@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\AddPost;
|
||||
use App\Post;
|
||||
use App\PostsTag;
|
||||
use App\Services\ImageService;
|
||||
@@ -11,6 +12,9 @@ use DateTime;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\DiskDoesNotExist;
|
||||
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileDoesNotExist;
|
||||
use Spatie\MediaLibrary\Exceptions\FileCannotBeAdded\FileIsTooBig;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
@@ -72,39 +76,35 @@ class DashboardController extends Controller
|
||||
|
||||
/**
|
||||
* Edit today's entry
|
||||
* @param AddPost $request
|
||||
* @param TagDetectorService $tag_detector
|
||||
* @return
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function edit(TagDetectorService $tag_detector)
|
||||
public function edit(AddPost $request, TagDetectorService $tag_detector)
|
||||
{
|
||||
/** @var Post $today_post */
|
||||
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
|
||||
->where('user_id', Auth::user()->getAuthIdentifier())
|
||||
->firstOrFail();
|
||||
|
||||
$data = request()->validate([
|
||||
'message' => 'required'
|
||||
]);
|
||||
$data = $request->validated();
|
||||
|
||||
$today_post->deleteTags();
|
||||
|
||||
$tags = $tag_detector->detectFrom($data['message']);
|
||||
|
||||
$today_post->content = $data['message'];
|
||||
$today_post->save();
|
||||
$all_tags = Tag::all();
|
||||
$all_tags_names = [];
|
||||
foreach ($all_tags as $tag) {
|
||||
$all_tags_names[$tag->id] = $tag->name;
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (in_array($tag, $all_tags_names)) {
|
||||
PostsTag::create([
|
||||
'post_id' => $today_post->id,
|
||||
'tag_id' => array_search($tag, $all_tags_names),
|
||||
]);
|
||||
if (!empty($validated_data['file'])) {
|
||||
try {
|
||||
$today_post->addMediaFromRequest('file')->toMediaCollection('post_image');
|
||||
} catch (DiskDoesNotExist $e) {
|
||||
} catch (FileDoesNotExist $e) {
|
||||
} catch (FileIsTooBig $e) {
|
||||
}
|
||||
}
|
||||
|
||||
$today_post->deleteTags();
|
||||
$today_post->content = $data['message'];
|
||||
$today_post->save();
|
||||
|
||||
$this->setPostsTagsForPost($today_post, $tag_detector->detectFrom($data['message']));
|
||||
|
||||
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
||||
}
|
||||
|
||||
@@ -127,37 +127,42 @@ class DashboardController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AddPost $request
|
||||
* @param TagDetectorService $tag_detector
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function store(TagDetectorService $tag_detector, ImageService $image_service)
|
||||
public function store(AddPost $request, TagDetectorService $tag_detector)
|
||||
{
|
||||
$user_id = Auth::user()->getAuthIdentifier();
|
||||
$validated_data = $request->validated();
|
||||
|
||||
$validated_data = request()->validate([
|
||||
'message' => 'required',
|
||||
'file' => 'file|mimes:jpeg,png,jpg,gif,svg',
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'user_id' => $user_id,
|
||||
/** @var Post $post */
|
||||
$post = Post::create([
|
||||
'user_id' => Auth::user()->getAuthIdentifier(),
|
||||
'date_post' => new DateTime(),
|
||||
'content' => $validated_data['message'],
|
||||
];
|
||||
]);
|
||||
|
||||
if (!empty($validated_data['file'])) {
|
||||
/** @var UploadedFile $uploaded_document */
|
||||
$uploaded_document = $validated_data['file'];
|
||||
$file_name = $uploaded_document->getClientOriginalName();
|
||||
$uploaded_document->storeAs(Auth::user()->getFolder(), $file_name);
|
||||
$image_service->makeThumbnail($uploaded_document->getRealPath(), ['width' => 300]);
|
||||
$data['image'] = $file_name;
|
||||
try {
|
||||
$post->addMediaFromRequest('file')->toMediaCollection('post_image');
|
||||
} catch (DiskDoesNotExist $e) {
|
||||
} catch (FileDoesNotExist $e) {
|
||||
} catch (FileIsTooBig $e) {
|
||||
}
|
||||
}
|
||||
|
||||
$tags = $tag_detector->detectFrom($data['content']);
|
||||
$this->setPostsTagsForPost($post, $tag_detector->detectFrom($validated_data['message']));
|
||||
|
||||
$check = Post::create($data);
|
||||
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Post $post
|
||||
* @param $tags
|
||||
*/
|
||||
private function setPostsTagsForPost(Post $post, $tags)
|
||||
{
|
||||
$all_tags = Tag::all();
|
||||
$all_tags_names = [];
|
||||
foreach ($all_tags as $tag) {
|
||||
@@ -165,14 +170,12 @@ class DashboardController extends Controller
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (in_array($tag, $all_tags_names)) {
|
||||
if (in_array($tag, $all_tags_names, true)) {
|
||||
PostsTag::create([
|
||||
'post_id' => $check->id,
|
||||
'post_id' => $post->id,
|
||||
'tag_id' => array_search($tag, $all_tags_names),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user