-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #780 from owlchester/bulk-relation-creation
Bulk Relation Creation
- Loading branch information
Showing
10 changed files
with
143 additions
and
49 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace App\Services\Entity; | ||
|
||
use App\Http\Requests\StoreRelation; | ||
use App\Models\Relation; | ||
use App\Traits\CampaignAware; | ||
|
||
class RelationService | ||
{ | ||
use CampaignAware; | ||
|
||
protected array $entities; | ||
protected Relation $new; | ||
protected int $count; | ||
|
||
/** | ||
*/ | ||
public function createRelations(StoreRelation $request): self | ||
{ | ||
$this->count = 0; | ||
$data = $request->only([ | ||
'owner_id', 'attitude', 'relation', 'colour', 'is_pinned', 'two_way', 'visibility_id' | ||
]); | ||
$data['campaign_id'] = $this->campaign->id; | ||
|
||
if ($request->has('targets')) { | ||
$this->entities = $request->get('targets'); | ||
} else { | ||
$this->entities = [$request->get('target_id')]; | ||
} | ||
foreach ($this->entities as $entity_id) { | ||
$data['target_id'] = $entity_id; | ||
$relation = new Relation(); | ||
$relation = $relation->create($data); | ||
$this->count++; | ||
if (!isset($new)) { | ||
$new = $relation; | ||
} | ||
if ($request->has('two_way')) { | ||
$relation->createMirror(); | ||
$this->count++; | ||
} | ||
} | ||
$this->new = $new; | ||
|
||
return $this; | ||
} | ||
|
||
public function getEntities(): array | ||
{ | ||
return $this->entities; | ||
} | ||
|
||
public function getNew(): Relation | ||
{ | ||
return $this->new; | ||
} | ||
|
||
public function getCount(): int | ||
{ | ||
return $this->count; | ||
} | ||
} |
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
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
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
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
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