update profile backend

This commit is contained in:
JiaSheng
2023-08-06 10:50:32 +08:00
parent 4db0fb9f29
commit f4e045df5c
10 changed files with 270 additions and 21 deletions
@@ -0,0 +1,44 @@
<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Http\Resources\BusinessTypeResource;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class ListBusinessTypesLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Retrieved Business Types',
'message' => 'You have successfully retrieved a list of Business Types'
];
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function logic(Request $request) : JsonResponse
{
$businessTypes = [];
foreach(BusinessType::BUSINESS_TYPE_LIST as $key => $val) {
$type = (object)["id" => $key, "type" => $val];
array_push($businessTypes, $type);
}
return $this->collectionResponse(BusinessTypeResource::collection($businessTypes));
}
}
@@ -0,0 +1,65 @@
<?php
namespace App\Classes\Modules\Companies\ControllersLogic;
use App\Http\Resources\CompanyResource;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyProfileObject;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\UpdatesCompanyProfile;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class UpdateCompanyProfileLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Update Company Account Profile',
'message' => 'You have successfully updated the Company Account Profile'
];
}
/** @var FetchesCompany */
private $fetchesCompany;
/** @var UpdatesCompanyProfile */
private $updatesCompanyProfile;
/**
* UpdateCompanyProfileLogic constructor.
* @param FetchesCompany $fetchesCompany
* @param UpdatesCompanyProfile $updatesCompanyProfile
*/
public function __construct(
FetchesCompany $fetchesCompany,
UpdatesCompanyProfile $updatesCompanyProfile
)
{
$this->fetchesCompany = $fetchesCompany;
$this->updatesCompanyProfile = $updatesCompanyProfile;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\AccessForbiddenException
* @throws \App\Classes\Exceptions\MalformedRequestException
* @throws \App\Classes\Exceptions\RequestValidationException
*/
public function logic(Request $request) : JsonResponse
{
$object = new CompanyProfileObject($request->input('email'), $request->input('phone'), $request->input('business_type'));
$company = $this->fetchesCompany->execute(['id' => $request->route('id')]);
$company_query = $this->updatesCompanyProfile->execute($company, $object);
return $this->resourceResponse(new CompanyResource($company_query));
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Classes\Modules\Companies\DataTransferObjects;
use App\Classes\General\Interfaces\DataTransferObject;
use App\Classes\ValueObjects\Constants\BusinessType;
class CompanyProfileObject implements DataTransferObject
{
/** @var string */
private $email;
/** @var string */
private $phone;
/** @var int|null */
private $businessType;
/**
* CompanyObject constructor.
* @param string $email
* @param string $phone
* @param int|null $businessType
*/
public function __construct(string $email, string $phone, ?int $businessType = BusinessType::IMPORTER)
{
$this->email = $email;
$this->phone = $phone;
$this->businessType = $businessType;
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @return string
*/
public function getPhone(): string
{
return $this->phone;
}
/**
* @return int
*/
public function getBusinessType(): int
{
return $this->businessType;
}
}
@@ -0,0 +1,32 @@
<?php
namespace App\Classes\Modules\Companies\Services;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyProfileObject;
use App\Models\Company;
class UpdatesCompanyProfile extends AbstractUpdateRecord
{
/**
* @param Company $model
* @param CompanyProfileObject $object
* @return \Illuminate\Database\Eloquent\Model
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function execute(Company $model, CompanyProfileObject $object)
{
$model->business_type = $object->getBusinessType();
if ($model->contacts) {
$contact = $model->contacts()->first();
$contact->email = $object->getEmail();
$contact->phone = $object->getPhone();
$contact->save();
}
return $this->handler($model);
}
}