• File: PostCommentController.php
  • Full Path: /var/www/paytekchalenge/app/Http/Controllers/PostCommentController.php
  • Date Modified: 07/19/2022 7:23 PM
  • File size: 986 bytes
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Http\Controllers;

use App\Models\PostComment;
use Illuminate\Http\Request;

class PostCommentController extends Controller
{
    public function addComment(Request $request)
    {
        PostComment::create([
            'comment' => $request->comment,
            'post_id'=> $request->post_id,
            'member_id' => $request->user()->id
        ]);
        return response()->json(['message' => 'Comentário Adicionado com Sucesso'], 200);
    }

    public function getComments($id)
    {
        $comments = PostComment::query()
            ->join('posts', 'posts.id', '=', 'post_comments.post_id')
            ->join('members', 'members.id', '=', 'post_comments.member_id')
            ->where('post_comments.post_id', $id)
            ->select('post_comments.*', 'members.name as username', 'posts.id as post_id', 'members.photo')
            ->get();

        return response()->json(['data' =>$comments], 200);
    }
}