@@ -26,6 +26,7 @@ class HomeController extends Controller
|
||||
* Show the application dashboard.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Support\Renderable
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
@@ -59,6 +60,65 @@ class HomeController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit today's entry
|
||||
*/
|
||||
public function edit(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'
|
||||
]);
|
||||
|
||||
$tags_to_delete = PostsTag::where('post_id', $today_post->id)->get();
|
||||
foreach ($tags_to_delete as $tag_to_delete) {
|
||||
$tag_to_delete->delete();
|
||||
}
|
||||
|
||||
$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),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
try {
|
||||
$today_post = Post::whereDate('date_post', '=', (new DateTime())->format('Y-m-d'))
|
||||
->where('user_id', Auth::user()->getAuthIdentifier())
|
||||
->firstOrFail();
|
||||
|
||||
$today_post->delete();
|
||||
return Redirect::to('home')->withSuccess('Great! Your today\'s post is now deleted. You can make a new one!');
|
||||
} catch (\ErrorException $e) {
|
||||
return Redirect::to('home')->withErrors('Oh no! We could\'t find your today\'s post.');
|
||||
}
|
||||
}
|
||||
|
||||
public function store(TagDetectorService $tag_detector)
|
||||
{
|
||||
$today = new DateTime();
|
||||
|
Reference in New Issue
Block a user