-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Latest changes featuring API controller
- Loading branch information
1 parent
c39c64b
commit 2ca587b
Showing
23 changed files
with
1,220 additions
and
560 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Comment; | ||
use Illuminate\Support\Facades\Request; | ||
use App\Http\Controllers\Controller; | ||
|
||
class CommentsApiController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
$comments = Comment::all(); | ||
|
||
return response()->json([ | ||
'comments' => $comments | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function create() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
$this->validate($request, [ | ||
'content' => 'required' | ||
]); | ||
$comment = Comment::create([ | ||
'content' => $request->input('content'), | ||
'user_id' => Auth::user()->id, | ||
]); | ||
return response()->json([ | ||
'comment' => $comment | ||
]); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function show(Comment $comment) | ||
{ | ||
$comment->owner = $comment->user->name; | ||
|
||
$comment->time = $comment->created_at->diffforHumans(); | ||
|
||
$comment->score = $comment->likesCount(); | ||
|
||
$comment->img = $comment->user->avatar; | ||
|
||
return response()->json([ | ||
'comment' => $comment | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function edit(Comment $comment) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, Comment $comment) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Comment $comment) | ||
{ | ||
// | ||
} | ||
|
||
public function ToggleLike(Comment $comment) | ||
{ | ||
$comment->toggleLike(); | ||
$comment->save(); | ||
$comment->score = $comment->likesCount(); | ||
$comment->liked = $comment->isLiked(); | ||
return response()->json([ | ||
'comment' => $comment | ||
]); | ||
} | ||
|
||
|
||
public function isLiked(Comment $comment) | ||
{ | ||
$liked = $comment->isLiked(); | ||
return response()->json([ | ||
'liked' => $liked | ||
]); | ||
} | ||
} |
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,138 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Post; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
|
||
class PostsApiController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
$posts = Post::all(); | ||
|
||
foreach( $posts as $post ) { | ||
$post->owner = $post->user->name; | ||
$post->time = $post->created_at->diffforHumans(); | ||
} | ||
|
||
return response()->json([ | ||
'posts' => $posts | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function create() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
$this->validate($request, [ | ||
'content' => 'required|min:1', | ||
'title' => 'required|min:1' | ||
]); | ||
|
||
$post = new Post(); | ||
$post->title = $request->input('content'); | ||
$post->content = $request->input('title'); | ||
$post->user_id = Auth::user()->id; | ||
|
||
# Image handling | ||
// $avatar = $request->file('image'); | ||
// $filename = time() . '.' . $avatar->getClientOriginalExtension(); | ||
// Image::make($avatar)->save( public_path('/uploads/images/' . $filename)); | ||
// $post->image = $filename; | ||
|
||
$post->save(); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param \App\Post $post | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function show(Post $post) | ||
{ | ||
$post->userImg = $post->user->avatar; | ||
$post->owner = $post->user->name; | ||
$post->time = $post->created_at->diffforHumans(); | ||
$post->score = $post->likesCount(); | ||
|
||
return response()->json([ | ||
'post' => $post | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
* | ||
* @param \App\Post $post | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function edit(Post $post) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \App\Post $post | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, Post $post) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param \App\Post $post | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Post $post) | ||
{ | ||
// | ||
} | ||
|
||
public function ToggleLike(Post $post) | ||
{ | ||
$post->toggleLike(); | ||
$post->save(); | ||
$post->score = $post->likesCount(); | ||
$post->liked = $post->isLiked(); | ||
return response()->json([ | ||
'post' => $post | ||
]); | ||
} | ||
|
||
|
||
public function isLiked(Post $post) | ||
{ | ||
$liked = $post->isLiked(); | ||
return response()->json([ | ||
'liked' => $liked | ||
]); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
app/Http/Controllers/Api/PostsCommentsApiController.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,117 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api; | ||
|
||
use App\Comment; | ||
use App\Post; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
|
||
class PostsCommentsApiController extends Controller | ||
{ | ||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index(Post $post) | ||
{ | ||
$comments = $post->comments()->get(); | ||
|
||
foreach( $comments as $comment ) { | ||
|
||
$comment->owner = $comment->user->name; | ||
|
||
$comment->time = $comment->created_at->diffforHumans(); | ||
|
||
$comment->score = $comment->likesCount(); | ||
|
||
$comment->img = $comment->user->avatar; | ||
|
||
} | ||
|
||
return response()->json([ | ||
'comments' => $comments | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for creating a new resource. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function create() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function show(Post $post, $commentsid) | ||
{ | ||
$comments = $post->comments()->get(); | ||
|
||
$comment = $comments[$commentsid - 1]; | ||
|
||
$comment->owner = $comment->user->name; | ||
|
||
$comment->time = $comment->created_at->diffforHumans(); | ||
|
||
$comment->score = $comment->likesCount(); | ||
|
||
#$comment->img = $comment->user->avatar; | ||
|
||
return response()->json([ | ||
'comment' => $comment | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function edit(Comment $comment) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function update(Request $request, Comment $comment) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param \App\Comment $comment | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function destroy(Comment $comment) | ||
{ | ||
// | ||
} | ||
} |
Oops, something went wrong.