Last active
January 3, 2022 06:32
-
-
Save VusalGhasanov/175a212acc7ea178e33260a01c85672d to your computer and use it in GitHub Desktop.
Customer store
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
<?php | |
namespace App\Http\Controller; | |
use App\Service\CustomerService; | |
use App\Requests\CustomerRequest; | |
class CustomerController extends Controller | |
{ | |
/** | |
* service | |
* | |
* @var CustomerService | |
*/ | |
protected $service; | |
/** | |
* __construct | |
* | |
* @param CustomerService $service | |
* @return void | |
*/ | |
public function __construct(CustomerService $service) | |
{ | |
$this->service = $service; | |
} | |
/** | |
* store data | |
* | |
* @param CustomerRequest $request | |
* @return array | |
*/ | |
public function store(CustomerRequest $request): array | |
{ | |
$payload = $request->validated(); | |
return $this->service->store($payload); | |
} | |
} |
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
<?php | |
namespace App\Repositories; | |
use DB; | |
class BranchGoodRepository | |
{ | |
/** | |
* __construct | |
* | |
* @param string $tablename | |
* @return void | |
*/ | |
public function __construct(string $tablename) | |
{ | |
$this->tablename = $tablename; | |
} | |
/** | |
* store data | |
* | |
* @param array $payload | |
* @return bool | |
*/ | |
public function store(array $payload): bool | |
{ | |
return DB::table($this->tablename)->insert($payload); | |
} | |
} |
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
<?php | |
namespace App\Repositories; | |
interface BranchGoodRepositoryInterface | |
{ | |
/** | |
* store data | |
* | |
* @param array $payload | |
* @return bool | |
*/ | |
public function store(array $payload): bool; | |
} |
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
<?php | |
namespace App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
class CustomerRequest extends FormRequest | |
{ | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return true; | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
'name' => ['required'], | |
'email' => ['required'] | |
]; | |
} | |
} |
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
<?php | |
namespace App\Services; | |
use App\Repositories\CustomerRepositoryInterface; | |
use App\Jobs\SendMailToCustomerJob; | |
class CustomerService | |
{ | |
/** | |
* repository | |
* | |
* @var CustomerRepositoryInterface | |
*/ | |
protected $repository; | |
/** | |
* __construct | |
* | |
* @param CustomerRepositoryInterface $repository | |
* @return void | |
*/ | |
public function __construct(CustomerRepositoryInterface $repository) | |
{ | |
$this->repository = $repository; | |
} | |
/** | |
* store data | |
* | |
* @param array $payload | |
* @return array | |
*/ | |
public function store(array $payload): array | |
{ | |
$saved = $this->repository->store($payload); | |
// job for send email with queue. | |
SendMailToCustomerJob::dispatch($payload['email'], $payload); | |
return [ | |
'saved' => $saved | |
] | |
} | |
} |
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
1. Binded repository and interface in RepositoryServiceProvide =) |
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
<?php | |
namespace App\Jobs; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Contracts\Queue\ShouldBeUnique; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Foundation\Bus\Dispatchable; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Queue\SerializesModels; | |
use App\Mail\WelcomeNewCustomer; | |
use Mail; | |
class SendMailToCustomerJob implements ShouldQueue | |
{ | |
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
/** | |
* @var string | |
*/ | |
private string $email; | |
/** | |
* @var array | |
*/ | |
private array $data; | |
/** | |
* Create a new job instance. | |
* | |
* @param string $email | |
* @return void | |
*/ | |
public function __construct(string $email, array $data) | |
{ | |
$this->email = $email; | |
$this->data = $data; | |
} | |
/** | |
* Execute the job. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
Mail::to($this->email)->send(new WelcomeNewCustomer($this->data)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment