added primary keys to the new db tables

This commit is contained in:
2019-05-30 00:43:06 -05:00
parent df21c42353
commit a786730ca6
6 changed files with 129 additions and 29 deletions

View File

@@ -8,6 +8,7 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Log; use Log;
use DB;
//App Library //App Library
use App\Library\Structures\StructureHelper; use App\Library\Structures\StructureHelper;

View File

@@ -8,41 +8,132 @@
namespace App\Library\Structures\Helper; namespace App\Library\Structures\Helper;
//Internal Library //Internal Library
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Log; use Log;
use DB;
use Carbon\Carbon;
//Job //App Models
use App\Jobs\ProcessStocksJob; use App\Models\Structure\Structure;
use App\Models\Structure\Service;
//Library
use App\Library\Esi\Esi;
use Seat\Eseye\Cache\NullCache;
use Seat\Eseye\Configuration;
use Seat\Eseye\Containers\EsiAuthentication;
use Seat\Eseye\Eseye;
//Models
use App\Models\Stock\StructureStock;
class StructureStockHelper { class StructureStockHelper {
private $scopeCheck; public function StoreNewStructure($structure, $info, $solarName) {
$struct = new Structure;
private $structureInfo; $struct->structure_id = $structure->structure_id;
$struct->structure_name = $info->name;
public function __construct($structure) { $struct->corporation_id = $info->owner_id;
$esi = new Esi(); $struct->solar_system_id = $info->solar_system_id;
$structureScope = $esi->HaveEsiScope($charId, 'esi-universe.read_structurs.v1'); $struct->solary_system_name = $solarName;
if(isset($info->type_id)) {
if($structureScope == false) { $struct->type_id = $info->type_id;
$this->scopeCheck = false; }
$struct->corporation_id = $structure->corporation_id;
if(isset($structure->services)) {
$struct->services = true;
} else { } else {
$this->scopeCheck = true; $struct->services = false;
}
if(isset($structure->state_timer_start)) {
$struct->state_timer_start = $this->DecodeDate($structure->state_timer_start);
}
if(isset($structure->state_timer_end)) {
$struct->state_timer_end = $this->DecodeDate($structure->state_timer_end);
}
if(isset($structure->fuel_expires)) {
$struct->fuel_expires = $structure->fuel_expires;
}
$struct->profile_id = $structure->profile_id;
$struct->position_x = $info->position->x;
$struct->position_y = $info->position->y;
$struct->position_z = $info->position->z;
if(isset($structure->next_reinforce_apply)) {
$struct->next_reinforce_apply = $structure->next_reinforce_apply;
}
if(isset($structure->next_reinforce_hour)) {
$struct->next_reinforce_hour = $structure->next_reinforce_hour;
}
if(isset($structure->next_reinforce_weekday)) {
$struct->next_reinforce_weekday = $structure->next_reinforce_weekday;
}
$struct->reinforce_hour = $structure->reinforce_hour;
if(isset($structure->reinforce_weekday)) {
$struct->reinforce_weekday = $structure->reinforce_weekday;
}
if(isset($structure->unanchors_at)) {
$struct->unanchors_at = $structure->unanchors_at;
}
//If we set the structure services to true, let's save the services
if($structure->services == true) {
$this->StorestructureServices($structure->services, $structure->structure_id);
} }
$this->structureInfo = $structure; //Save the database record
$struct->save();
}
public function UpdateExistingStructure($structure, $info, $solarName) {
//For each line see if it is part of the structure array, and attempt to modify each variable
//This will be implemented in the near future.
if(isset($structure->services)) {
foreach($structure->services as $service) {
//Search for the service, and if found, update it, else add it.
$serviceFound = Service::where([
'structure_id' => $structure->structure_id,
'name' => $service->name,
])->get();
if($serviceFound) {
Service::where([
'structure_id' => $structure->structure_id,
'name' => $service->name,
])->update([
'state' => $service->state,
]);
} else {
$newService = new Service;
$newService->structure_id = $structure->structure_id;
$newService->name = $service->name;
$newService->state = $service->state;
}
}
}
}
public function StoreStructureServices($services, $structureId) {
foreach($services as $service) {
//Find the structure id and the name of the service to see if it exists
$found = Service::where([
'structure_id' => $structureId,
'name' => $service->name,
])->get();
if(!$found) {
$new = new Service;
$new->structure_id = $structureId;
$new->name = $service->name;
$new->state = $service->state;
$new->save();
} else {
Service::where([
'structure_id' => $structureId,
'name' => $service->name,
])->update([
'state' => $service->state,
]);
}
}
}
private function DecodeDate($date) {
$esiHelper = new Esi();
$dateTime = $esiHelper->DecodeDate($date);
return $dateTime;
} }
} }

View File

@@ -17,6 +17,9 @@ class Asset extends Model
//Timestamps //Timestamps
public $timestamps = true; public $timestamps = true;
//Primary Key
public $primaryKey = 'id';
/** /**
* The attributes that are mass assignable * The attributes that are mass assignable
* *

View File

@@ -12,6 +12,9 @@ class Service extends Model
//Timestamps //Timestamps
public $timestamps = false; public $timestamps = false;
//Primary Key
public $primaryKey = 'id';
/** /**
* The attributes that are mass assignable * The attributes that are mass assignable
* *

View File

@@ -14,6 +14,9 @@ class Structure extends Model
//Table Name //Table Name
public $table = 'alliance_structures'; public $table = 'alliance_structures';
//Primary Key
public $primaryKey = 'structure_id';
//Timestamps //Timestamps
public $timestamps = false; public $timestamps = false;

View File

@@ -15,8 +15,7 @@ class CorporationAssetsTable extends Migration
{ {
if(!Schema::hasTable('alliance_structures')) { if(!Schema::hasTable('alliance_structures')) {
Schema::create('alliance_structures', function(Blueprint $table) { Schema::create('alliance_structures', function(Blueprint $table) {
$table->increments('id'); $table->string('structure_id')->primary()->unique();
$table->string('structure_id');
$table->string('structure_name'); $table->string('structure_name');
$table->string('solar_system_id'); $table->string('solar_system_id');
$table->string('solar_system_name')->nullable(); $table->string('solar_system_name')->nullable();
@@ -42,7 +41,7 @@ class CorporationAssetsTable extends Migration
if(!Schema::hasTable('structure_services')) { if(!Schema::hasTable('structure_services')) {
Schema::create('structure_services', function(Blueprint $table) { Schema::create('structure_services', function(Blueprint $table) {
$table->increments('id'); $table->increments('id')->primary();
$table->string('structure_id'); $table->string('structure_id');
$table->string('name'); $table->string('name');
$table->string('state'); $table->string('state');
@@ -51,7 +50,7 @@ class CorporationAssetsTable extends Migration
if(!Schema::hasTable('alliance_assets')) { if(!Schema::hasTable('alliance_assets')) {
Schema::create('alliance_assets', function(Blueprint $table) { Schema::create('alliance_assets', function(Blueprint $table) {
$table->increments('id'); $table->increments('id')->primary();
$table->boolean('is_blueprint_copy')->nullable(); $table->boolean('is_blueprint_copy')->nullable();
$table->boolean('is_singleton'); $table->boolean('is_singleton');
$table->string('item_id'); $table->string('item_id');