Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
#42 Review reported controller, test and request
Browse files Browse the repository at this point in the history
  • Loading branch information
marky291 committed Jun 3, 2019
1 parent 1bdf324 commit ff8b150
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 18 deletions.
28 changes: 28 additions & 0 deletions app/Http/Controllers/ReviewReportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\ReviewReportRequest;
use App\Interactions\Review;
use App\Notifications\ReviewWasReported;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ReviewReportController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param Review $review
* @param Request $request
* @return JsonResponse
*/
public function store(Review $review, ReviewReportRequest $request): JsonResponse
{
$review = $review->report($request->validated(), auth()->user());

$status = $review->save();

return response()->json(['status' => $status], 200);
}
}
3 changes: 2 additions & 1 deletion app/Http/Middleware/CheckForMaintenanceMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class CheckForMaintenanceMode extends Middleware
* @var array
*/
protected $exemptIP = [
'80.233.36.207',
'80.233.36.207',
'45.56.25.157',
];

/**
Expand Down
31 changes: 31 additions & 0 deletions app/Http/Requests/ReviewReportRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ReviewReportRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'reason' => 'required', 'string', 'max:255',
'meta' => 'sometimes', 'array',
];
}
}
9 changes: 9 additions & 0 deletions app/Interactions/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\ReviewComment;
use App\Listings\Listing;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\Eloquent\Collection;

Expand Down Expand Up @@ -48,6 +49,14 @@ class Review extends Interaction
*/
protected $guarded = [];

/**
* @return MorphToMany
*/
public function listing()
{
return $this->morphedByMany(Listing::class, 'interaction');
}

/**
* A review has many comments.
*/
Expand Down
7 changes: 6 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -24,6 +25,10 @@ public function boot()
{
$this->registerPolicies();

//
// Implicitly grant "Super Admin" role all permissions
// This works in the app by using gate-related functions like auth()->user->can() and @can()
Gate::before(function ($user, $ability) {
return $user->hasRole('admin') ? true : null;
});
}
}
20 changes: 19 additions & 1 deletion public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16113,6 +16113,9 @@ __webpack_require__.r(__webpack_exports__);
commenting: false,
comment: new vform__WEBPACK_IMPORTED_MODULE_1__["Form"]({
message: ''
}),
report: new vform__WEBPACK_IMPORTED_MODULE_1__["Form"]({
reason: ''
})
};
},
Expand Down Expand Up @@ -16153,7 +16156,22 @@ __webpack_require__.r(__webpack_exports__);
_this.$Message.error(error.message);
});
},
reportReview: function reportReview() {}
reportReview: function reportReview() {
var _this2 = this;

this.$Modal.prompt({
title: 'Reporting review created by {REVIEW_NAME}',
content: 'Type your reason for reporting this review:'
}).then(function (data) {
_this2.report.reason = data.value;

_this2.report.post('/review/' + _this2.review.id + '/report').then(function (response) {
_this2.$Message.success('Awesome!, We will notify you when we decide action upon the review.');
})["catch"](function (error) {
_this2.$Message.error(error.message);
});
});
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"/js/app.js": "/js/app.js?id=4e7130d8dcde832ba890",
"/js/app.js": "/js/app.js?id=17e7812e0655ed5accbc",
"/css/app.css": "/css/app.css?id=3903f9404b68e9b6a947"
}
20 changes: 17 additions & 3 deletions resources/js/components/ReviewComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
comment: new Form({
message: '',
}),
report: new Form({
reason: '',
})
}
},
Expand Down Expand Up @@ -52,9 +56,19 @@
this.$Message.error(error.message);
})
},
reportReview() {
}
reportReview: function() {
this.$Modal.prompt({
title: 'Reporting review created by {REVIEW_NAME}',
content: 'Type your reason for reporting this review:'
}).then((data) => {
this.report.reason = data.value;
this.report.post('/review/'+this.review.id+'/report').then(response => {
this.$Message.success('Awesome!, We will notify you when we decide action upon the review.');
}).catch(error => {
this.$Message.error(error.message);
})
});
},
}
}
Expand Down
5 changes: 5 additions & 0 deletions resources/views/account/frame.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
<ul class="list-unstyled">
<li class="tw-py-2" {{ $selected === 'account' ? 'selected' : null }}><a class="tw-border-l-4 hover:tw-border-purple tw-px-6 hover:tw-no-underline tw-pb-1 tw-text-sm tw-text-grey-darkest tw-font-bold" href="/account">My Account</a></li>
<li class="tw-py-2" {{ $selected === 'notifications' ? 'selected' : null }}><a class="tw-border-l-4 hover:tw-border-purple tw-px-6 hover:tw-no-underline tw-pb-1 tw-text-sm tw-text-grey-darkest" href="/account/notifications">Notifications</a></li>
@role('creator')
<li class="tw-py-2" {{ $selected === 'servers' ? 'selected' : null }}><a class="tw-border-l-4 hover:tw-border-purple tw-px-6 hover:tw-no-underline tw-pb-1 tw-text-sm tw-text-grey-darkest" href="/account/servers">My Servers</a></li>
@endrole
@can('moderate')
<li class="tw-py-2" {{ $selected === 'moderation' ? 'selected' : null }}><a class="tw-border-l-4 hover:tw-border-purple tw-px-6 hover:tw-no-underline tw-pb-1 tw-text-sm tw-text-grey-darkest" href="/account">Moderator Tools</a></li>
@endcan
</ul>
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion resources/views/layouts/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
<at-submenu>
<template slot="title"><i class="icon icon-user"></i>{{ auth()->user()->username }}</template>
<at-menu-item name="account"><i class="icon icon-settings"></i>My Account</at-menu-item>
<at-menu-item name="account/servers"><i class="icon icon-settings"></i>Listings</at-menu-item>
@can('moderate')
<at-menu-item name="moderate/reports"><i class="icon icon-alert-octagon"></i>Moderator Tools</at-menu-item>
@endcan
@role('creator')
<at-menu-item name="account/servers"><i class="icon icon-settings"></i>My Listings</at-menu-item>
@endrole
<at-menu-item name="logout"><i class="icon icon-settings"></i>Logout</at-menu-item>
</at-submenu>
@else
Expand Down
14 changes: 7 additions & 7 deletions resources/views/listing/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@
<div class="container px-5 pt-4 pb-3">
<h3 class="heading mb-4 tw-font-bold text-dark heading-underline">Screenshot Previews</h3>
<div class="mb-3">
<carousel-3d :disable3d="true" :space="360" :height="200" :width="350" :autoplay="true" :autoplay-timeout="5000" :controls-visible="true" :controls-width="30" :controls-height="60" :clickable="false">
@foreach($listing->screenshots as $index => $screenshot)
<slide :index="{{ $index }}">
<img class="h-100" src="{{ $screenshot->link }}" alt="Image {{ $index }}">
</slide>
@endforeach
</carousel-3d>
{{-- <carousel-3d :disable3d="true" :space="360" :height="200" :width="350" :autoplay="true" :autoplay-timeout="5000" :controls-visible="true" :controls-width="30" :controls-height="60" :clickable="false">--}}
{{-- @foreach($listing->screenshots as $index => $screenshot)--}}
{{-- <slide :index="{{ $index }}">--}}
{{-- <img class="h-100" src="{{ $screenshot->link }}" alt="Image {{ $index }}">--}}
{{-- </slide>--}}
{{-- @endforeach--}}
{{-- </carousel-3d>--}}
</div>
</div>
</section>
Expand Down
6 changes: 5 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
Route::resource('review.comment', 'ReviewCommentController', ['middleware'=>['auth']])->only([
'store',
]);
Route::resource('review.report', 'ReviewReportController', ['middleware' => ['auth', 'can:moderate']])->only([
'store',
]);

Route::resource('listing.reviews', 'ReviewController', ['middleware' => ['auth']])->only([
'store', 'update', 'destroy',
'store', 'update', 'destroy', 'report',
]);

// default index page.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Tests\Feature;

use App\Notifications\ReviewWasReported;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
use App\Listings\Listing;
use App\Interactions\Review;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ReviewRequestTest extends TestCase
class ReviewControllerTest extends TestCase
{
use WithFaker;
use RefreshDatabase;
Expand Down Expand Up @@ -92,4 +94,21 @@ public function it_can_be_destroyed()

$this->assertDatabaseMissing('reviews', ['id' => $review->getKey()]);
}

public function test_it_can_be_reported()
{
$this->signIn();

$review = factory(Review::class)->create();

$listing = factory(Listing::class)->create();

$listing->reviews()->save($review);

$response = $this->post("/review/{$review->getKey()}/report", ['reason' => 'foo', 'meta' => 'bar']);

$response->assertStatus(200);

$this->assertCount(1, $review->reports);
}
}
5 changes: 4 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ abstract class TestCase extends BaseTestCase

/**
* Set up a fake user with a listing.
* @param string $withRole
*/
public function signIn()
public function signIn(string $withRole = 'admin'): void
{
$user = factory(User::class)->create();

$user->assignRole($withRole);

$this->be($user);
}

Expand Down

0 comments on commit ff8b150

Please sign in to comment.