Skip to content

Commit

Permalink
Refactor OSC resource and controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisGautier committed Dec 27, 2023
1 parent 145bfe1 commit 9fbf8bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 28 deletions.
33 changes: 26 additions & 7 deletions app/Filament/Resources/OscResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,37 +190,56 @@ public static function getPages(): array
public static function getEloquentQuery(): Builder
{
if( auth()->user()->role == 3) {
return parent::getEloquentQuery()->where('pays', 'Togo')
return parent::getEloquentQuery()->where(function ($query) {
$query->where('pays', '=', 'Togo');
})


->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
if( auth()->user()->role == 4) {
return parent::getEloquentQuery()->where('pays', 'Benin')->orWhere('pays', 'Bénin')
return parent::getEloquentQuery()->where(function ($query) {
$query->where('pays', '=', 'Benin')
->orWhere('pays', '=', 'Bénin');
})
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
if( auth()->user()->role == 5) {
return parent::getEloquentQuery()->where('pays', 'Cameroun')
if(auth()->user()->role == 5) {
return parent::getEloquentQuery()->where(function ($query) {
$query->where('pays', '=', 'Cameroun');
})

->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
if( auth()->user()->role == 6) {
return parent::getEloquentQuery()->where('pays', 'Senegal')->orWhere('pays', 'Sénégal')
return parent::getEloquentQuery()->where(function ($query) {
$query->where('pays','=', 'Senegal')->orWhere('pays','=', 'Sénégal');
})

->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
if( auth()->user()->role == 7) {
return parent::getEloquentQuery()->where('pays', 'Cote d\'ivoire')->orWhere('pays', 'Côte d\'ivoire')->orWhere('pays', 'Côte d\'Ivoire')->orWhere('pays', 'Cote d\'Ivoire')
return parent::getEloquentQuery()->where(function ($query) {
$query ->where('pays','=', 'Cote d\'ivoire')->orWhere('pays', '=', 'Côte d\'ivoire')->orWhere('pays','=', 'Côte d\'Ivoire')->orWhere('pays','=', 'Cote d\'Ivoire');
})

->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
if( auth()->user()->role == 8) {
return parent::getEloquentQuery()->where('pays', 'Tanzania')
return parent::getEloquentQuery()->where(function ($query) {
$query->where('pays','=', 'Tanzania');
})

->withoutGlobalScopes([
SoftDeletingScope::class,
]);
Expand Down
11 changes: 7 additions & 4 deletions app/Http/Controllers/Api/OddController.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ public function destroy($id)
// generate sql query to count all oscs by odd id and return the number of oscs associated to this odd by categorieOdd
public function countOscByOdd($idOdd)
{
$sql = "SELECT COUNT(DISTINCT osc_categorie_odds.osc_id) as count FROM osc_categorie_odds
INNER JOIN categorie_odds ON osc_categorie_odds.categorie_odd_id = categorie_odds.id
INNER JOIN odds ON categorie_odds.id_odd = odds.id
WHERE odds.id = $idOdd";
$sql = "SELECT COUNT(DISTINCT osc_categorie_odds.osc_id) as count
FROM osc_categorie_odds
INNER JOIN categorie_odds ON osc_categorie_odds.categorie_odd_id = categorie_odds.id
INNER JOIN odds ON categorie_odds.id_odd = odds.id
INNER JOIN oscs ON osc_categorie_odds.osc_id = oscs.id
WHERE odds.id = $idOdd
AND oscs.active = true";
$count = DB::select($sql);
return $count[0]->count;
}
Expand Down
51 changes: 34 additions & 17 deletions app/Http/Controllers/Api/OscController.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,39 @@ public function destroy($id)
* @responseFile storage/responses/searchosc.json
*/
public function searchOsc(Request $request)
{
$idsCategorieOdd = explode(',', $request->idsCategorieOdd);
{
$idsCategorieOdd = explode(',', $request->idsCategorieOdd);

$data = array();

$data = array();
foreach ($idsCategorieOdd as $iValue) {
$categorieOdd = CategorieOdd::find($iValue);
$categorieOdd->oscs;

foreach ($idsCategorieOdd as $iValue) {
$categorieOdd = CategorieOdd::find($iValue);
$categorieOdd->oscs;
foreach ($categorieOdd->oscs as $osc) {
foreach ($categorieOdd->oscs as $osc) {
// Ajoutez une condition pour vérifier si l'OSC est active
if ($osc->active == 1) {
$osc->user;

foreach ($osc->categorieOdds as $categorieOdd) {
$categorieOdd->odd;
}

$osc->zoneInterventions;

$bool = $this->checkIfOscInDataArray($data, $osc);

if (!$bool) {
$data[] = $osc;
}
}
}

return $this->sendResponse($data, 'OSC retrieved successfully.');
}

return $this->sendResponse($data, 'Active OSC retrieved successfully.');
}





Expand All @@ -338,23 +347,31 @@ public function searchOsc(Request $request)
* @urlParam q string required the query search. Example: ONG
* @responseFile storage/responses/getoscs.json
*/
public function searchOscByQuery(Request $request)
{
$q = $request->input('q');
$oscs = OSC::search($q)->where('active', 1)->get();

public function searchOscByQuery(Request $request)
{
$q = $request->input('q');
$oscs = OSC::search($q)->get();
$filteredOscs = [];

foreach ($oscs as $osc) {
foreach ($oscs as $osc) {
// Ajoutez une condition pour vérifier si l'OSC est active
if ($osc->active == 1) {
$osc->user;

foreach ($osc->categorieOdds as $categorieOdd) {
$categorieOdd->odd;
}

$osc->zoneInterventions;
}

return $this->sendResponse($oscs, 'OSC retrieved successfully.');
$filteredOscs[] = $osc;
}
}

return $this->sendResponse($filteredOscs, 'Active OSC retrieved successfully.');
}


/**
* Count OSCs.
Expand All @@ -364,7 +381,7 @@ public function searchOscByQuery(Request $request)
*/
public function countOscInDb()
{
$oscs = Osc::all();
$oscs = Osc::where('active', 1)->get();
$count = count($oscs);
return $this->sendResponse($count, 'number of OSCs in db');
}
Expand Down

0 comments on commit 9fbf8bf

Please sign in to comment.