Skip to content

Commit

Permalink
Merge pull request #171 from fey/feature/comments-v2
Browse files Browse the repository at this point in the history
add mvp comments
  • Loading branch information
fey committed Feb 8, 2020
2 parents 45b17ad + a1ebd12 commit 530dbcd
Show file tree
Hide file tree
Showing 29 changed files with 360 additions and 13 deletions.
7 changes: 0 additions & 7 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@ plugins:
file_extensions: "php"
ignore_warnings: true
standard: "./phpcs.xml"
sonar-php:
enabled: true
checks:
php:S1192:
enabled: false
tests_patterns:
- tests/**
phpmd:
enabled: true
config:
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ php artisan migrate --seed
#### Прикладные вещи

* Все экшены контроллеров должны быть покрыты тестами
* Формы делаются с помощью https://github.com/LaravelCollective/html
* Формы делаются с помощью [netojose/laravel-bootstrap-4-forms](https://github.com/netojose/laravel-bootstrap-4-forms)
* В подавляющем большинстве используется ресурсный роутинг. Что под него не подходит сначала обсуждается (такое бывает крайне редко)
* Тексты только через локали
* Чтобы включить логирование Rollbar, необходимо установить переменную `LOG_CHANNEL=rollbar` и `ROLLBAR_TOKEN=`
Expand Down Expand Up @@ -77,4 +77,7 @@ $ git config core.hooksPath .githooks
[![Hexlet Ltd. logo](https://raw.githubusercontent.com/Hexlet/hexletguides.github.io/master/images/hexlet_logo128.png)](https://ru.hexlet.io/pages/about?utm_source=github&utm_medium=link&utm_campaign=exercises-javascript)

This repository is created and maintained by the team and the community of Hexlet, an educational project. [Read more about Hexlet (in Russian)](https://ru.hexlet.io/pages/about?utm_source=github&utm_medium=link&utm_campaign=exercises-javascript).
##

## FAQ
Q: Ошибка `Illuminate\Session\TokenMismatchException: CSRF token mismatch.`
A: Сбросить кеш конфига `php artisan config:clear`
6 changes: 5 additions & 1 deletion app/Chapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Chapter extends Model
{
Expand All @@ -12,6 +11,11 @@ public function users()
return $this->belongsToMany(User::class, 'read_chapters');
}

public function comments()
{
return $this->morphMany('\App\Comment', 'commentable');
}

public function parent()
{
return $this->belongsTo(self::class);
Expand Down
34 changes: 34 additions & 0 deletions app/Comment.php
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');
});
}
}
6 changes: 5 additions & 1 deletion app/Exercise.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Exercise extends Model
{
public function chapter()
{
return $this->belongsTo(Chapter::class);
}

public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
1 change: 1 addition & 0 deletions app/Helpers/ExerciseHelper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

if (!function_exists('getExerciseListingViewFilepath')) {
function getExerciseListingViewFilepath(string $exercisePath): string
{
Expand Down
57 changes: 57 additions & 0 deletions app/Http/Controllers/CommentController.php
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
);
}
}
47 changes: 47 additions & 0 deletions app/Policies/CommentPolicy.php
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;
}
}
3 changes: 3 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\ServiceProvider;
use URL;
use DB;
use Blade;

class AppServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -35,5 +36,7 @@ public function boot()
if (config('app.env') === 'production') {
URL::forceScheme('https');
}

Blade::include('components.comments', 'comments');
}
}
5 changes: 3 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Comment;
use App\Policies\CommentPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

Expand All @@ -14,6 +16,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
Comment::class => CommentPolicy::class,
];

/**
Expand All @@ -24,7 +27,5 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
$this->registerPolicies();

//
}
}
5 changes: 5 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public function readChapters()
{
return $this->hasMany(ReadChapter::class);
}

public function comments()
{
return $this->hasMany(Comment::class);
}
}
12 changes: 12 additions & 0 deletions database/factories/CommentFactory.php
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 database/migrations/2020_02_07_223934_create_comments_table.php
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');
}
}
1 change: 1 addition & 0 deletions resources/lang/en/account.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'settings' => 'Settings',
'account' => 'Account',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/activitylog.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'title' => 'Activity log',
'time' => 'Time',
Expand Down
17 changes: 17 additions & 0 deletions resources/lang/en/comment.php
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',
];
1 change: 1 addition & 0 deletions resources/lang/en/console.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'generate_sitemap' => 'Generating sitemap'
];
1 change: 1 addition & 0 deletions resources/lang/en/exercise.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'exercise' => 'Exercise',
'show' => [
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ru/account.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'settings' => 'Настройка',
'account' => 'Аккаунт',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/ru/activitylog.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'title' => 'История активности',
'time' => 'Время',
Expand Down
17 changes: 17 additions & 0 deletions resources/lang/ru/comment.php
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' => 'Ответ на комментарий',
];
1 change: 1 addition & 0 deletions resources/lang/ru/exercise.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

return [
'exercise' => 'Упражнение',
'show' => [
Expand Down
Loading

0 comments on commit 530dbcd

Please sign in to comment.