-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor contact management with services (#2168)
- Loading branch information
Showing
23 changed files
with
1,590 additions
and
329 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
class RandomHelper | ||
{ | ||
/** | ||
* Generate a UUID. | ||
* | ||
* @return string | ||
*/ | ||
public static function uuid() | ||
{ | ||
return Str::uuid()->toString(); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace App\Services\Contact\Contact; | ||
|
||
use App\Helpers\RandomHelper; | ||
use App\Services\BaseService; | ||
use App\Models\Contact\Contact; | ||
|
||
class CreateContact extends BaseService | ||
{ | ||
private $contact; | ||
|
||
/** | ||
* Get the validation rules that apply to the service. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'account_id' => 'required|integer|exists:accounts,id', | ||
'first_name' => 'required|string|max:255', | ||
'middle_name' => 'nullable|string|max:255', | ||
'last_name' => 'nullable|string|max:255', | ||
'nickname' => 'nullable|string|max:255', | ||
'gender_id' => 'required|integer|exists:genders,id', | ||
'description' => 'nullable|string|max:255', | ||
'is_partial' => 'nullable|boolean', | ||
]; | ||
} | ||
|
||
/** | ||
* Create a contact. | ||
* | ||
* @param array $data | ||
* @return Contact | ||
*/ | ||
public function execute(array $data) : Contact | ||
{ | ||
$this->validate($data); | ||
|
||
$this->contact = Contact::create($data); | ||
|
||
$this->generateUUID(); | ||
|
||
$this->contact->setAvatarColor(); | ||
|
||
$this->contact->save(); | ||
|
||
return $this->contact; | ||
} | ||
|
||
/** | ||
* Generates a UUID for this contact. | ||
* | ||
* @return void | ||
*/ | ||
private function generateUUID() | ||
{ | ||
$this->contact->uuid = RandomHelper::uuid(); | ||
$this->contact->save(); | ||
} | ||
} |
Oops, something went wrong.