%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/news/app/Http/Controllers/Api/
Upload File :
Create Path :
Current File : /var/www/html/news/app/Http/Controllers/Api/ArticleCommentsController.php

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Article\ArticleStoreRequest;
use App\Http\Requests\Article\ArticleUpdateRequest;
use App\Http\Requests\Comment\CommentStoreRequest;
use App\Repositories\Article\ArticlesRepository;
use App\Repositories\Comment\CommentsRepository;
use App\Repositories\Tag\TagRepository;
use App\Transformers\CommentTransformer;
use App\Transformers\TagTransformer;
use EllipseSynergie\ApiResponse\Contracts\Response;
use Illuminate\Http\Request;

class ArticleCommentsController extends ApiController
{
    /**
     * @var CommentTransformer
     */
    private $transformer;
    /**
     * @var ArticlesRepository
     */
    private $articlesRepository;
    /**
     * @var CommentsRepository
     */
    private $commentsRepository;

    /**
     * ArticleCommentsController constructor.
     * @param Response $response
     * @param CommentTransformer $transformer
     * @param ArticlesRepository $articlesRepository
     * @param CommentsRepository $commentsRepository
     */
    public function __construct(Response $response, CommentTransformer $transformer, ArticlesRepository $articlesRepository, CommentsRepository $commentsRepository)
    {
        parent::__construct($response);
        $this->transformer = $transformer;
        $this->articlesRepository = $articlesRepository;
        $this->commentsRepository = $commentsRepository;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $article)
    {
        $article = $this->articlesRepository->findOrFail($article);
        $comments = $article->comments()->getQuery()->where('is_approved', true)->get();
        return $this->response->withCollection($comments, $this->transformer);
    }

    /**
     * @param CommentStoreRequest $request
     * @param $article
     * @return mixed
     */
    public function store(CommentStoreRequest $request, $article)
    {
        $article = $this->articlesRepository->findOrFail($article);
        $comment = $this->commentsRepository->create($request->all());
        return $this->response->withItem($comment, $this->transformer);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @param $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $article, $comment)
    {
        $article = $this->articlesRepository->findOrFail($article);
        $comment = $article->comments()->findOrFail($comment);
        $result = $this->commentsRepository->delete($comment->id);
        if (!$result) {
            return $this->response->errorInternalError('Unexpected error occurred');
        }
        return response([], 204);
    }

    public function approve(Request $request, $article, $comment)
    {
        $article = $this->articlesRepository->findOrFail($article);
        $comment = $article->comments()->findOrFail($comment);
        $comment->is_approved = true;
        $comment->save();
        return $this->response->withItem($comment, $this->transformer);
    }

}

Zerion Mini Shell 1.0