💥 Ajoute les tags en base et les formate

Pour #1
This commit is contained in:
2019-09-30 16:32:40 +02:00
parent a7715c8460
commit a9cd4ed610
7 changed files with 112 additions and 9 deletions

View File

@@ -3,6 +3,9 @@
namespace App\Http\Controllers;
use App\Post;
use App\PostsTag;
use App\Services\TagDetectorService;
use App\Tag;
use DateTime;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
@@ -33,35 +36,51 @@ class HomeController extends Controller
$today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d'))
->where('user_id', $user_id)
->count();
$already = false;
if ($today_post > 0) {
$already = true;
}
return view('home', [
'already' => $already,
'today' => $today,
'posts' => $posts,
]);
}
public function store()
public function store(TagDetectorService $tag_detector)
{
$today = new DateTime();
$data = request()->validate([
'message' => 'required'
]);
$data = [
'user_id' => Auth::user()->getAuthIdentifier(),
'date_post' => new DateTime(), // Take back the date from the form ?
'content' => $data['message'],
];
$tags = $tag_detector->detectFrom($data['content']);
$check = Post::create($data);
$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' => $check->id,
'tag_id' => array_search($tag, $all_tags_names),
]);
}
}
return Redirect::to('home')->withSuccess('Great! Form successfully submit with validation.');
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StatsController extends Controller
{
public function tag($tag = null)
{
// TODO
exit;
}
}