Skip to content

Commit

Permalink
Latest changes featuring API controller
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanRudvin committed Jul 12, 2017
1 parent c39c64b commit 2ca587b
Show file tree
Hide file tree
Showing 23 changed files with 1,220 additions and 560 deletions.
129 changes: 129 additions & 0 deletions app/Http/Controllers/Api/CommentsApiController.php
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
]);
}
}
138 changes: 138 additions & 0 deletions app/Http/Controllers/Api/PostsApiController.php
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 app/Http/Controllers/Api/PostsCommentsApiController.php
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)
{
//
}
}
Loading

0 comments on commit 2ca587b

Please sign in to comment.