🎨 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;
use Illuminate\Http\Request;
use App\Post;
use DateTime;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
@ -26,11 +27,20 @@ class HomeController extends Controller
public function index()
{
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', [
'already' => $already,
'today' => $today,
'posts' => $posts,
]);
@ -38,17 +48,19 @@ class HomeController extends Controller
public function store()
{
$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 ?
'date_post' => new DateTime(), // Take back the date from the form ?
'content' => $data['message'],
];
$check = \App\Post::create($data);
$check = Post::create($data);
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 = [
];
/**
* 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="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
@if (session('status'))
@ -15,6 +15,9 @@
@endif
<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">
@csrf
<div class="contact-form">
@ -32,9 +35,10 @@
</div>
</div>
</form>
@endif
@foreach($posts as $post)
<div class="post">
<p>{{ $post->date_post }}</p>
<p>{{ \Carbon\Carbon::instance($post->date_post)->diffForHumans() }}</p>
<p>{{ $post->content }}</p>
</div>
@endforeach

View File

@ -1,100 +1,40 @@
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@extends('layouts.app')
<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 -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<!-- Styles -->
<style>
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>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
@endauth
</div>
@endif
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="links">
<a href="https://laravel.com/docs">Docs</a>
<a href="https://laracasts.com">Laracasts</a>
<a href="https://laravel-news.com">News</a>
<a href="https://blog.laravel.com">Blog</a>
<a href="https://nova.laravel.com">Nova</a>
<a href="https://forge.laravel.com">Forge</a>
<a href="https://vapor.laravel.com">Vapor</a>
<a href="https://github.com/laravel/laravel">GitHub</a>
<h1>{{ strftime('%B %G', $today->format('U')) }}</h1>
<form action="{{ url('store') }}" method="post" accept-charset="utf-8">
@csrf
<div class="contact-form">
<div class="form-group">
<label class="control-label col-sm-5" for="message">Quoi de neuf aujourd'hui ?</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">J'enregistre ma journée</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
</div>
@endsection

View File

@ -11,11 +11,8 @@
|
*/
Route::get('/', function () {
return view('welcome');
});
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');