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 05b61a4
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 84 deletions.
6 changes: 4 additions & 2 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function show(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 @@ -72,4 +73,5 @@ public function list(Request $request)

return $test;
}

}
115 changes: 115 additions & 0 deletions app/Http/Controllers/ProfileCommentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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);
}

}
59 changes: 47 additions & 12 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,91 @@

namespace App\Http\Controllers;

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

class ProfileController extends Controller
{
/**
* @param \App\Models\User $user
* @param \App\Models\User $user
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Contracts\View\View
*/
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')
->simplePaginate(5);

return view('pages.profile.profile', [
'user' => $user,
'user' => $user,
'isMyAccount' => $isMyAccount,
'posts' => $posts,
'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'),
'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;

}
28 changes: 21 additions & 7 deletions resources/views/pages/profile/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<!-- Cover image -->
<div class="rounded-top"
style="height:200px;background-image:url(https://images.unsplash.com/photo-1698434156086-918aa526b531?auto=format&fit=crop&q=80&w=2340&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D); background-position: center; background-size: cover; background-repeat: no-repeat;">

</div>
<!-- Card body START -->
<div class="px-5">
Expand All @@ -19,34 +20,42 @@
</div>
</div>
<div class="ms-sm-4 mt-sm-3">

<h1 class="mb-0 h5">
{{ $user->name }}
<x-icon path="bs.patch-check-fill" class="text-primary" />
</h1>
<small class="opacity-75">На проекте с 23 мая 2022</small>
</div>

<div class="d-flex mt-3 justify-content-center ms-sm-auto">
<div class="d-flex flex-column my-3 ms-sm-auto ">


@if ($isMyAccount)
<x-logout class="btn btn-link" formId="sign-out">
<x-icon path="bs.logout" class="pe-1" />
Выйти из профиля
</x-logout>
<a href="{{route('my.edit')}}" class="btn btn-danger">
<x-icon path="bs.pencil-fill" class="pe-1"/>
Редактировать
</a>
@else
<a href="https://github.com/{{$user->nickname}}" class="d-block mb-2">
<x-icon path="bs.github" width="1.5em" height="1.5em" class="pe-1"/>
</a>


<x-logout class="btn btn-link" formId="sign-out">
<x-icon path="bs.logout" class="pe-1" />
Выйти из профиля
</x-logout>
@endif
</div>
</div>

<p>Тут будут награды пользователя:</p>
{{--<p>Тут будут награды пользователя:</p>--}}

</div>
</div>

@if ($isMyAccount)
{{-- @if ($isMyAccount)
<div class="bg-body-tertiary rounded p-5">
<div class="p-5">
<div class="text-center mb-3">
Expand All @@ -66,6 +75,11 @@
</div>
</div>
@endif
--}}

</div>
<turbo-frame id="tabs-frame" src="{{route('profile.posts',$user)}}" target="_top"/>
</div>


@endsection
Loading

0 comments on commit 05b61a4

Please sign in to comment.