api code global jain
This commit is contained in:
8
app/Repositories/Api/Access/Thana/ThanaInterface.php
Normal file
8
app/Repositories/Api/Access/Thana/ThanaInterface.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Api\Access\Thana;
|
||||
|
||||
interface ThanaInterface
|
||||
{
|
||||
// public function getThanaDetail();
|
||||
}
|
||||
354
app/Repositories/Api/Access/Thana/ThanaRepository.php
Normal file
354
app/Repositories/Api/Access/Thana/ThanaRepository.php
Normal file
@@ -0,0 +1,354 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Api\Access\Thana;
|
||||
|
||||
use App\Models\Thana;
|
||||
use App\Models\Sant;
|
||||
use App\Models\ThanaMember;
|
||||
use App\Constant\Constant;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Repositories\Api\Access\Thana\ThanaInterface;
|
||||
|
||||
class ThanaRepository implements ThanaInterface
|
||||
{
|
||||
/**
|
||||
* @var Thana
|
||||
*/
|
||||
protected $thana;
|
||||
|
||||
/**
|
||||
* @param Thana $thana
|
||||
* ThanaRepository constructor.
|
||||
*
|
||||
*/
|
||||
public function __construct(Thana $thana)
|
||||
{
|
||||
$this->thana = $thana;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add member in thana
|
||||
*/
|
||||
public function storeThana(array $data)
|
||||
{
|
||||
$response = [
|
||||
'status' => Constant::CODE_403,
|
||||
'error' => trans('api.something_went_wrong'),
|
||||
'success' => Constant::STATUS_FALSE
|
||||
];
|
||||
try {
|
||||
$sant = Sant::find($data['sant_id']);
|
||||
|
||||
$thanaData = [
|
||||
'name' => randomStrings(9),
|
||||
'created_by' => loggedInUser()->id,
|
||||
'updated_by' => loggedInUser()->id,
|
||||
];
|
||||
|
||||
|
||||
|
||||
$thanaMember = ThanaMember::where('sant_id', $data['sant_id'])->select('thana_id')->first();
|
||||
|
||||
if (empty($thanaMember)) {
|
||||
if (empty($sant->ownedThana()->first())) {
|
||||
$thanaOfSant = $sant->ownedThana()->create($thanaData);
|
||||
} else {
|
||||
$thanaOfSant = $sant->ownedThana()->first();
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($thanaOfSant) && !empty($thanaOfSant)) {
|
||||
$createdThanaId = Thana::find($thanaOfSant->id);
|
||||
} else {
|
||||
$createdThanaId = Thana::find($thanaMember->thana_id);
|
||||
}
|
||||
|
||||
$oldMember = collect($createdThanaId->getThanaMember()->pluck('sant_id')->toArray());
|
||||
|
||||
if (isset($data['thana_sant_id']) && !empty($data['thana_sant_id'])) {
|
||||
$newMember = collect($data['thana_sant_id']);
|
||||
$merged = $oldMember->merge($newMember);
|
||||
$merged = $merged->push($data['sant_id']);
|
||||
// foreach ($data['thana_sant_id'] as $santId) {
|
||||
// $santRelation = [
|
||||
// 'sant_id' => $santId,
|
||||
// ];
|
||||
$createdThanaId->getThanaMember()->sync($merged->toArray());
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
if (!empty($createdThanaId)) {
|
||||
$response['data'] = $createdThanaId->toArray();
|
||||
$response['status'] = Constant::CODE_200;
|
||||
$response['success'] = Constant::STATUS_TRUE;
|
||||
$response['message'] = trans('api.thana.store');
|
||||
unset($response['error']);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete member in thana
|
||||
*/
|
||||
public function destroyThana(array $data)
|
||||
{
|
||||
$response = [
|
||||
'status' => Constant::CODE_403,
|
||||
'error' => trans('api.something_went_wrong'),
|
||||
'success' => Constant::STATUS_FALSE
|
||||
];
|
||||
try {
|
||||
$sant = Sant::find($data['sant_id']);
|
||||
|
||||
$thanaData = [
|
||||
'name' => randomStrings(9),
|
||||
'created_by' => loggedInUser()->id,
|
||||
'updated_by' => loggedInUser()->id,
|
||||
];
|
||||
|
||||
$thanaMember = ThanaMember::where('sant_id', $data['sant_id'])->select('thana_id')->first();
|
||||
|
||||
if (empty($thanaMember)) {
|
||||
if (empty($sant->ownedThana()->first())) {
|
||||
$thanaOfSant = $sant->ownedThana()->create($thanaData);
|
||||
} else {
|
||||
$thanaOfSant = $sant->ownedThana()->first();
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($thanaOfSant) && !empty($thanaOfSant)) {
|
||||
$createdThanaId = Thana::find($thanaOfSant->id);
|
||||
} else {
|
||||
$createdThanaId = Thana::find($thanaMember->thana_id);
|
||||
}
|
||||
|
||||
if (isset($data['thana_sant_id']) && !empty($data['thana_sant_id'])) {
|
||||
$santRelation = [
|
||||
'sant_id' => $data['thana_sant_id'],
|
||||
];
|
||||
|
||||
$createdThanaId->thanaMember()->where('sant_id', $data['thana_sant_id'])->update(['is_approved' => Constant::STATUS_ZERO]);
|
||||
}
|
||||
|
||||
if (!empty($createdThanaId)) {
|
||||
$response['data'] = $createdThanaId->toArray();
|
||||
$response['status'] = Constant::CODE_200;
|
||||
$response['success'] = Constant::STATUS_TRUE;
|
||||
$response['message'] = trans('api.thana.destroy');
|
||||
unset($response['error']);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete member in thana
|
||||
*/
|
||||
public function thanaMember(array $data)
|
||||
{
|
||||
$response = [
|
||||
'status' => Constant::CODE_403,
|
||||
'error' => trans('api.something_went_wrong'),
|
||||
'success' => Constant::STATUS_FALSE
|
||||
];
|
||||
try {
|
||||
$sant = Sant::find($data['sant_id']);
|
||||
|
||||
$thanaData = [
|
||||
'name' => randomStrings(9),
|
||||
'created_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
|
||||
];
|
||||
|
||||
$thanaMember = ThanaMember::where('sant_id', $data['sant_id'])->select('thana_id')->first();
|
||||
|
||||
if (empty($thanaMember)) {
|
||||
if (empty($sant->ownedThana()->first())) {
|
||||
$thanaOfSant = $sant->ownedThana()->create($thanaData);
|
||||
} else {
|
||||
$thanaOfSant = $sant->ownedThana()->first();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (isset($thanaOfSant) && !empty($thanaOfSant)) {
|
||||
$createdThanaId = Thana::find($thanaOfSant->id);
|
||||
} else {
|
||||
$createdThanaId = Thana::find($thanaMember->thana_id);
|
||||
}
|
||||
|
||||
$createdThanaData = $createdThanaId->getThanaMember()->orderBy('thana_members.is_leader', 'DESC')->with(['dharm', 'sampraday'])->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
||||
$ifLeaderExist = $createdThanaId->getThanaMember()->where('is_leader', Constant::STATUS_ONE)->first();
|
||||
|
||||
|
||||
if (!empty($createdThanaId)) {
|
||||
if ($ifLeaderExist) {
|
||||
$response['is_leader_exist'] = true;
|
||||
} else {
|
||||
$response['is_leader_exist'] = false;
|
||||
}
|
||||
$response['data'] = $createdThanaData;
|
||||
$response['status'] = Constant::CODE_200;
|
||||
$response['success'] = Constant::STATUS_TRUE;
|
||||
$response['message'] = trans('api.thana.store');
|
||||
unset($response['error']);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function updateThana(array $data, object $thana)
|
||||
{
|
||||
$response = [
|
||||
'status' => Constant::CODE_403,
|
||||
'error' => trans('api.something_went_wrong'),
|
||||
'success' => Constant::STATUS_FALSE
|
||||
];
|
||||
try {
|
||||
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get info of thana
|
||||
*/
|
||||
public function showThana(object $thana)
|
||||
{
|
||||
$response = [
|
||||
'status' => Constant::CODE_403,
|
||||
'error' => trans('api.something_went_wrong'),
|
||||
'success' => Constant::STATUS_FALSE
|
||||
];
|
||||
|
||||
try {
|
||||
$response['data'] = $thana->toArray();
|
||||
$response['status'] = Constant::CODE_200;
|
||||
$response['success'] = Constant::STATUS_TRUE;
|
||||
$response['message'] = trans('api.thana.list');
|
||||
unset($response['error']);
|
||||
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getListOfSant($data)
|
||||
{
|
||||
$response = [];
|
||||
|
||||
try {
|
||||
|
||||
$santIsMembetOfThana = ThanaMember::pluck('sant_id');
|
||||
|
||||
$santData = Sant::where('status', '2')->where('id', '!=', $data['sant_id'])->whereNotIn('id', $santIsMembetOfThana)->with(['dharm', 'sampraday']);
|
||||
|
||||
$getSantData = Sant::find($data['sant_id']);
|
||||
|
||||
if (isset($data['name']) && !empty($data['name'])) {
|
||||
$santData = $santData->where('name', 'LIKE', "%{$data['name']}%");
|
||||
}
|
||||
|
||||
// if (isset($getSantData['dharma_id']) && !empty($getSantData['dharma_id'])) {
|
||||
// $santData = $santData->where('dharma_id', $getSantData['dharma_id']);
|
||||
// }
|
||||
|
||||
if (isset($getSantData['sampraday_id']) && !empty($getSantData['sampraday_id'])) {
|
||||
$santData = $santData->where('sampraday_id', $getSantData['sampraday_id']);
|
||||
}
|
||||
|
||||
if (isset($getSantData['gender']) && !empty($getSantData['gender'])) {
|
||||
$santData = $santData->where('gender', $getSantData['gender']);
|
||||
}
|
||||
|
||||
$santData = $santData->paginate($data['limit'] ?? 10, ['*'], 'page', $data['page'] ?? 1);
|
||||
|
||||
$response['data'] = $santData;
|
||||
$response['message'] = trans('sant.list_sant_success');
|
||||
$response['status'] = Constant::CODE_200;
|
||||
|
||||
} catch (\Exception $ex) {
|
||||
Log::error($ex);
|
||||
$response['message'] = trans('sant.something_went_wrong');
|
||||
$response['status'] = Constant::CODE_403;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete member in thana
|
||||
*/
|
||||
public function thanaMakeLeader(array $data)
|
||||
{
|
||||
$response = [
|
||||
'status' => Constant::CODE_403,
|
||||
'error' => trans('api.something_went_wrong'),
|
||||
'success' => Constant::STATUS_FALSE
|
||||
];
|
||||
try {
|
||||
$sant = Sant::find($data['sant_id']);
|
||||
|
||||
$thanaData = [
|
||||
'name' => randomStrings(9),
|
||||
'created_by' => !empty(loggedInUser()->id) ? loggedInUser()->id : Constant::NULL,
|
||||
];
|
||||
|
||||
$thanaMember = ThanaMember::where('sant_id', $data['sant_id'])->select('thana_id')->first();
|
||||
|
||||
if (empty($thanaMember)) {
|
||||
if (empty($sant->ownedThana()->first())) {
|
||||
$thanaOfSant = $sant->ownedThana()->create($thanaData);
|
||||
} else {
|
||||
$thanaOfSant = $sant->ownedThana()->first();
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($thanaOfSant) && !empty($thanaOfSant)) {
|
||||
$createdThanaId = Thana::find($thanaOfSant->id);
|
||||
} else {
|
||||
$createdThanaId = Thana::find($thanaMember->thana_id);
|
||||
}
|
||||
|
||||
$createdThanaId->thanaMember()->where('sant_id', $data['thana_sant_id'])->update(['is_leader' => 1]);
|
||||
|
||||
if (!empty($thanaData)) {
|
||||
$response['data'] = $createdThanaId->toArray();
|
||||
$response['status'] = Constant::CODE_200;
|
||||
$response['success'] = Constant::STATUS_TRUE;
|
||||
$response['message'] = trans('api.thana.leader');
|
||||
unset($response['error']);
|
||||
}
|
||||
} catch (Exception $ex) {
|
||||
Log::error($ex);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user