forked from Hexlet/hexlet-sicp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from fey/feature/comments-v2
add mvp comments
- Loading branch information
Showing
29 changed files
with
360 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class Comment extends Model | ||
{ | ||
use SoftDeletes; | ||
|
||
protected $with = ['user']; | ||
protected $fillable = ['content', 'commentable_type', 'commentable_id']; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
public function commentable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
static::addGlobalScope('order', function (Builder $builder) { | ||
$builder->orderBy('created_at', 'asc'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Comment; | ||
use Illuminate\Http\Request; | ||
|
||
class CommentController extends Controller | ||
{ | ||
public function store(Request $request) | ||
{ | ||
$this->validate( | ||
$request, | ||
[ | ||
'commentable_type' => 'required|string', | ||
'commentable_id' => 'required|string|min:1', | ||
'content' => 'required|string|min:1|max:500' | ||
] | ||
); | ||
$user = auth()->user(); | ||
|
||
$comment = $user->comments()->save( | ||
Comment::make($request->all()) | ||
); | ||
|
||
return $this->redirectBackToComment($comment); | ||
} | ||
|
||
public function update(Request $request, Comment $comment) | ||
{ | ||
$this->validate($request, [ | ||
'content' => 'required|string|min:1|max:500' | ||
]); | ||
$content = $request->get('content', $comment->content); | ||
$comment->update(['content' => $content]); | ||
|
||
return $this->redirectBackToComment($comment); | ||
} | ||
|
||
public function destroy(Comment $comment) | ||
{ | ||
$comment->delete(); | ||
|
||
return back(); | ||
} | ||
|
||
/** | ||
* @param Comment $comment | ||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector | ||
*/ | ||
private function redirectBackToComment(Comment $comment) | ||
{ | ||
return redirect( | ||
url()->previous() . '#comment-' . $comment->id | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Comment; | ||
use App\User; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
|
||
class CommentPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
public function viewAny() | ||
{ | ||
return true; | ||
} | ||
|
||
public function view() | ||
{ | ||
return true; | ||
} | ||
|
||
public function create() | ||
{ | ||
return true; | ||
} | ||
|
||
public function update(User $user, Comment $comment) | ||
{ | ||
return $user->id === $comment->user->id; | ||
} | ||
|
||
public function delete(User $user, Comment $comment) | ||
{ | ||
return $user->id === $comment->user->id; | ||
} | ||
|
||
public function restore() | ||
{ | ||
return false; | ||
} | ||
|
||
public function forceDelete() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
/** @var \Illuminate\Database\Eloquent\Factory $factory */ | ||
|
||
use App\Comment; | ||
use Faker\Generator as Faker; | ||
|
||
$factory->define(Comment::class, function (Faker $faker) { | ||
return [ | ||
'content' => $faker->text, | ||
]; | ||
}); |
37 changes: 37 additions & 0 deletions
37
database/migrations/2020_02_07_223934_create_comments_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateCommentsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('comments', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->integer('user_id'); | ||
$table->string('commentable_type'); | ||
$table->integer('commentable_id'); | ||
$table->text('content'); | ||
$table->integer('parent_id')->nullable(); | ||
$table->timestamps(); | ||
$table->softDeletes(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('comments'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'settings' => 'Settings', | ||
'account' => 'Account', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'title' => 'Activity log', | ||
'time' => 'Time', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
return [ | ||
'none_comments' => 'There are no comments yet.', | ||
'authentication_required' => 'Authentication required', | ||
'must_log_in' => 'You must log in to post a comment.', | ||
'enter_your_message' => 'Enter your message here', | ||
'submit' => 'Submit', | ||
'edit_comment' => 'Edit Comment', | ||
'update_comment_here' => 'Update your message here:', | ||
'cancel' => 'Cancel', | ||
'update' => 'Update', | ||
'edit' => 'Edit', | ||
'delete' => 'Delete', | ||
'reply' => 'Reply', | ||
'reply_to_comment' => 'Reply to Comment', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'generate_sitemap' => 'Generating sitemap' | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'exercise' => 'Exercise', | ||
'show' => [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'settings' => 'Настройка', | ||
'account' => 'Аккаунт', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'title' => 'История активности', | ||
'time' => 'Время', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
return [ | ||
'none_comments' => 'Комментарии отсутствуют.', | ||
'authentication_required' => 'Необходима авторизация', | ||
'must_log_in' => 'Вы должны авторизоваться для создания комментария.', | ||
'enter_your_message' => 'Поле ввода сообщения', | ||
'submit' => 'Отправить', | ||
'edit_comment' => 'Редактировать', | ||
'update_comment_here' => 'Изменить сообщение здесь:', | ||
'cancel' => 'Отменить', | ||
'update' => 'Обновить', | ||
'edit' => 'Изменить', | ||
'delete' => 'Удалить', | ||
'reply' => 'Ответить', | ||
'reply_to_comment' => 'Ответ на комментарий', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'exercise' => 'Упражнение', | ||
'show' => [ | ||
|
Oops, something went wrong.