Files
Global-Jain/app/Http/Controllers/Api/V1/Access/PostController.php
2025-11-05 10:37:10 +05:30

335 lines
9.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Access;
use App\Constant\Constant;
use App\Http\Controllers\Controller;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Requests\Api\Post\StorePostRequest;
use App\Http\Requests\Api\Post\UpdatePostRequest;
use App\Repositories\Api\Access\Post\PostInterface as PostRepository;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Api\ApiController;
use App\Http\Requests\Api\Post\GetSantPostRequest;
use App\Http\Requests\Api\Post\PostAddCommentRequest;
use App\Http\Requests\Api\Post\PostLikeRequest;
use App\Http\Requests\Api\Post\PostRemoveCommentRequest;
use App\Http\Requests\Api\Post\PostUpdateCommentRequest;
use App\Models\PostComment;
use App\Policies\ReplyPolicy;
class PostController extends ApiController
{
/**
* @var Post
*/
protected $postRepository;
/**
* @param PostRepository $userRepo
* PostController constructor.
*/
public function __construct(PostRepository $postRepository)
{
$this->postRepository = $postRepository;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
try {
$response = $this->postRepository->getAllPosts($request);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function getUserPost(Request $request)
{
try {
$response = $this->postRepository->getUserPost($request->all());
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
try {
$response = $this->postRepository->storePost($request);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Display the specified resource.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function show(Post $post)
{
try {
$response = $this->postRepository->showPost($post);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function update(UpdatePostRequest $request, Post $post)
{
try {
$response = $this->postRepository->updatePost($request, $post);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Post $post
* @return \Illuminate\Http\Response
*/
public function destroy(Post $post)
{
try {
$response = $this->postRepository->destroyPost($post);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Display a listing of the categorty.
*
* @return \Illuminate\Http\Response
*/
public function getCategory()
{
try {
$response = $this->postRepository->getCategory();
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
/**
* Like on post.
*
* @return \Illuminate\Http\Response
*/
public function likePost(PostLikeRequest $request)
{
try {
$response = $this->postRepository->likePost($request->all());
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
/**
* Like on post.
*
* @return \Illuminate\Http\Response
*/
public function addComment(PostAddCommentRequest $request)
{
try {
$response = $this->postRepository->addComment($request->all());
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
/**
* Like on post.
*
* @return \Illuminate\Http\Response
*/
public function updateComment(PostUpdateCommentRequest $request)
{
try {
$reply = PostComment::findOrFail($request['comment_id']);
$this->authorize(ReplyPolicy::UPDATE, $reply);
$response = $this->postRepository->updateComment($request->all());
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
/**
* Like on post.
*
* @return \Illuminate\Http\Response
*/
public function removeComment(PostRemoveCommentRequest $request)
{
try {
$response = $this->postRepository->removeComment($request->all());
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
/**
* Get a previous draft post.
*
* @return \Illuminate\Http\Response
*/
public function getDraftPost()
{
try {
$response = $this->postRepository->getDraftPost();
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
/**
* Display a listing of the sant post.
*
* @return \Illuminate\Http\Response
*/
public function getSantPost(GetSantPostRequest $request)
{
try {
$response = $this->postRepository->getSantPost($request);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Display a listing of the sant post.
*
* @return \Illuminate\Http\Response
*/
public function getPostComments(Request $request, Post $post)
{
try {
$response = $this->postRepository->getPostComments($request->all(), $post);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Display a listing of the sant post.
*
* @return \Illuminate\Http\Response
*/
public function getSanghPost(GetSantPostRequest $request)
{
try {
$response = $this->postRepository->getSanghPost($request);
$this->updateStatusCode($response);
return $this->respond($response);
} catch (\Exception $ex) {
Log::error($ex);
return $this->respondInternalError(trans('api.something_went_wrong'));
}
}
/**
* Hide post.
*
* @return \Illuminate\Http\Response
*/
public function hidePost(Request $request)
{
try {
$response = $this->postRepository->hidePost($request->all());
$this->setStatusCode($response['status']);
} catch (\Exception $ex) {
$response['message'] = $ex->getMessage();
$this->setStatusCode(Constant::CODE_403);
}
return $this->respond($response);
}
}