🎨 Empêche 2 posts par jour et améliore l'affichage

This commit is contained in:
Shikiryu 2019-09-25 15:20:22 +02:00
parent 31dc160ada
commit e6527a72af
6 changed files with 82 additions and 105 deletions

View File

@ -2,7 +2,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Post;
use DateTime;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Redirect;
@ -26,11 +27,20 @@ class HomeController extends Controller
public function index() public function index()
{ {
setlocale(LC_TIME, 'fr_FR.utf8'); setlocale(LC_TIME, 'fr_FR.utf8');
$today = new \DateTime(); $today = new DateTime();
$user_id = Auth::user()->getAuthIdentifier();
$posts = Post::where('user_id', $user_id)->orderBy('date_post', 'DESC')->get();
$today_post = Post::whereDate('date_post', '=', $today->format('Y-m-d'))
->where('user_id', $user_id)
->count();
$posts = \App\Post::where('user_id', Auth::user()->getAuthIdentifier())->get(); $already = false;
if ($today_post > 0) {
$already = true;
}
return view('home', [ return view('home', [
'already' => $already,
'today' => $today, 'today' => $today,
'posts' => $posts, 'posts' => $posts,
]); ]);
@ -38,17 +48,19 @@ class HomeController extends Controller
public function store() public function store()
{ {
$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'],
]; ];
$check = \App\Post::create($data); $check = Post::create($data);
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,15 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class WelcomeController extends Controller
{
public function index()
{
return view('welcome', [
'today' => new \DateTime(),
]);
}
}

View File

@ -23,4 +23,13 @@ class Post extends Model
protected $hidden = [ protected $hidden = [
]; ];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'date_post' => 'datetime',
];
} }

View File

@ -5,7 +5,7 @@
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-8"> <div class="col-md-8">
<div class="card"> <div class="card">
<div class="card-header">Dashboard</div> <div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body"> <div class="card-body">
@if (session('status')) @if (session('status'))
@ -15,6 +15,9 @@
@endif @endif
<h1>{{ strftime('%B %G', $today->format('U')) }}</h1> <h1>{{ strftime('%B %G', $today->format('U')) }}</h1>
@if($already)
<div class="alert alert-info" role="alert">L'activité du jour a été entrée.</div>
@else
<form action="{{ url('store') }}" method="post" accept-charset="utf-8"> <form action="{{ url('store') }}" method="post" accept-charset="utf-8">
@csrf @csrf
<div class="contact-form"> <div class="contact-form">
@ -32,9 +35,10 @@
</div> </div>
</div> </div>
</form> </form>
@endif
@foreach($posts as $post) @foreach($posts as $post)
<div class="post"> <div class="post">
<p>{{ $post->date_post }}</p> <p>{{ \Carbon\Carbon::instance($post->date_post)->diffForHumans() }}</p>
<p>{{ $post->content }}</p> <p>{{ $post->content }}</p>
</div> </div>
@endforeach @endforeach

View File

@ -1,100 +1,40 @@
<!DOCTYPE html> @extends('layouts.app')
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title> @section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Accueil') }}</div>
<!-- Fonts --> <div class="card-body">
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> @if (session('status'))
<div class="alert alert-success" role="alert">
<!-- Styles --> {{ session('status') }}
<style> </div>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.full-height {
height: 100vh;
}
.flex-center {
align-items: center;
display: flex;
justify-content: center;
}
.position-ref {
position: relative;
}
.top-right {
position: absolute;
right: 10px;
top: 18px;
}
.content {
text-align: center;
}
.title {
font-size: 84px;
}
.links > a {
color: #636b6f;
padding: 0 25px;
font-size: 13px;
font-weight: 600;
letter-spacing: .1rem;
text-decoration: none;
text-transform: uppercase;
}
.m-b-md {
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
@if (Route::has('login'))
<div class="top-right links">
@auth
<a href="{{ url('/home') }}">Home</a>
@else
<a href="{{ route('login') }}">Login</a>
@if (Route::has('register'))
<a href="{{ route('register') }}">Register</a>
@endif @endif
@endauth
</div>
@endif
<div class="content"> <h1>{{ strftime('%B %G', $today->format('U')) }}</h1>
<div class="title m-b-md"> <form action="{{ url('store') }}" method="post" accept-charset="utf-8">
Laravel @csrf
</div> <div class="contact-form">
<div class="form-group">
<div class="links"> <label class="control-label col-sm-5" for="message">Quoi de neuf aujourd'hui ?</label>
<a href="https://laravel.com/docs">Docs</a> <div class="col-sm-10">
<a href="https://laracasts.com">Laracasts</a> <textarea class="form-control" rows="5" name="message" id="message"></textarea>
<a href="https://laravel-news.com">News</a> <span class="text-danger">{{ $errors->first('message') }}</span>
<a href="https://blog.laravel.com">Blog</a> </div>
<a href="https://nova.laravel.com">Nova</a> </div>
<a href="https://forge.laravel.com">Forge</a> <div class="form-group">
<a href="https://vapor.laravel.com">Vapor</a> <div class="col-sm-offset-2 col-sm-10">
<a href="https://github.com/laravel/laravel">GitHub</a> <button type="submit" class="btn btn-default">J'enregistre ma journée</button>
</div>
</div>
</div>
</form>
</div>
</div> </div>
</div> </div>
</div> </div>
</body> </div>
</html> @endsection

View File

@ -11,11 +11,8 @@
| |
*/ */
Route::get('/', function () {
return view('welcome');
});
Auth::routes(); Auth::routes();
Route::get('/home', 'HomeController@index')->name('home'); Route::get('/', 'WelcomeController@index')->name('home');
Route::get('/home', 'HomeController@index')->name('dashboard');
Route::post('store', 'HomeController@store')->name('store'); Route::post('store', 'HomeController@store')->name('store');