💥 Ajoute les tags en base et les formate

Pour #1
This commit is contained in:
cdesmidt 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; namespace App\Http\Controllers;
use App\Post; use App\Post;
use App\PostsTag;
use App\Services\TagDetectorService;
use App\Tag;
use DateTime; use DateTime;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Redirect;
@ -33,35 +36,51 @@ class HomeController extends Controller
$today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d')) $today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d'))
->where('user_id', $user_id) ->where('user_id', $user_id)
->count(); ->count();
$already = false; $already = false;
if ($today_post > 0) { if ($today_post > 0) {
$already = true; $already = true;
} }
return view('home', [ return view('home', [
'already' => $already, 'already' => $already,
'today' => $today, 'today' => $today,
'posts' => $posts, 'posts' => $posts,
]); ]);
} }
public function store() public function store(TagDetectorService $tag_detector)
{ {
$today = new DateTime(); $today = new DateTime();
$data = request()->validate([ $data = request()->validate([
'message' => 'required' 'message' => 'required'
]); ]);
$data = [ $data = [
'user_id' => Auth::user()->getAuthIdentifier(), 'user_id' => Auth::user()->getAuthIdentifier(),
'date_post' => new DateTime(), // Take back the date from the form ? 'date_post' => new DateTime(), // Take back the date from the form ?
'content' => $data['message'], 'content' => $data['message'],
]; ];
$tags = $tag_detector->detectFrom($data['content']);
$check = Post::create($data); $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.'); 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;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Providers;
use App\Services\TagDetectorService;
use Illuminate\Support\ServiceProvider;
class TagDetectorProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->app->bind(TagDetectorService::class);
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace App\Services;
class TagDetectorService
{
public const REGEX_FIND = '/\s#([^\s]+)(?:\s|$)/m';
public const REGEX_REPLACE = '/\s(#[^\s]+)(?:\s|$)/m';
/**
* @param string $text
*
* @return array
*/
public function detectFrom($text)
{
if (preg_match_all(self::REGEX_FIND, $text, $tags) !== false) {
return $tags[1];
}
return [];
}
/**
* @param string $text
*
* @return string|string[]|null
*/
public function linkTagFrom($text)
{
return preg_replace(
self::REGEX_FIND, str_replace('$1', '$1', sprintf(' <a href="%s/%s">%s</a> ', url('stats'), '$1', '#$1')),
$text
);
}
}

View File

@ -175,6 +175,8 @@ return [
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
\App\Providers\TagDetectorProvider::class,
], ],
/* /*

View File

@ -1,5 +1,7 @@
@extends('layouts.app') @extends('layouts.app')
@inject('tag_detector', 'App\Services\TagDetectorService)
@section('content') @section('content')
<div class="container"> <div class="container">
<div class="row justify-content-center"> <div class="row justify-content-center">
@ -39,7 +41,7 @@
@foreach($posts as $post) @foreach($posts as $post)
<div class="post"> <div class="post">
<p>{{ \Carbon\Carbon::instance($post->date_post)->diffForHumans() }}</p> <p>{{ \Carbon\Carbon::instance($post->date_post)->diffForHumans() }}</p>
<p>{{ $post->content }}</p> <p>{!! $tag_detector->linkTagFrom($post->content) !!}</p>
</div> </div>
@endforeach @endforeach
</div> </div>

View File

@ -16,3 +16,4 @@ Auth::routes();
Route::get('/', 'WelcomeController@index')->name('home'); Route::get('/', 'WelcomeController@index')->name('home');
Route::get('/home', 'HomeController@index')->name('dashboard'); Route::get('/home', 'HomeController@index')->name('dashboard');
Route::post('store', 'HomeController@store')->name('store'); Route::post('store', 'HomeController@store')->name('store');
Route::post('stats', 'StatsController@tag')->name('stats'); // TODO make a group for stats