Skip to content
Merged

Dev #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use App\Notifications\PostDeleted;
use App\Notifications\PostEdited;
use App\Post;
use App\Post_tag;
use App\PostTag;
use App\Tag;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class PostsController extends Controller
{
Expand All @@ -19,6 +19,16 @@ public function __construct()
$this->middleware('can:update,post')->except(['index', 'userPosts', 'adminIndex', 'create', 'store']);
}

public function validateRequest($request, $post)
{
return $request->validate([
'code' => ['required', 'regex:/[a-zA-Z0-9_-]+/', Rule::unique('posts')->ignore($post->id)],
'name' => 'required|min:5|max:100',
'description' => 'required|max:255',
'text' => 'required'
]);
}

public function index()
{
$posts = Post::with('tags')->latest()->get();
Expand All @@ -43,12 +53,7 @@ public function create()

public function store(Request $request)
{
$attr = $request->validate([
'code' => 'required|unique:posts|regex:/[a-zA-Z0-9_-]+/',
'name' => 'required|min:5|max:100',
'description' => 'required|max:255',
'text' => 'required'
]);
$attr = $this->validateRequest($request, new Post());

if ($request->has('published')) {
$attr['published'] = 1;
Expand All @@ -58,6 +63,14 @@ public function store(Request $request)

$post = Post::create($attr);

if (!is_null($request['tags'])) {
$requestTags = explode(', ', $request['tags']);
foreach ($requestTags as $tag) {
$tag = Tag::firstOrCreate(['name' => $tag]);
$post->tags()->attach($tag);
}
}

sendMailNotifyToAdmin(new PostCreated($post));
flash( 'Post created successfully');

Expand All @@ -76,11 +89,7 @@ public function edit(Post $post)

public function update(Request $request, Post $post)
{
$values = $request->validate([
'name' => 'required|min:5|max:100',
'description' => 'required|max:255',
'text' => 'required'
]);
$values = $this->validateRequest($request, $post);

$post->update($values);

Expand All @@ -105,7 +114,7 @@ public function update(Request $request, Post $post)
if ($deleteTags->isNotEmpty()) {
foreach ($deleteTags as $tag) {
$post->tags()->detach($tag);
$isLastTag = Post_tag::where('tag_id', $tag->id)->first();
$isLastTag = PostTag::where('tag_id', $tag->id)->first();
if (!$isLastTag) $tag->delete();
};
}
Expand Down
10 changes: 10 additions & 0 deletions app/PostTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class PostTag extends Pivot
{
protected $table = 'post_tag';
}
11 changes: 0 additions & 11 deletions app/Post_tag.php

This file was deleted.

3 changes: 2 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
];

public function posts() {
public function posts()
{
return $this->hasMany(Post::class, 'owner_id');
}
}
20 changes: 20 additions & 0 deletions app/View/Components/PostForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\View\Components;

use Illuminate\View\Component;

class PostForm extends Component
{
public $post;

public function __construct($post)
{
$this->post = $post;
}

public function render()
{
return view('components.post-form', ['post' => $this->post]);
}
}
3 changes: 1 addition & 2 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public function up()
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at');
$table->timestamps();
});
}

Expand Down
5 changes: 2 additions & 3 deletions database/migrations/2020_08_26_090725_create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ public function up()
$table->string('name', 100);
$table->string('description', 255);
$table->text('text');
$table->boolean('published')->default(0);
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->boolean('published')->default(false);
$table->timestamps();

$table->foreignId('owner_id')->constrained('users')->onDelete('cascade');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function up()
$table->id();
$table->string('email');
$table->text('message');
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->timestamps();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function up()
$table->id();
$table->string('email');
$table->text('text');
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->timestamps();
});
}

Expand Down
3 changes: 1 addition & 2 deletions database/migrations/2020_09_07_173528_create_tags_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public function up()
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->timestamps();
});
}

Expand Down
108 changes: 108 additions & 0 deletions resources/views/components/post-form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<div class="form__fields row d-flex flex-column">
<div class="form__field col-6 mb-3">
<label for="form-code">Code</label>
<input type="text"
class="form-control @error('code') is-invalid @enderror"
id="form-code"
name="code"
value="{{ old('code', $post->code) }}"
placeholder=""
required=""
>
<div class="invalid-feedback">
Code is required.
</div>

@error('code')
<div class="alert alert-danger">
{{ $message }}
</div>
@enderror
</div>

<div class="form__field col-6 mb-3">
<label for="form-name">Post Name</label>
<input type="text"
class="form-control @error('name') is-invalid @enderror"
id="form-name"
name="name"
value="{{ old('name', $post->name) }}"
required=""
>

<div class="invalid-feedback">
Post Name is required.
</div>

@error('name')
<div class="alert alert-danger">
{{ $message }}
</div>
@enderror
</div>

<div class="form__field col-6 mb-3">
<label for="form-description">Description</label>
<input type="text"
class="form-control @error('description') is-invalid @enderror"
id="form-description"
name="description"
value="{{ old('description', $post->description) }}"
required=""
>

<div class="invalid-feedback">
Post Description is required.
</div>

@error('description')
<div class="alert alert-danger">
{{ $message }}
</div>
@enderror
</div>

<div class="form__field d-flex flex-column col-12 mb-3">
<label for="form-text">Text</label>
<textarea name="text"
class="form-control @error('text') is-invalid @enderror"
id="form-text"
cols="30"
rows="10"
placeholder="Post content here"
required="">{{ old('text', $post->text) }}</textarea>

<div class="invalid-feedback">
Post Description is required.
</div>

@error('text')
<div class="alert alert-danger">
{{ $message }}
</div>
@enderror
</div>

<div class="form__field col-6 mb-3">
<label for="form-tags">Tags</label>
<input type="text"
class="form-control"
id="form-tags"
name="tags"
placeholder="tag1, tag2"
value="{{ old('tags', $post->tags->pluck('name')->implode(', ')) }}"
>
</div>

<div class="form__field form-check mb-2">
<input class="form__checkbox"
id="form-checkbox"
type="checkbox"
name="published"
@if(old('published', $post->published)) checked @endif
>
<label class="form-check-label" for="form-checkbox">
Published
</label>
</div>
</div>
8 changes: 4 additions & 4 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
@foreach($posts as $post)
<div class="post__item">
<div class="post__intro text-break row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
<div class="post__heading col-12 col-lg-6 p-4 d-flex flex-column">
<div class="post__heading col-12 col-lg-6 p-4 d-flex flex-column" style="height: 280px;">
<strong class="d-inline-block mb-2 text-primary">Post #{{ $post->id }}</strong>

<h3 class="post__name mb-0">{{ $post->name }}</h3>

<div class="post__created-at mb-1 text-muted">{{ $post->created_at->toFormattedDateString() }}</div>

<p class="post__preview card-text mb-auto text-justify" style="height: 115px"> {{ str_limit($post->text, $limit = 100, $end = '...') }} </p>
<p class="post__preview card-text mb-auto text-justify"> {{ str_limit($post->text, $limit = 100, $end = '...') }} </p>

@if($post->tags->isNotEmpty())
<div class="post__tags mb-2">
@foreach($post->tags as $tag)
<a href="/tags/{{ $tag->name }}" class="badge badge-info text-white">{{ $tag->name }}</a>
<a href="{{ route('tags.show', $tag) }}" class="badge badge-info text-white">{{ $tag->name }}</a>
@endforeach
</div>
@endif

<a href="{{ route('post-show', $post->id) }}" class="post__view">Continue reading</a>
<a href="{{ route('posts.show', $post->id) }}" class="post__view">Continue reading</a>
</div>

<div class="post__photo col-6 d-none d-lg-flex align-items-center p-2">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/layouts/base/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
<!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="{{ route('user-posts') }}">{{ __('My posts') }}</a>
<a class="nav-link" href="{{ route('user.posts') }}">{{ __('My posts') }}</a>
</li>

<li class="nav-item">
<a class="nav-link" href="{{ route('contacts') }}">{{ __('Contacts') }}</a>
</li>

<li class="nav-item">
<a class="nav-link" href="{{ route('post-create') }}">{{ __('Create Post') }}</a>
<a class="nav-link" href="{{ route('posts.create') }}">{{ __('Create Post') }}</a>
</li>

<li class="nav-item">
Expand Down
8 changes: 4 additions & 4 deletions resources/views/posts/admin-index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
@foreach($posts as $post)
<div class="post__item col-md-6 ">
<div class="post__intro text-break row py-2 no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
<div class="post__heading col-6 p-4 d-flex flex-column position-static">
<div class="post__heading col-6 p-4 d-flex flex-column position-static" style="height: 280px;">
<strong class="d-inline-block mb-2 text-primary">Post #{{ $post->id }}</strong>

<h3 class="post__name mb-0">{{ $post->name }}</h3>

<div class="post__created-at mb-1 text-muted">{{ $post->created_at->toFormattedDateString() }}</div>

<p class="post__preview card-text mb-auto text-justify" style="height: 115px"> {{ str_limit($post->text, $limit = 100, $end = '...') }} </p>
<p class="post__preview card-text mb-auto text-justify"> {{ str_limit($post->text, $limit = 100, $end = '...') }} </p>

<a href="{{ route('post-show', $post->id) }}" class="post__view">Continue reading</a>
<a href="{{ route('posts.show', $post->id) }}" class="post__view">Continue reading</a>
</div>

<div class="post__photo col-6 d-none d-lg-flex align-items-center p-2">
Expand All @@ -38,7 +38,7 @@
<div class="d-flex col-12 justify-content-end pr-2">
<a href="/posts/{{ $post->id }}/edit" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Edit</a>

<form method="post" action="/posts/{{ $post->id }}">
<form method="post" action="{{ route('posts.destroy') }}">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Delete</button>
Expand Down
Loading