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 22c5682
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 116 deletions.
35 changes: 28 additions & 7 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Illuminate\Support\Facades\Response;
use Illuminate\Database\Eloquent\Builder;

class PostController extends Controller
{
public $action = "append";

/**
* @param \App\Models\Post $post
*
Expand All @@ -19,13 +23,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 +40,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 +58,19 @@ public function update(Request $request, Post $post)
return redirect()->route('post.edit', $post);//пока сюда
}

public function delete(Request $request, Post $post)
{
$this->authorize('isOwner', $post);

$post->delete();
$this->action = "replace";

//сюда поставить уведомление

return $this->list($request);//пока сюда
}


/**
* @param \Illuminate\Http\Request $request
*
Expand All @@ -63,13 +80,17 @@ public function list(Request $request)
{
$posts = Post::with(['user'])
->withCount('comments')
->when($request->has('user_id'), fn(Builder $query) => $query->where('user_id', $request->get('user_id')))
->orderBy('id', 'desc')
->simplePaginate(5);
->cursorPaginate(3);

$test = view('post.list', [
'posts' => $posts,
$test = view('particles.posts.list', [
'posts' => $posts,
'isMyProfile' => $request->has('user_id') && $request->user()?->id == $request->get('user_id'),
'action' => $this->action
])->fragmentsIf(!$request->isMethodSafe());

return $test;
return $test;
}

}
80 changes: 80 additions & 0 deletions app/Http/Controllers/ProfileCommentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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()->latest()->get(),
'user' => $user,
'active' => 'comments'
])
)->fragmentIf(!request()->isMethod('GET'), 'comments');
}


/**
* @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 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);
}

}
49 changes: 39 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,61 @@ 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)
{
return view('pages.profile.tabs.posts-tab', [
'user' => $user,
'active' => 'posts'
])->fragmentsIf(!$request->isMethodSafe());
}

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 22c5682

Please sign in to comment.