Created
June 8, 2020 06:42
-
-
Save myathtut-zafir/8a35f241c7517ab61ad1a733ddb5d04a to your computer and use it in GitHub Desktop.
Election Region API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api; | |
use App\Http\Controllers\Traits\APIResponser; | |
use App\Http\Controllers\Controller; | |
use App\Http\Resources\ElectionRegionCollection; | |
use App\Models\AmyothaElectionRegion; | |
use App\Models\District; | |
use App\Models\PyiThuElectionRegion; | |
use App\Models\StateElectionRegion; | |
use App\Models\StateEthnicElectionRegion; | |
use App\Models\Township; | |
use Illuminate\Http\Request; | |
class ElectionRegionApiController extends Controller | |
{ | |
use APIResponser; | |
function getFamousElectionRegion() | |
{ | |
$stateElectionRegions = StateElectionRegion::with(['representative.party'])->get(); | |
$pyiThuElectionRegion = PyiThuElectionRegion::with(['representative.party'])->get(); | |
$amyothaElectionRegion = AmyothaElectionRegion::with(['representative.party'])->get(); | |
$mergeStateAndAmyotarElectionRegion = $stateElectionRegions->toBase()->merge($amyothaElectionRegion); //merge state and amyotha election region. | |
$mergePyiThuElectionRegion = $mergeStateAndAmyotarElectionRegion->toBase()->merge($pyiThuElectionRegion); //merge pyithu election region. | |
return response()->json(ElectionRegionCollection::collection($mergePyiThuElectionRegion)); | |
} | |
function getStateElectionRegion(Request $request) | |
{ | |
if ($request->input('divisionId') != 0 && $request->input('townshipId') != 0) { | |
$stateElectionRegions = StateElectionRegion::with(['representative.party']) | |
->where('division_id', $request->input('divisionId')) | |
->where('township_id', $request->input('townshipId')) | |
->get(); | |
} else { | |
$stateElectionRegions = StateElectionRegion::with(['representative.party']) | |
->where('division_id', $request->input('divisionId')) | |
->get(); | |
} | |
return response()->json(ElectionRegionCollection::collection($stateElectionRegions)); | |
} | |
function getPyiThuElectionRegion(Request $request) | |
{ | |
if ($request->input('divisionId') != 0 && $request->input('townshipId') != 0) { | |
$pyithuElectionRegion = PyiThuElectionRegion::with(['representative.party']) | |
->where('division_id', $request->input('divisionId')) | |
->where('township_id', $request->input('townshipId')) | |
->get(); | |
} else { | |
$pyithuElectionRegion = PyiThuElectionRegion::with(['representative.party']) | |
->where('division_id', $request->input('divisionId')) | |
->get(); | |
} | |
return response()->json(ElectionRegionCollection::collection($pyithuElectionRegion)); | |
} | |
function getAmyotarElectionRegion(Request $request) | |
{ | |
if ($request->input('divisionId') != 0 && $request->input('townshipId') != 0) { | |
$township = Township::where('id', $request->input('townshipId'))->first(); | |
$district = District::where('id', $township->district_id)->first(); | |
$amyotharElectionRegionIds = [$township->amyothaElectionRegion->id, $district->amyotha_election_region_id]; | |
$amyotarElectionRegion = AmyothaElectionRegion::with(['representative.party']) | |
->whereIn('id', $amyotharElectionRegionIds) | |
->get(); | |
} else { | |
$amyotarElectionRegion = AmyothaElectionRegion::with(['representative.party']) | |
->where('division_id', $request->input('divisionId')) | |
->get(); | |
} | |
return response()->json(ElectionRegionCollection::collection($amyotarElectionRegion)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment