diff --git a/app/Http/Controllers/ContractController.php b/app/Http/Controllers/ContractController.php index d83daac81..236c253d6 100644 --- a/app/Http/Controllers/ContractController.php +++ b/app/Http/Controllers/ContractController.php @@ -7,6 +7,7 @@ use DB; use Carbon\Carbon; //Libraries +use App\Library\Lookups\LookupHelper; //use App\Library\Contracts\ContractHelper; //Models @@ -130,6 +131,9 @@ class ContractController extends Controller 'bid' => 'required', ]); + //Delcare some class variables we will need + $lookup = new LookupHelper; + $amount = 0.00; //Convert the amount to a whole number from abbreviations @@ -141,11 +145,22 @@ class ContractController extends Controller $amount = $request->bid; } + //Get the character id and character name from the auth of the user calling + //this function + $characterId = auth()->user()->getId(); + $characterName = auth()->user()->getName(); + //Use the lookup helper in order to find the user's corporation id and name + $corporationId = $lookup->LookupCharacter($characterId); + $corporationName = $lookup->LookupCorporationName($corporationId); + //Create the model object to save data to $bid = new Bid; $bid->contract_id = $request->contract_id; $bid->bid_amount = $amount; - $bid->accepted = false; + $bid->character_id = $characterId; + $bid->character_name = $characterName; + $bid->corporation_id = $corporationId; + $bid->corporation_name = $corporationName; $bid->save(); //Redirect to the correct page diff --git a/app/Library/Lookups/LookupHelper.php b/app/Library/Lookups/LookupHelper.php index 0ac547a4d..dcf82c253 100644 --- a/app/Library/Lookups/LookupHelper.php +++ b/app/Library/Lookups/LookupHelper.php @@ -83,6 +83,47 @@ class LookupHelper { } } + /** + * Function to retrieve a corporation name from the lookup tables + * or add the details of the corporation if it's not found + * + */ + public function LookupCorporationName($corpId) { + //check for the character in the user_to_corporation table + $found = CorporationToAlliance::where('corporation_id', $corpId)->get(['corporation_name']); + + //If we don't find the corporation in the table, then we need to retrieve it from ESI + //and add the corporation to the table + if(!isset($found[0]->corporation_name)) { + //Get the configuration for ESI from the environmental variables + $config = config('esi'); + + //Setup a new ESI container + $esi = new Eseye(); + + try { + $corporation = $esi->invoke('get', '/corporations/{corporation_id}/', [ + 'corporation_id' => $corpId, + ]); + } catch(\Seat\Eseye\Exceptions\RequestFailedException $e){ + return $e->getEsiResponse(); + } + + //Save all of the data to the database + $corp = new CorporationToAlliance; + $corp->corporation_id = $corporation->corporation_id; + $corp->corporation_name = $corporation->name; + $corp->alliance_id = $corporation->alliance_id; + $corp->alliance_name = $alliance->name; + $corp->save(); + + //Return the corporation name + return $corporation->name; + } else { + return $found[0]->corporation_name; + } + } + //Add corporations to the lookup table for quicker lookups without having to //hit the ESI API all the time public function LookupCorporation($corpId) {