Files
Global-Jain/app/Repositories/Api/Access/ChaturmasReview/ChaturmasReviewRepository.php

182 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2025-11-05 10:37:10 +05:30
<?php
namespace App\Repositories\Api\Access\ChaturmasReview;
use App\Constant\Constant;
use App\Models\ChaturmasReview;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class ChaturmasReviewRepository implements ChaturmasReviewInterface
{
/**
* @var ChaturmasReview
*/
protected $chaturmasReview;
/**
* @param Chaturmas $chaturmasReview
* ChaturmasRepository constructor.
*
*/
public function __construct(ChaturmasReview $chaturmasReview)
{
$this->chaturmasReview = $chaturmasReview;
}
/**
*
*/
public function getChaturmasReviewList(array $data)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = [];
$allChaturmasReviewObject = $this->chaturmasReview->query()
->join('chaturmas_dates', 'chaturmas_dates.id', '=', 'chaturmas.chaturmas_date_id')
->select('chaturmas.*')
->with(['chaturmasInfo', 'createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->where('sant_id', $data['sant_id']);
$allChaturmasReviewObject = $allChaturmasReviewObject
->orderBy('chaturmas_dates.year', 'DESC')
->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
$response['data'] = $allChaturmasReviewObject;
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas_review.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
*
*/
public function storeChaturmasReview(array $data, object $chaturmas)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
$chaturmasReviewData = [
'user_id' => $user->id,
'sant_id' => $data['sant_id'],
'sant_id' => $data['sant_id'],
'chaturmas_id' => $chaturmas->id,
'comment' => $data['comment'],
'created_by' => $user->id
];
$chaturmasReview = $chaturmas->reviews()->create($chaturmasReviewData);
event(new \App\Events\WrongChaturmas($user, $data['sant_id'], $chaturmas->id, $data['comment']));
if (!empty($chaturmasReview)) {
$response['data'] = $chaturmasReview->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas_review.store');
unset($response['error']);
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
*
*/
public function updateChaturmasReview(array $data, object $chaturmasReview)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$user = loggedInUser();
if (empty($user->mobile)) {
$response['error'] = trans('api.mobile');
} else {
$chaturmasReviewExistForYear = $this->chaturmasReview->query()->with(['createdBy:id,name,mobile,country_code','updatedBy:id,name,mobile,country_code'])->where('id', '!=', $chaturmasReview->id)->where('sant_id', $chaturmasReview->sant_id)->where('chaturmas_date_id', $data['year'])->first();
$response['status'] = Constant::CODE_403;
$response['success'] = Constant::STATUS_FALSE;
$response['error'] = trans('api.chaturmas_review.exist', ['year' => $data['year']]);
if (empty($chaturmasReviewExistForYear)) {
$id = $chaturmasReview->id;
$chaturmasReview = $chaturmasReview->update(
[
'place' => $data['place'],
'chaturmas_date_id' => $data['year'],
// 'sant_id' => $data['sant_id'],
'updated_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
]
);
if (!empty($chaturmasReview)) {
$chaturmasReview = $this->chaturmasReview->find($id)->load('chaturmasInfo', 'createdBy:id,name,mobile,country_code', 'updatedBy:id,name,mobile,country_code');
$response['data'] = $chaturmasReview->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas_review.update');
unset($response['error']);
}
}
}
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
/**
* Get info of chaturmas years
*/
public function showChaturmasReview(object $chaturmasReview)
{
$response = [
'status' => Constant::CODE_403,
'error' => trans('api.something_went_wrong'),
'success' => Constant::STATUS_FALSE
];
try {
$response['data'] = $chaturmasReview->load('chaturmasInfo')->toArray();
$response['status'] = Constant::CODE_200;
$response['success'] = Constant::STATUS_TRUE;
$response['message'] = trans('api.chaturmas_review.list');
unset($response['error']);
} catch (Exception $ex) {
Log::error($ex);
}
return $response;
}
}