Skip to content

Commit

Permalink
Admin fully implemented with deletion of comments and posts
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbigbear committed Dec 20, 2019
1 parent 520e4b3 commit 60588c1
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 53 deletions.
2 changes: 1 addition & 1 deletion imgbook/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RegisterController extends Controller
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand Down
16 changes: 14 additions & 2 deletions imgbook/app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use App\Profanity;
use App\Comment;
Expand Down Expand Up @@ -148,8 +149,19 @@ public function update(Request $request, $id, Profanity $p)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function apiDestroy($id, $post_id)
{
//
$comment = Comment::findOrFail($id);
$post = Post::findOrFail($post_id);

if($comment->user_id == (Auth::user()->id) || Auth::user()->type == 'admin') {
$comment->delete();
$message = array(
'message' => 'Comment deleted'
);
return response($message,200);
}

return redirect()->route('posts.index')->with('message', 'Unauthorized to delete');
}
}
28 changes: 0 additions & 28 deletions imgbook/app/Http/Controllers/HomeController.php

This file was deleted.

9 changes: 5 additions & 4 deletions imgbook/app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function store(Request $request, Profanity $p)
$image = $validatedData['image'];
$tags = [];

if (sizeOf($validatedData['checkbox']) != 0) {
if (isset($validatedData['checkbox'])) {
$checkboxes = $validatedData['checkbox'];
for ($i=0; $i < 10; $i++) {
if(array_key_exists($i, $checkboxes)) {
Expand All @@ -96,9 +96,11 @@ public function store(Request $request, Profanity $p)
$a->title = $validatedData['title'];
$a->description = $validatedData['description'];
$a->user_id = Auth::user()->id;
$a->tags()->attach($tags);
$a->save();

$post = Post::findOrFail($a)->last();
$post->tags()->attach($tags);

\App::call('App\Http\Controllers\ImageController@store', ['image' => $image, 'post_id' => $a->id]);

session()->flash('message', 'Post created');
Expand Down Expand Up @@ -176,7 +178,6 @@ public function update(Request $request, $id, Profanity $p)
if(array_key_exists($i, $checkboxes)) {
array_push($tags, $i);
}

}
}

Expand All @@ -201,7 +202,7 @@ public function destroy($userId, $id)
{
$post = Post::findOrFail($id);

if($post->user_id == $userId) {
if($post->user_id == $userId || Auth::user()->type == 'admin') {
$post->delete();
return redirect()->route('posts.index')->with('message', 'Post Deleted');
}
Expand Down
81 changes: 68 additions & 13 deletions imgbook/public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1869,12 +1869,19 @@ __webpack_require__.r(__webpack_exports__);
//
//
//
//
//
//
//
//
/* harmony default export */ __webpack_exports__["default"] = ({
props: ['postId', 'userId'],
props: ['postId', 'userId', 'userType'],
data: function data() {
return {
comments: [],
error: '',
success: '',
admin: 'admin',
newComment: '',
commentMessage: 'Post a comment',
changeOrPost: 'Post',
Expand Down Expand Up @@ -1905,22 +1912,35 @@ __webpack_require__.r(__webpack_exports__);
this.putComment();
}
},
postComment: function postComment() {
deleteComment: function deleteComment(comment) {
var _this2 = this;

axios["delete"]("/comment/" + comment.id + "/" + this.postId).then(function (response) {
//success
_this2.fetchComments();

_this2.success = response.message;
})["catch"](function (error) {
//failure
_this2.error = error.response.data.error;
});
},
postComment: function postComment() {
var _this3 = this;

axios.post("/comments", {
text: this.newComment,
post_id: this.postId,
user_id: this.userId
}).then(function (response) {
//success
_this2.comments.unshift(response.data);
_this3.comments.unshift(response.data);

_this2.newComment = '';
_this2.error = '';
_this3.newComment = '';
_this3.error = '';
})["catch"](function (error) {
//failure
_this2.error = error.response.data.error;
_this3.error = error.response.data.error;
});
},
editCommentButton: function editCommentButton(comment) {
Expand All @@ -1930,21 +1950,21 @@ __webpack_require__.r(__webpack_exports__);
this.commentIdBeingEdited = comment;
},
putComment: function putComment() {
var _this3 = this;
var _this4 = this;

axios.put("/comments/" + this.commentIdBeingEdited.id, {
text: this.newComment
}).then(function (response) {
//success
_this3.fetchComments();
_this4.fetchComments();

_this3.commentMessage = "Post a comment";
_this3.changeOrPost = "Post";
_this3.commentIdBeingEdited = null;
_this3.error = '';
_this4.commentMessage = "Post a comment";
_this4.changeOrPost = "Post";
_this4.commentIdBeingEdited = null;
_this4.error = '';
})["catch"](function (error) {
//failure
_this3.error = error.response.data.error;
_this4.error = error.response.data.error;
});
}
}
Expand Down Expand Up @@ -37356,6 +37376,13 @@ var render = function() {
])
: _vm._e(),
_vm._v(" "),
_vm.success
? _c("div", { staticClass: "alert alert-success" }, [
_c("strong", [_vm._v("Success")]),
_vm._v(" " + _vm._s(_vm.success) + "\n ")
])
: _vm._e(),
_vm._v(" "),
_c("label", [_c("b", [_vm._v(_vm._s(_vm.commentMessage))])]),
_vm._v(" "),
_c("div", { staticClass: "input-group" }, [
Expand Down Expand Up @@ -37433,6 +37460,34 @@ var render = function() {
},
[_vm._v("Edit")]
)
: _vm._e(),
_vm._v(" "),
comment.user.id == _vm.userId
? _c(
"button",
{
staticClass: "btn btn-danger pull-right",
on: {
click: function($event) {
return _vm.editCommentButton(comment)
}
}
},
[_vm._v("Delete")]
)
: _vm.userType == _vm.admin
? _c(
"button",
{
staticClass: "btn btn-danger pull-right",
on: {
click: function($event) {
return _vm.deleteComment(comment)
}
}
},
[_vm._v("Delete")]
)
: _vm._e()
])
])
Expand Down
23 changes: 21 additions & 2 deletions imgbook/resources/js/components/CommentComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<div class="alert alert-danger" v-if="error">
<strong>Error</strong> {{error}}
</div>
<div class="alert alert-success" v-if="success">
<strong>Success</strong> {{success}}
</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 All @@ -22,6 +25,8 @@
</div>
<div class="col-md-auto">
<button class="btn btn-primary pull-right" id="editButton" v-if="comment.user.id == userId" @click="editCommentButton(comment)">Edit</button>
<button class="btn btn-danger pull-right" v-if="comment.user.id == userId" @click="deleteComment(comment)">Delete</button>
<button class="btn btn-danger pull-right" v-else-if="userType == admin" @click="deleteComment(comment)">Delete</button>
</div>
</div>
</div>
Expand All @@ -31,19 +36,21 @@

<script type="application/javascript">
export default {
props: ['postId', 'userId'],
props: ['postId', 'userId', 'userType'],
data() {
return {
comments: [],
error: '',
success: '',
admin: 'admin',
newComment: '',
commentMessage: 'Post a comment',
changeOrPost: 'Post',
commentIdBeingEdited: Object
}
},
mounted() {
this.fetchComments()
this.fetchComments();
},
methods: {
fetchComments: function() {
Expand All @@ -66,6 +73,18 @@
this.putComment();
}
},
deleteComment: function (comment) {
axios.delete("/comment/" + comment.id + "/" + this.postId)
.then(response => {
//success
this.fetchComments();
this.success = response.message;
})
.catch(error => {
//failure
this.error = error.response.data.error;
})
},
postComment: function () {
axios.post("/comments", {
text: this.newComment,
Expand Down
4 changes: 3 additions & 1 deletion imgbook/resources/views/posts/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

@section('title', 'Posts')
@section('back')
<small class="text-muted">Logged in as: {{ Auth::user()->username }}</small>
@auth
<small class="text-muted">Logged in as: {{ Auth::user()->username }}</small>
@endauth
@endsection
@section('content')
<div class="row justify-content-center">
Expand Down
5 changes: 4 additions & 1 deletion imgbook/resources/views/posts/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
@endif
</div>
<p class="card-text">{{ $post->description }}</p>
<comment-component :post-id={{$post->id}} :user-id={{Auth::user()->id}}></comment-component>
<comment-component :post-id={{$post->id}}
:user-id={{Auth::user()->id}}
:user-type={{ json_encode(Auth::user()->type) }}>
</comment-component>
</div>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion imgbook/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@
Route::put('comments/{id}', 'CommentController@update')
->name('comments.update')->middleware('auth');

Route::get('/home', 'HomeController@index')->name('home');
Route::delete('comment/{id}/{post_id}', 'CommentController@apiDestroy')
->name('api.comment.destroy')->middleware('auth');

0 comments on commit 60588c1

Please sign in to comment.