Skip to content

Commit b2300d5

Browse files
authored
Merge pull request #18 from tabuna/profile
WIP
2 parents 75c1f02 + d1d0f5b commit b2300d5

18 files changed

+388
-66
lines changed

app/Casts/PostTypeEnum.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Casts;
4+
5+
enum PostTypeEnum: string
6+
{
7+
case Article = 'article';
8+
case Link = 'link';
9+
10+
}

app/Casts/StatusEnum.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Casts;
4+
5+
enum StatusEnum: string
6+
{
7+
case Publish = 'publish';
8+
case Draft = 'draft';
9+
10+
}

app/Http/Controllers/PostController.php

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Casts\PostTypeEnum;
56
use App\Models\Post;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Validation\Rule;
810
use Illuminate\View\View;
911
use Illuminate\Support\Facades\Response;
1012
use Illuminate\Database\Eloquent\Builder;
@@ -27,15 +29,34 @@ public function show(Post $post)
2729
]);
2830
}
2931

30-
public function edit(Post $post): View
32+
public function edit(Request $request,Post $post)
3133
{
3234
$this->authorize('isOwner', $post);
3335

34-
$title = $post->exists ? 'Редактирование' : 'Новая статья';
35-
return view('post.edit', [
36+
$request->validate([
37+
'select_type' => [
38+
'sometimes',
39+
Rule::enum(PostTypeEnum::class)
40+
]
41+
]);
42+
43+
$isEditing = $post->exists;
44+
if($request->has('select_type') && !$isEditing){
45+
$post->type = PostTypeEnum::from($request->input('select_type'));
46+
$view = 'post.edit.'. $request->input('select_type');
47+
}elseif($isEditing){
48+
$view = 'post.edit.'. $post->type->value;
49+
50+
}else{
51+
$view = 'post.edit.'.PostTypeEnum::Article->value;
52+
}
53+
54+
$title = $isEditing ? 'Редактирование' : 'Новая статья';
55+
return view($view, [
3656
'title' => $title,
3757
'post' => $post,
38-
]);
58+
'isEditing' => $isEditing
59+
])->fragmentsIf(!$request->isMethodSafe());
3960
}
4061

4162
public function update(Request $request, Post $post)
@@ -45,6 +66,10 @@ public function update(Request $request, Post $post)
4566
$request->validate([
4667
'title' => 'required|string',
4768
'content' => 'required|string',
69+
'type' => [
70+
$post->exists ? 'missing' : 'required',
71+
Rule::enum(PostTypeEnum::class)
72+
]
4873
]);
4974

5075
$post->fill([

app/Http/Controllers/ProfileCommentsController.php

+31-9
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,58 @@
55
use App\Models\Comment;
66
use Illuminate\Http\Request;
77
use App\Models\User;
8+
use Illuminate\Support\Facades\Auth;
89

910
class ProfileCommentsController extends Controller
1011
{
12+
public string $action = 'append';
13+
1114
/**
1215
* @param \App\Models\User $user
1316
* @param array $data
1417
*
1518
* @return string
1619
*/
17-
public function show(User $user, array $data = [])
20+
public function show(Request $request, User $user, array $data = [])
1821
{
22+
$comments = $this->getComments($user);
23+
$comments->withPath('/profile/'.$user->nickname.'/comments');
24+
25+
if ($request->get('page') > 1 && $comments->isEmpty())
26+
{
27+
$request->merge([
28+
'page' => $comments->lastPage(),
29+
]);
30+
$comments = $this->getComments($user);
31+
}
32+
33+
$isMyAccount = $user->id === Auth::user()?->id;
1934
return view(
2035
'pages.profile.tabs.comments-tab',
2136
array_merge($data, [
22-
'comments' => $user->comments()->latest()->get(),
37+
'comments' => $comments,
2338
'user' => $user,
24-
'active' => 'comments'
39+
'active' => 'comments',
40+
'action' => $this->action,
41+
'isMyAccount' => $isMyAccount
2542
])
26-
)->fragmentIf(!request()->isMethod('GET'), 'comments');
43+
)->fragmentsIf(!request()->isMethod('GET'));
2744
}
2845

46+
protected function getComments(User $user){
47+
return $comments = $user->comments()
48+
->orderBy('id', 'desc')
49+
->paginate(2);;
50+
}
2951

3052
/**
3153
* @param \App\Models\Comment $comment
3254
*
3355
* @return string
3456
*/
35-
public function showEdit(Comment $comment)
57+
public function showEdit(Request $request,Comment $comment)
3658
{
37-
return $this->show($comment->commenter, [
59+
return $this->show($request,$comment->commenter, [
3860
'edit' => $comment->getKey(),
3961
]);
4062
}
@@ -58,23 +80,23 @@ public function update(Request $request, Comment $comment)
5880
'comment' => $request->message,
5981
]);
6082

61-
return $this->show($comment->commenter);
83+
return $this->show($request,$comment->commenter);
6284
}
6385

6486
/**
6587
* @param \App\Models\Comment $comment
6688
*
6789
* @return string
6890
*/
69-
public function delete(Comment $comment)
91+
public function delete(Request $request, Comment $comment)
7092
{
7193
$this->authorize('delete', $comment);
7294

7395
$comment->children()->exists()
7496
? $comment->delete()
7597
: $comment->forceDelete();
7698

77-
return $this->show($comment->commenter);
99+
return $this->show($request,$comment->commenter);
78100
}
79101

80102
}

app/Models/Post.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace App\Models;
44

5+
use App\Casts\PostTypeEnum;
6+
use App\Casts\StatusEnum;
57
use App\Models\Concerns\HasComments;
68
use App\Models\Concerns\Taggable;
9+
use Illuminate\Database\Eloquent\Builder;
710
use Illuminate\Database\Eloquent\Factories\HasFactory;
811
use Illuminate\Database\Eloquent\Model;
912
use Illuminate\Support\Str;
@@ -22,7 +25,9 @@ class Post extends Model implements Feedable
2225
'title',
2326
'content',
2427
'slug',
25-
'user_id'
28+
'user_id',
29+
'type',
30+
'status'
2631
];
2732

2833
/**
@@ -32,6 +37,12 @@ class Post extends Model implements Feedable
3237
'title' => 'string',
3338
'content' => 'string',
3439
'slug' => 'string',
40+
'type' => PostTypeEnum::class,
41+
'status' => StatusEnum::class
42+
];
43+
44+
protected $attributes = [
45+
'type' => PostTypeEnum::Article
3546
];
3647

3748
public static function boot()

app/Providers/AppServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Providers;
44

5+
use Illuminate\Pagination\Paginator;
56
use Illuminate\Support\ServiceProvider;
67

78
class AppServiceProvider extends ServiceProvider
@@ -19,6 +20,6 @@ public function register(): void
1920
*/
2021
public function boot(): void
2122
{
22-
//
23+
Paginator::useBootstrapFive();
2324
}
2425
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('posts', function (Blueprint $table) {
15+
$table->string('type')->default(\App\Casts\PostTypeEnum::Article);
16+
$table->string('status')->default(\App\Casts\StatusEnum::Draft);
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*/
23+
public function down(): void
24+
{
25+
Schema::table('posts', function (Blueprint $table) {
26+
$table->dropColumn('type');
27+
$table->dropColumn('status');
28+
});
29+
}
30+
};

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
11
@extends('pages.profile.tabs.base')
22

33
@section('tab')
4-
@if($comments->isEmpty())
5-
@if ($isMyAccount)
6-
<div class="bg-body-tertiary rounded p-5 rounded">
7-
<div class="p-5">
8-
<div class="text-center mb-3">
9-
Вы не написали ни одного комментария
10-
</div>
11-
<div class="text-center">
12-
<a href="{{ route('feed') }}" class="btn btn-secondary">Статьи</a>
13-
</div>
14-
</div>
15-
</div>
16-
@else
17-
<div class="bg-body-tertiary rounded p-5 rounded">
18-
<div class="p-5">
19-
<div class="text-center mb-3">
20-
Этот пользователь не оставил ни одного комментария
21-
</div>
22-
</div>
23-
</div>
24-
@endif
25-
@else
264

275
<turbo-frame id="comments-frame">
286
<x-stream target="comments">
297
<div class="col-xl-8 col-md-12 mx-auto mb-3">
30-
<div class="bg-body-tertiary overflow-hidden px-5 py-3 rounded">
31-
@foreach($comments as $comment)
32-
@include('pages.profile.tabs.particles.comments.comment', [
33-
'comment' => $comment,
34-
'edit' => $edit ?? null,
35-
])
36-
@endforeach
37-
</div>
8+
@if($comments->isEmpty())
9+
@if ($isMyAccount)
10+
<div class="bg-body-tertiary rounded p-5 rounded">
11+
<div class="p-5">
12+
<div class="text-center mb-3">
13+
Вы не написали ни одного комментария
14+
</div>
15+
<div class="text-center">
16+
<a href="{{ route('feed') }}" class="btn btn-secondary">Статьи</a>
17+
</div>
18+
</div>
19+
</div>
20+
@else
21+
<div class="bg-body-tertiary rounded p-5 rounded">
22+
<div class="p-5">
23+
<div class="text-center mb-3">
24+
Этот пользователь не оставил ни одного комментария
25+
</div>
26+
</div>
27+
</div>
28+
@endif
29+
@else
30+
31+
<div class="bg-body-tertiary overflow-hidden px-5 py-3 rounded mb-3">
32+
@foreach($comments as $comment)
33+
@include('pages.profile.tabs.particles.comments.comment', [
34+
'comment' => $comment,
35+
'edit' => $edit ?? null,
36+
])
37+
@endforeach
38+
</div>
39+
{{ $comments->links() }}
40+
41+
@endif
3842
</div>
43+
3944
</x-stream>
45+
4046
</turbo-frame>
41-
@endif
47+
4248
@endsection

resources/views/pages/profile/tabs/particles/comments/comment.blade.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
<div class="d-flex position-relative overflow-hidden">
44
<div class="avatar avatar-sm me-3">
5-
<a href="#!">
65
<img class="avatar-img rounded-circle"
76
src="{{ $comment->commenter->avatar }}" alt="{{ $comment->commenter->name }}">
8-
</a>
7+
98
</div>
109

1110
<div class="w-100">
@@ -25,12 +24,12 @@ class="link-body-emphasis text-decoration-none">
2524

2625
@can('update', $comment)
2726
·
28-
<a href="{{ route('profile.comments.show.edit', $comment) }}" data-turbo-method="post"
27+
<a href="{{ route('profile.comments.show.edit', ['comment' => $comment, 'page' =>$comments->currentPage()]) }}" data-turbo-method="post"
2928
class="link-body-emphasis text-decoration-none">Редактировать</a>
3029
@endcan
3130

3231
@can('delete', $comment)
33-
· <a href="{{ route('profile.comments.delete', $comment) }}"
32+
· <a href="{{ route('profile.comments.delete', ['comment' => $comment, 'page' =>$comments->currentPage()]) }}"
3433
class="link-body-emphasis text-decoration-none"
3534
data-turbo-method="DELETE"
3635
data-turbo-confirm="Вы уверены, что хотите удалить комментарий?">
@@ -42,7 +41,7 @@ class="link-body-emphasis text-decoration-none"
4241

4342

4443
@if($edit === $comment->getKey())
45-
<form method="POST" action="{{ route('profile.comments.update', $comment->getKey()) }}"
44+
<form method="POST" action="{{ route('profile.comments.update', ['comment' => $comment, 'page' =>$comments->currentPage()]) }}"
4645
class="mb-1 d-flex flex-column position-relative">
4746
@method('PUT')
4847
<textarea required class="form-control mb-3" name="message"

resources/views/particles/comments/comment.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@if(!$comment->trashed())
1717
<div class="d-flex position-relative overflow-hidden">
1818
<div class="avatar avatar-sm me-3">
19-
<a href="#!">
19+
<a href="{{route('profile',$comment->commenter)}}">
2020
<img class="avatar-img rounded-circle"
2121
src="{{ $comment->commenter->avatar }}" alt="{{ $comment->commenter->name }}">
2222
</a>

0 commit comments

Comments
 (0)