Skip to content

Commit

Permalink
статьи и комментарии для профиля
Browse files Browse the repository at this point in the history
  • Loading branch information
SadElephant committed Nov 13, 2023
1 parent f6afd52 commit fa5738d
Show file tree
Hide file tree
Showing 15 changed files with 520 additions and 110 deletions.
10 changes: 6 additions & 4 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public function show(Post $post)
$post->with('comments');

return view('post.show', [
'post' => $post,
'post' => $post,
]);
}

public function edit(Post $post): View
{
//нужно будет проверять аавтора, если пост существует
$this->authorize('isOwner', $post);

$title = $post->exists ? 'Редактирование' : 'Новая статья';
return view('post.edit', [
Expand All @@ -36,7 +36,7 @@ public function edit(Post $post): View

public function update(Request $request, Post $post)
{
//нужно проверять аавтора, если пост существует
$this->authorize('isOwner', $post);

$request->validate([
'title' => 'required|string',
Expand All @@ -54,6 +54,7 @@ public function update(Request $request, Post $post)
return redirect()->route('post.edit', $post);//пока сюда
}


/**
* @param \Illuminate\Http\Request $request
*
Expand All @@ -70,6 +71,7 @@ public function list(Request $request)
'posts' => $posts,
])->fragmentsIf(!$request->isMethodSafe());

return $test;
return $test;
}

}
117 changes: 117 additions & 0 deletions app/Http/Controllers/ProfileCommentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Http\Controllers;

use App\Models\Comment;
use Illuminate\Http\Request;
use App\Models\User;

class ProfileCommentsController extends Controller
{
/**
* @param \App\Models\User $user
* @param array $data
*
* @return string
*/
public function show(User $user, array $data = [])
{
return view(
'pages.profile.tabs.comments-tab',
array_merge($data, [
'comments' => $user->comments,
'user' => $user,
'active' => 'comments'
])
)->fragmentIf(!request()->isMethod('GET'), 'comments');
}

/**
* @param \App\Models\Comment $comment
*
* @return string
*/
public function showReply(Comment $comment)
{
return $this->show($comment->commenter, [
'reply' => $comment->getKey(),
]);
}

/**
* @param \App\Models\Comment $comment
*
* @return string
*/
public function showEdit(Comment $comment)
{
return $this->show($comment->commenter, [
'edit' => $comment->getKey(),
]);
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Comment $comment
*
* @return string
*/
public function reply(Request $request, Comment $comment)
{
$this->authorize('reply', $comment);

$request->validate([
'message' => 'required|string',
]);

$reply = new Comment([
'comment' => $request->input('message'),
'approved' => true,
]);

$reply->commenter()->associate($request->user());
$reply->commentable()->associate($comment->commentable);
$reply->parent()->associate($comment);
$reply->save();

return $this->show($comment->commenter);
}

/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Comment $comment
*
* @return string
*/
public function update(Request $request, Comment $comment)
{
$this->authorize('update', $comment);

$request->validate([
'message' => 'required|string',
]);

$comment->update([
'comment' => $request->message,
]);

return $this->show($comment->commenter);
}

/**
* @param \App\Models\Comment $comment
*
* @return string
*/
public function delete(Comment $comment)
{
$this->authorize('delete', $comment);

$comment->children()->exists()
? $comment->delete()
: $comment->forceDelete();

return $this->show($comment->commenter);
}

}
58 changes: 48 additions & 10 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;

Expand All @@ -15,7 +16,7 @@ class ProfileController extends Controller
*/
public function show(User $user, Request $request)
{
$isMyAccount = $user->id === $request->user()->id;
$isMyAccount = $user->id === $request->user()?->id;

$posts = $user->load('posts')
->orderBy('id', 'desc')
Expand All @@ -25,33 +26,70 @@ public function show(User $user, Request $request)
'user' => $user,
'isMyAccount' => $isMyAccount,
'posts' => $posts,
'active' => 'posts'
]);
}

public function edit(Request $request){

public function edit(Request $request)
{
return view('pages.profile.edit', [
'user' => $request->user(),
'user' => $request->user(),
]);
}

public function update(Request $request)
{
$request->validate([
'name' => 'required|string',
'about'=> 'required|string'
],
$request->validate(
[
'name' => 'required|string',
'about' => 'sometimes|string'
],
[],
[

'name' => 'Имя',
'name' => 'Имя',
'about' => 'О себе'
]);
]
);

$request->user()->fill([
'name' => $request->input('name'),
'about' => $request->input('about')
])->save();
return $this->edit($request)->fragment('profile');
}

/**
* @param \Illuminate\Http\Request $request
*
* @return string
*/
public function postTab(Request $request, User $user)
{
$posts = $user->posts()
->withCount('comments')
->orderBy('id', 'desc')
->simplePaginate(3);

$test = view('pages.profile.tabs.posts-tab', [
'posts' => $posts,
'user' => $user,
'isMyAccount' => $user->id === $request->user()?->id,
'active' => 'posts'
])->fragmentsIf(!$request->isMethodSafe());

return $test;
}

public function commentsTab(User $user, array $data = [])
{
return view(
'pages.profile.tabs.comments-tab',
array_merge($data, [
'user' => $user,
'active' => 'comments'
])
)
->fragmentIf(!request()->isMethod('GET'), 'comments');
}
}
41 changes: 41 additions & 0 deletions app/Policies/OwnerPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Policies;

use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
use Illuminate\Database\Eloquent\Model;

trait OwnerPolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view the model.
*
* @param User $user
* @param Model $model
*
* @return mixed
*/
public function owner(User $user, Model $model)
{
return ($user->id === $model->user_id) || is_null($model->user_id)
? Response::allow()
: Response::denyAsNotFound();
}

/**
* Determine whether the user can view the model.
*
* @param User $user
* @param Model $model
*
* @return bool
*/
public function isOwner(User $user, Model $model): bool
{
return is_null($model->user_id) || ($user->id === $model->user_id);
}
}
9 changes: 9 additions & 0 deletions app/Policies/PostPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Policies;

class PostPolicy
{
use OwnerPolicy;

}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"doctrine/dbal": "^3.7",
"guzzlehttp/guzzle": "^7.2",
"jolicode/jolitypo": "^1.4",
"laravel/framework": "^10.10",
"laravel/framework": "^10.31",
"laravel/sanctum": "^3.2",
"laravel/socialite": "^5.9",
"laravel/tinker": "^2.8",
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fa5738d

Please sign in to comment.