Skip to content

Commit 74c6e87

Browse files
committed
edit frontend
1 parent ae1362c commit 74c6e87

File tree

13 files changed

+202
-177
lines changed

13 files changed

+202
-177
lines changed

app/Http/Controllers/NewsController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33
namespace App\Http\Controllers;
44

55
use App\News;
6+
use App\NewTag;
7+
use App\Tag;
68
use Illuminate\Http\Request;
79
use Illuminate\Validation\Rule;
810

911
class NewsController extends Controller
1012
{
13+
public function __construct()
14+
{
15+
$this->middleware('role:admin')->except(['index', 'show']);
16+
}
17+
1118
public function validateRequest($request, $new)
1219
{
1320
return $request->validate([
@@ -32,6 +39,15 @@ public function store(Request $request)
3239
$values = $this->validateRequest($request, new News());
3340

3441
$new = News::create($values);
42+
43+
if (!is_null($request['tags'])) {
44+
$requestTags = explode(', ', $request['tags']);
45+
foreach ($requestTags as $tag) {
46+
$tag = Tag::firstOrCreate(['name' => $tag]);
47+
$new->tags()->attach($tag);
48+
}
49+
}
50+
3551
flash('New created successfully');
3652

3753
return back();
@@ -52,6 +68,11 @@ public function update(Request $request, News $new)
5268
$values = $this->validateRequest($request, $new);
5369

5470
$new->update($values);
71+
72+
$newTags = $new->tags->keyBy('name');
73+
74+
updateTags($new, $request, NewTag::class);
75+
5576
flash( 'New updated successfully');
5677

5778
return back();

app/Http/Controllers/PostsController.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ public function validateRequest($request, $post)
2929
}
3030

3131
public function index()
32-
{
33-
$posts = Post::with('tags')->where('published', 1)->latest()->get();
34-
return view('/index', compact('posts'));
35-
}
36-
37-
public function userPosts()
3832
{
3933
$posts = auth()->user()->posts()->with('tags')->latest()->get();
4034
return view('/posts.index', compact('posts'));
@@ -96,31 +90,7 @@ public function update(Request $request, Post $post)
9690

9791
$post->update($values);
9892

99-
$postTags = $post->tags->keyBy('name');
100-
101-
if (!is_null($request['tags'])) {
102-
$requestTags = collect(explode(', ', $request['tags']))->keyBy(function ($item) { return $item; });
103-
} else {
104-
$requestTags = collect([]);
105-
}
106-
107-
$deleteTags = $postTags->diffKeys($requestTags);
108-
$addTags = $requestTags->diffKeys($postTags);
109-
110-
if ($addTags->isNotEmpty()) {
111-
foreach ($addTags as $tag) {
112-
$tag = Tag::firstOrCreate(['name' => $tag]);
113-
$post->tags()->attach($tag);
114-
};
115-
}
116-
117-
if ($deleteTags->isNotEmpty()) {
118-
foreach ($deleteTags as $tag) {
119-
$post->tags()->detach($tag);
120-
$isLastTag = PostTag::where('tag_id', $tag->id)->first();
121-
if (!$isLastTag) $tag->delete();
122-
};
123-
}
93+
updateTags($post, $request, PostTag::class);
12494

12595
sendMailNotifyToAdmin(new PostEdited($post));
12696
flash( 'Post edited successfully');

app/helpers.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use App\Tag;
4+
35
if (! function_exists('flash')) {
46
/**
57
* @param $message
@@ -43,3 +45,40 @@ function pushNotification($text = null, $title = null) {
4345
return app(\App\Services\PushNotificationsService::class)->send($text, $title);
4446
}
4547
}
48+
49+
if (! function_exists('updateTags')) {
50+
/**
51+
* @param $model
52+
* @param $request
53+
* @param $pivotTable
54+
*/
55+
56+
function updateTags($model, $request, $pivotTable) {
57+
$modelTags = $model->tags->keyBy('name');
58+
59+
if (!is_null($request['tags'])) {
60+
$requestTags = collect(explode(', ', $request['tags']))->keyBy(function ($item) { return $item; });
61+
} else {
62+
$requestTags = collect([]);
63+
}
64+
65+
$deleteTags = $modelTags->diffKeys($requestTags);
66+
$addTags = $requestTags->diffKeys($modelTags);
67+
68+
if ($addTags->isNotEmpty()) {
69+
foreach ($addTags as $tag) {
70+
$tag = Tag::firstOrCreate(['name' => $tag]);
71+
$model->tags()->attach($tag);
72+
};
73+
}
74+
75+
if ($deleteTags->isNotEmpty()) {
76+
foreach ($deleteTags as $tag) {
77+
$model->tags()->detach($tag);
78+
$isLastTag = $pivotTable::where('tag_id', $tag->id)->first();
79+
if (!$isLastTag) $tag->delete();
80+
};
81+
}
82+
}
83+
}
84+

resources/views/admin/news.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
@endsection
66

77
@section('content')
8-
@include('layouts.news.news-section')
8+
<main class="py-4" style="min-height: 88vh">
9+
<div class="container">
10+
<div class="intro row">
11+
@include('layouts.news.news-section')
12+
@include('layouts.aside-tags')
13+
</div>
14+
</div>
15+
</main>
916
@endsection
1017

1118
@section('footer')

resources/views/admin/posts.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
@endsection
66

77
@section('content')
8-
@include('layouts.posts.posts-section')
8+
<main class="py-4" style="min-height: 88vh">
9+
<div class="container">
10+
<div class="intro row">
11+
@include('layouts.posts.posts-section')
12+
@include('layouts.aside-tags')
13+
</div>
14+
</div>
15+
</main>
916
@endsection
1017

1118
@section('footer')

resources/views/components/new-form.blade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ class="form-control @error('text') is-invalid @enderror"
4040
</div>
4141
@enderror
4242
</div>
43+
44+
<div class="form__field col-6 mb-3">
45+
<label for="form-tags">Tags</label>
46+
<input type="text"
47+
class="form-control"
48+
id="form-tags"
49+
name="tags"
50+
placeholder="tag1, tag2"
51+
value="{{ old('tags', $new->tags->pluck('name')->implode(', ')) }}"
52+
>
53+
</div>
4354
</div>

resources/views/index.blade.php

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,14 @@
77
@section('content')
88
<main class="py-4" style="min-height: 88vh">
99
<div class="container">
10-
<section class="posts-section mb-2 row flex-column flex-sm-row">
11-
<h3 class="posts-section__header col-12 order-2 order-sm-0 text-center">Latest posts</h3>
12-
13-
@if ($posts->count())
14-
<div class="posts-section__posts order-2 order-sm-0 col-12 col-sm-8 col-lg-10 post">
15-
@foreach($posts as $post)
16-
<div class="post__item">
17-
<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">
18-
<div class="post__heading col-12 col-lg-6 p-4 d-flex flex-column" style="height: 280px;">
19-
<strong class="d-inline-block mb-2 text-primary">Post #{{ $post->id }}</strong>
20-
21-
<h3 class="post__name mb-0">{{ $post->name }}</h3>
22-
23-
<div class="post__created-at mb-1 text-muted">{{ $post->created_at->toFormattedDateString() }}</div>
24-
25-
<p class="post__preview card-text mb-auto text-justify"> {{ str_limit($post->text, $limit = 100, $end = '...') }} </p>
26-
27-
@if($post->tags->isNotEmpty())
28-
<div class="post__tags mb-2">
29-
@foreach($post->tags as $tag)
30-
<a href="{{ route('tags.show', $tag) }}" class="badge badge-info text-white">{{ $tag->name }}</a>
31-
@endforeach
32-
</div>
33-
@endif
34-
35-
<a href="{{ route('posts.show', $post->id) }}" class="post__view">Continue reading</a>
36-
</div>
37-
38-
<div class="post__photo col-6 d-none d-lg-flex align-items-center p-2">
39-
<svg class="bd-placeholder-img" width="auto" height="250" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Placeholder: Thumbnail">
40-
<title>Placeholder</title>
41-
<rect width="100%" height="100%" fill="#55595c"></rect>
42-
<text x="35%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text>
43-
</svg>
44-
</div>
45-
</div>
46-
</div>
47-
@endforeach
48-
</div>
49-
50-
@include('layouts.aside-tags')
51-
@else
52-
<p class="no-posts">No available posts yet</p>
53-
@endif
54-
</section>
10+
<div class="intro row">
11+
<div class="flex-column">
12+
@include('layouts.posts.posts-section')
13+
@include('layouts.news.news-section')
14+
</div>
15+
16+
@include('layouts.aside-tags')
17+
</div>
5518
</div>
5619
</main>
5720
@endsection

resources/views/layouts/base/header.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</li>
2222

2323
<li class="nav-item">
24-
<a class="nav-link" href="{{ route('user.posts') }}">{{ __('My posts') }}</a>
24+
<a class="nav-link" href="{{ route('posts.index') }}">{{ __('My posts') }}</a>
2525
</li>
2626

2727
<li class="nav-item">
Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
1-
<main class="py-4" style="min-height: 88vh">
2-
<div class="container">
3-
<section class="news-section news mb-2 row">
4-
<h2 class="news__header order-2 order-sm-0 col-12">News</h2>
1+
<section class="news-section mb-2 flex-column flex-sm-row col-12 col-sm-8 col-lg-10 order-2 order-sm-0">
2+
<h2 class="news-section__header col-12 text-center">News</h2>
53

6-
<div class="news-section__news new col-sm-8 col-lg-10 order-2 order-sm-0">
7-
@forelse($news as $new)
8-
<div class="new__item d-flex">
9-
<div class="new__heading d-flex flex-column p-3 w-100 border rounded mb-4 shadow-sm">
10-
<strong class="mb-2 text-primary">new #{{ $new->id }}</strong>
4+
<div class="news-section__news new">
5+
@forelse($news as $new)
6+
<div class="new__item d-flex">
7+
<div class="new__heading d-flex flex-column p-3 w-100 border rounded mb-4 shadow-sm">
8+
<strong class="mb-2 text-primary">new #{{ $new->id }}</strong>
119

12-
<h3 class="new__name mb-0">{{ $new->name }}</h3>
10+
<h3 class="new__name mb-0">{{ $new->name }}</h3>
1311

14-
<div class="new__created-at mb-1 text-muted">{{ $new->created_at->toFormattedDateString() }}</div>
12+
<div class="new__created-at mb-1 text-muted">{{ $new->created_at->toFormattedDateString() }}</div>
1513

16-
<p class="new__preview card-text flex-grow-1 text-justify"> {{ str_limit($new->text, $limit = 200, $end = '...') }} </p>
14+
<p class="new__preview card-text flex-grow-1 text-justify"> {{ str_limit($new->text, $limit = 200, $end = '...') }} </p>
1715

18-
@if($new->tags->isNotEmpty())
19-
<div class="post__tags mb-2">
20-
@foreach($new->tags as $tag)
21-
<span class="badge badge-info text-white">{{ $tag->name }}</span>
22-
@endforeach
23-
</div>
24-
@endif
16+
@if($new->tags->isNotEmpty())
17+
<div class="post__tags mb-2">
18+
@foreach($new->tags as $tag)
19+
<a href="{{ route('tags.show', $tag) }}" class="badge badge-info text-white">{{ $tag->name }}</a>
20+
@endforeach
21+
</div>
22+
@endif
2523

26-
<div class="d-flex justify-content-end">
27-
<a href="{{ route('news.show', $new) }}" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Read</a>
24+
<div class="d-flex justify-content-end">
25+
<a href="{{ route('news.show', $new) }}" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Read</a>
2826

29-
@if(auth()->user()->hasRole('admin'))
30-
<a href="{{ route('news.edit', $new) }}" class="btn btn-outline-secondary mx-1" style="width: 80px; font-size: 0.7rem">Edit</a>
27+
@if(auth()->user()->hasRole('admin'))
28+
<a href="{{ route('news.edit', $new) }}" class="btn btn-outline-secondary mx-1" style="width: 80px; font-size: 0.7rem">Edit</a>
3129

32-
<form method="post" action="{{ route('news.destroy', $new) }}">
33-
@csrf
34-
@method('DELETE')
30+
<form method="post" action="{{ route('news.destroy', $new) }}">
31+
@csrf
32+
@method('DELETE')
3533

36-
<button type="submit" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Delete</button>
37-
</form>
38-
@endif
39-
</div>
40-
</div>
34+
<button type="submit" class="btn btn-outline-secondary" style="width: 80px; font-size: 0.7rem">Delete</button>
35+
</form>
36+
@endif
4137
</div>
42-
@empty
43-
<p class="no-news">No available news yet</p>
44-
@endforelse
38+
</div>
4539
</div>
46-
47-
@include('layouts.aside-tags')
48-
</section>
40+
@empty
41+
<p class="no-news">No available news yet</p>
42+
@endforelse
4943
</div>
50-
</main>
44+
</section>
45+

0 commit comments

Comments
 (0)