Files
crud-laravel/app/Http/Controllers/CommentController.php
2024-05-21 14:17:55 +08:00

138 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class CommentController extends Controller
{
/**
* Retrieve all comments with their associated user.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response containing all comments.
*/
public function get(Request $request)
{
$comments = Comment::with('user')->get();
return response()->json($comments, 200);
}
/**
* Create a new comment on a specific post.
*
* @param Request $request The HTTP request object.
* @param int $postId The ID of the post to comment on.
* @return \Illuminate\Http\JsonResponse A JSON response containing the created comment.
*/
//OLD-------------------
// public function post(Request $request, $postId)
// {
// $data = $request->all();
// $comment = new Comment($data);
// $comment->post_id = $postId;
// $comment->user_id = auth()->id();
// $comment->save();
// return response()->json($comment, 201);
// }
//NEW--------------------
public function post(Request $request)
{
$validator = Validator::make($request->all(), [
'content' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
// return response()->json("$request->post_id", 401);
$comment = Comment::create([
'content' => $request->content,
'user_id' => auth()->id(),
'post_id' => $request->post_id
]);
return response()->json($comment, 201);
}
/**
* Retrieve all comments for a specific post.
*
* @param Request $request The HTTP request object.
* @param int $postId The ID of the post to retrieve comments for.
* @return \Illuminate\Http\JsonResponse A JSON response containing the comments for the specified post.
*/
public function getPostComments(Request $request, $postId)
{
$comments = Comment::where('post_id', $postId)->with('user')->get();
return response()->json($comments, 200);
}
/**
* Retrieve a specific comment by its ID.
*
* @param Request $request The HTTP request object.
* @param int $commentId The ID of the comment to retrieve.
* @return \Illuminate\Http\JsonResponse A JSON response containing the specified comment.
*/
public function getComment(Request $request, $commentId)
{
$comment = Comment::with('user')->find($commentId);
error_log($comment);
return response()->json($comment, 200);
}
/**
* Update an existing comment.
*
* @param Request $request The HTTP request object.
* @param int $commentId The ID of the comment to update.
* @return \Illuminate\Http\JsonResponse A JSON response containing the updated comment or an error message.
*/
//OLD--------------------------------
// public function put(Request $request, $commentId)
// {
// $comment = Comment::find($commentId);
// if ($comment && $comment->user_id === auth()->id()) {
// $data = $request->all();
// $comment->update($data);
// return response()->json($comment, 200);
// } else {
// return response()->json(['error' => 'You are not authorized to update this comment'], 401);
// }
// }
//NEW---------------------------
public function put(Request $request)
{
// below section is REQUIRED
$validator = Validator::make($request->all(), [
'content' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$data = [
'content' => $request->content
];
// above section is REQUIRED
//only owner can update the
$comment = Comment::where([
'id' => $request->id,
'user_id' => auth()->id(),
])->first();
//return empty post
if (!$comment) {
return response()->json(['error' => 'You are not authorized to update this comment'], 401);
}
$comment->update($data);
$comment->load('user');
//send updated data of comment
return response()->json($comment, 200);
}
}