Skip to content

Commit

Permalink
Profanity filter implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbigbear committed Dec 19, 2019
1 parent 6f2c252 commit 666fd0f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
40 changes: 37 additions & 3 deletions imgbook/app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Profanity;
use App\Comment;
use App\Post;

class CommentController extends Controller
{
Expand Down Expand Up @@ -50,11 +53,27 @@ public function create()
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function apiStore(Request $request)
public function apiStore(Request $request, Profanity $p)
{

$request = $this->validateStoreRequest($request);

$profanity = $p->checkProfanity($request['text']);

$profanity = (string) $profanity->getBody();
if($profanity === 'true') {
$profanity = true;
} else {
$profanity = false;
}

if($profanity) {
$errors = array(
'error' => 'Profanity detected'
);
return response($errors,400);
}

$c = new Comment;
$c->text = $request['text'];
$c->post_id = $request['post_id'];
Expand Down Expand Up @@ -94,15 +113,30 @@ public function edit($id)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(Request $request, $id, Profanity $p)
{

$request = $this->validateUpdateRequest($request);

$profanity = $p->checkProfanity($request['text']);

$profanity = (string) $profanity->getBody();
if($profanity === 'true') {
$profanity = true;
} else {
$profanity = false;
}

if($profanity) {
$errors = array(
'error' => 'Profanity detected'
);
return $errors;
}

$comment = Comment::findOrFail($id);
$comment->text = $request['text'];
$comment->save();
$comment = Comment::findOrFail($id);

return $comment;
}
Expand Down
37 changes: 34 additions & 3 deletions imgbook/app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@ public function store(Request $request, Profanity $p)
{
$validatedData = $this->validateRequest($request);

$profanity = $p->checkProfanity($validatedData['title']);
$profanity = $p->checkProfanity($validatedData['title'].$validatedData['description']);

$profanity = (string) $profanity->getBody();
if($profanity === 'true') {
$profanity = true;
} else {
$profanity = false;
}

if($profanity) {
$errors = array(
'message' => 'Profanity detected'
);
return view('posts.create', ['update' => False])->withErrors($errors);
}

$image = $validatedData['image'];

Expand Down Expand Up @@ -118,10 +132,28 @@ public function edit(Request $request, $id)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(Request $request, $id, Profanity $p)
{
$request = $this->validateUpdateRequest($request);

$profanity = $p->checkProfanity($request['title'].$request['description']);

$post = Post::findOrFail($id);

$profanity = (string) $profanity->getBody();
if($profanity === 'true') {
$profanity = true;
} else {
$profanity = false;
}

if($profanity) {
$errors = array(
'message' => 'Profanity detected'
);
return view('posts.create', ['update' => true, 'post' => $post])->withErrors($errors);
}

$post->title = $request['title'];
$post->description = $request['description'];
$post->save();
Expand All @@ -139,7 +171,6 @@ public function update(Request $request, $id)
*/
public function destroy($userId, $id)
{

$post = Post::findOrFail($id);

if($post->user_id == $userId) {
Expand Down
17 changes: 14 additions & 3 deletions imgbook/public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,11 +1866,15 @@ __webpack_require__.r(__webpack_exports__);
//
//
//
//
//
//
/* harmony default export */ __webpack_exports__["default"] = ({
props: ['postId', 'userId'],
data: function data() {
return {
comments: [],
error: '',
newComment: '',
commentMessage: 'Post a comment',
changeOrPost: 'Post',
Expand All @@ -1888,7 +1892,6 @@ __webpack_require__.r(__webpack_exports__);
//success
_this.comments = response.data;
_this.newComment = '';
console.log(response.data);
})["catch"](function (response) {
//failure
console.log(response);
Expand All @@ -1913,9 +1916,10 @@ __webpack_require__.r(__webpack_exports__);
_this2.comments.push(response.data);

_this2.newComment = '';
})["catch"](function (response) {
_this2.error = '';
})["catch"](function (error) {
//failure
console.log(response);
_this2.error = error.response.data.error;
});
},
editCommentButton: function editCommentButton(comment) {
Expand Down Expand Up @@ -37345,6 +37349,13 @@ var render = function() {
{ attrs: { id: "root" } },
[
_c("div", { staticClass: "form-group" }, [
_vm.error
? _c("div", { staticClass: "alert alert-danger" }, [
_c("strong", [_vm._v("Error")]),
_vm._v(" " + _vm._s(_vm.error) + "\n ")
])
: _vm._e(),
_vm._v(" "),
_c("label", [_c("b", [_vm._v(_vm._s(_vm.commentMessage))])]),
_vm._v(" "),
_c("div", { staticClass: "input-group" }, [
Expand Down
15 changes: 10 additions & 5 deletions imgbook/resources/js/components/CommentComponent.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<template>
<div id="root">
<div class="form-group">
<div class="alert alert-danger" v-if="error">
<strong>Error</strong> {{error}}
</div>
<label><b>{{ commentMessage }}</b></label>
<div class="input-group">
<input type="textarea" name="text" class="form-control" placeholder="Enter a comment" v-model="newComment">
Expand Down Expand Up @@ -32,6 +35,7 @@
data() {
return {
comments: [],
error: '',
newComment: '',
commentMessage: 'Post a comment',
changeOrPost: 'Post',
Expand All @@ -48,7 +52,6 @@
//success
this.comments = response.data;
this.newComment = '';
console.log(response.data);
})
.catch(response => {
//failure
Expand All @@ -72,10 +75,11 @@
//success
this.comments.push(response.data);
this.newComment = '';
this.error = '';
})
.catch(response => {
.catch(error => {
//failure
console.log(response);
this.error = error.response.data.error;
})
},
editCommentButton: function (comment) {
Expand All @@ -94,11 +98,12 @@
this.commentMessage = "Post a comment";
this.changeOrPost = "Post";
this.commentIdBeingEdited = null;
this.error = '';
this.fetchComments();
})
.catch(response => {
.catch(error => {
//failure
console.log(response);
this.error = error.response.data.error;
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions imgbook/resources/views/posts/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<small class="text-muted">Posted by: {{ $post->user->username }}</small>
<h5 class="card-title">{{ $post->title }}</h5>
</div>
@if ($post->user_id == $user_id)
@if ($post->user_id == Auth::user()->id)
<div class="col-md-auto">
<form method="GET"
action="{{ route('posts.edit', ['id' => $post->id]) }}">
Expand All @@ -27,7 +27,7 @@
</div>
<div class="col-md-auto">
<form method="POST"
action="{{ route('posts.destroy', ['user_id' => $user_id, 'id' => $post->id]) }}">
action="{{ route('posts.destroy', ['user_id' => Auth::user()->id, 'id' => $post->id]) }}">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
Expand Down

0 comments on commit 666fd0f

Please sign in to comment.