Skip to content

Commit

Permalink
馃Чadds better rest response for trade fair representation (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianzimmermann committed Sep 18, 2023
1 parent 2329380 commit 9f0c36c
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 71 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ codeception:

.PHONY: ci
ci: phpcs codeception phpstan

.PHONY: clean
clean:
rm -Rf composer.lock
rm -Rf ./vendor
find ./tests/_output/ -not -name .gitignore -not -name .gitkeep -delete
rm -Rf src/Generated/*
rm -Rf src/Orm/*
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function buildRepresentativeCompanyUserTradeFairRestResponse(

$restResource = $this->restResourceBuilder->createRestResource(
RepresentativeCompanyUserTradeFairRestApiConfig::RESOURCE_REPRESENTATIVE_COMPANY_USER_TRADE_FAIR_REST_API,
null,
$representativeCompanyUserTradeFairTransfer->getUuid(),
$representativeCompanyUserTradeFairTransfer,
);

Expand All @@ -57,23 +57,34 @@ public function buildRepresentativeCompanyUserTradeFairCollectionRestResponse(
$restResource = $this->restResourceBuilder->createRestResource(
RepresentativeCompanyUserTradeFairRestApiConfig::RESOURCE_REPRESENTATIVE_COMPANY_USER_TRADE_FAIR_REST_API,
null,
$representativeCompanyUserTradeFairTransfer,
$representativeCompanyUserTradeFairTransfer->getCollection(),
);

return $restResponse->addResource($restResource);
}

/**
* @param string $error
* @param int $code
* @param \Generated\Shared\Transfer\RestErrorMessageTransfer|null $restErrorMessageTransfer
* @param string|null $error
* @param string $code
* @param int $status
*
* @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface
*/
public function createRestErrorResponse(string $error, int $code, int $status = 0): RestResponseInterface
{
public function createRestErrorResponse(
?RestErrorMessageTransfer $restErrorMessageTransfer,
?string $error = null,
string $code = '200',
int $status = 0
): RestResponseInterface {
if ($restErrorMessageTransfer !== null) {
return $this->restResourceBuilder
->createRestResponse()
->addError($restErrorMessageTransfer);
}

$restErrorMessageTransfer = (new RestErrorMessageTransfer())
->setCode((string)$code)
->setCode($code)
->setStatus(0)
->setDetail($error);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FondOfOryx\Glue\RepresentativeCompanyUserTradeFairRestApi\Processor\Builder;

use Generated\Shared\Transfer\RestErrorMessageTransfer;
use Generated\Shared\Transfer\RestRepresentativeCompanyUserTradeFairResponseTransfer;
use Generated\Shared\Transfer\RestRepresentativeCompanyUserTradeFairTransfer;
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface;
Expand All @@ -27,11 +28,17 @@ public function buildRepresentativeCompanyUserTradeFairCollectionRestResponse(
): RestResponseInterface;

/**
* @param string $error
* @param int $code
* @param \Generated\Shared\Transfer\RestErrorMessageTransfer|null $restErrorMessageTransfer
* @param string|null $error
* @param string $code
* @param int $status
*
* @return \Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface
*/
public function createRestErrorResponse(string $error, int $code, int $status = 0): RestResponseInterface;
public function createRestErrorResponse(
?RestErrorMessageTransfer $restErrorMessageTransfer,
?string $error = null,
string $code = '200',
int $status = 0
): RestResponseInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use FondOfOryx\Client\RepresentativeCompanyUserTradeFairRestApi\RepresentativeCompanyUserTradeFairRestApiClientInterface;
use FondOfOryx\Glue\RepresentativeCompanyUserTradeFairRestApi\Processor\Builder\RestResponseBuilderInterface;
use FondOfOryx\Glue\RepresentativeCompanyUserTradeFairRestApi\Processor\Mapper\RepresentationMapperInterface;
use FondOfOryx\Shared\RepresentativeCompanyUserTradeFairRestApi\RepresentativeCompanyUserTradeFairRestApiConstants;
use Spryker\Glue\GlueApplication\Rest\JsonApi\RestResponseInterface;
use Spryker\Glue\GlueApplication\Rest\Request\Data\RestRequestInterface;

Expand Down Expand Up @@ -54,7 +53,7 @@ public function add(RestRequestInterface $restRequest): RestResponseInterface

if ($representationRestResponseTransfer->getError() !== null) {
return $this->responseBuilder
->createRestErrorResponse($representationRestResponseTransfer->getError(), RepresentativeCompanyUserTradeFairRestApiConstants::HTTP_CODE_VALIDATION_ERRORS);
->createRestErrorResponse($representationRestResponseTransfer->getError());
}

return $this->responseBuilder
Expand All @@ -75,7 +74,7 @@ public function get(RestRequestInterface $restRequest): RestResponseInterface

if ($representationRestResponseTransfer->getError() !== null) {
return $this->responseBuilder
->createRestErrorResponse($representationRestResponseTransfer->getError(), RepresentativeCompanyUserTradeFairRestApiConstants::HTTP_CODE_VALIDATION_ERRORS);
->createRestErrorResponse($representationRestResponseTransfer->getError());
}

return $this->responseBuilder->buildRepresentativeCompanyUserTradeFairCollectionRestResponse($representationRestResponseTransfer);
Expand All @@ -95,7 +94,7 @@ public function patch(RestRequestInterface $restRequest): RestResponseInterface

if ($representationRestResponseTransfer->getError() !== null) {
return $this->responseBuilder
->createRestErrorResponse($representationRestResponseTransfer->getError(), RepresentativeCompanyUserTradeFairRestApiConstants::HTTP_CODE_VALIDATION_ERRORS);
->createRestErrorResponse($representationRestResponseTransfer->getError());
}

return $this->responseBuilder->buildRepresentativeCompanyUserTradeFairRestResponse($representationRestResponseTransfer->getRepresentation());
Expand All @@ -115,7 +114,7 @@ public function delete(RestRequestInterface $restRequest): RestResponseInterface

if ($representationRestResponseTransfer->getError() !== null) {
return $this->responseBuilder
->createRestErrorResponse($representationRestResponseTransfer->getError(), RepresentativeCompanyUserTradeFairRestApiConstants::HTTP_CODE_VALIDATION_ERRORS);
->createRestErrorResponse($representationRestResponseTransfer->getError());
}

return $this->responseBuilder->buildRepresentativeCompanyUserTradeFairRestResponse($representationRestResponseTransfer->getRepresentation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@

<transfer name="RestRepresentativeCompanyUserTradeFairResponse">
<property name="isSuccessful" type="bool"/>
<property name="error" type="string" />
<property name="error" type="RestErrorMessage" />
<property name="representation" type="RestRepresentativeCompanyUserTradeFair"/>
<property name="collection" type="RestRepresentativeCompanyUserTradeFairCollection"/>
<property name="pagination" type="RestRepresentativeCompanyUserPagination"/>
</transfer>

<transfer name="RestRepresentativeCompanyUserPagination">
Expand All @@ -63,6 +62,7 @@

<transfer name="RestRepresentativeCompanyUserTradeFairCollection">
<property name="representations" singular="representation" type="RestRepresentativeCompanyUserTradeFair[]" />
<property name="pagination" type="RestRepresentativeCompanyUserPagination"/>
</transfer>

<transfer name="RepresentativeCompanyUserTradeFair" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace FondOfOryx\Zed\RepresentativeCompanyUserTradeFairRestApi\Business\Model;

use Exception;
use FondOfOryx\Zed\RepresentativeCompanyUserTradeFairRestApi\Business\Model\Mapper\RestDataMapperInterface;
use FondOfOryx\Zed\RepresentativeCompanyUserTradeFairRestApi\Business\Validator\DurationValidatorInterface;
use FondOfOryx\Zed\RepresentativeCompanyUserTradeFairRestApi\Communication\Plugin\PermissionExtension\CanManageRepresentationOnTradeFairPermissionPlugin;
Expand All @@ -14,10 +13,12 @@
use Generated\Shared\Transfer\RepresentativeCompanyUserTradeFairCollectionTransfer;
use Generated\Shared\Transfer\RepresentativeCompanyUserTradeFairFilterTransfer;
use Generated\Shared\Transfer\RepresentativeCompanyUserTradeFairTransfer;
use Generated\Shared\Transfer\RestErrorMessageTransfer;
use Generated\Shared\Transfer\RestRepresentativeCompanyUserPaginationTransfer;
use Generated\Shared\Transfer\RestRepresentativeCompanyUserTradeFairAttributesTransfer;
use Generated\Shared\Transfer\RestRepresentativeCompanyUserTradeFairRequestTransfer;
use Generated\Shared\Transfer\RestRepresentativeCompanyUserTradeFairResponseTransfer;
use Throwable;

class TradeFairRepresentationManager implements TradeFairRepresentationManagerInterface
{
Expand Down Expand Up @@ -80,22 +81,26 @@ public function addTradeFairRepresentation(

$error = $this->validate($restRepresentativeCompanyUserTradeFairRequestTransfer);

if ($error) {
if ($error !== null) {
return $restRepresentativeCompanyUserTradeFairResponse->setError($error);
}

$restRepresentativeCompanyUserTradeFairAttributesTransfer = $restRepresentativeCompanyUserTradeFairRequestTransfer->getAttributes();
$originatorId = $this->repository->getIdCustomerByReference($restRepresentativeCompanyUserTradeFairAttributesTransfer->getCustomerReferenceOriginator());
$representationId = $this->repository->getIdCustomerByReference($restRepresentativeCompanyUserTradeFairAttributesTransfer->getCustomerReferenceRepresentative());

$representationTransfer = (new RepresentativeCompanyUserTradeFairTransfer())
->setFkDistributor($representationId)
->setFkOriginator($originatorId)
->setName($restRepresentativeCompanyUserTradeFairAttributesTransfer->getTradeFairName())
->setStartAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getStartAt())
->setEndAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getEndAt());

$response = $this->representativeCompanyUserTradeFairFacade->addRepresentativeCompanyUserTradeFair($representationTransfer);
try {
$restRepresentativeCompanyUserTradeFairAttributesTransfer = $restRepresentativeCompanyUserTradeFairRequestTransfer->getAttributes();
$originatorId = $this->repository->getIdCustomerByReference($restRepresentativeCompanyUserTradeFairAttributesTransfer->getCustomerReferenceOriginator());
$representationId = $this->repository->getIdCustomerByReference($restRepresentativeCompanyUserTradeFairAttributesTransfer->getCustomerReferenceRepresentative());

$representationTransfer = (new RepresentativeCompanyUserTradeFairTransfer())
->setFkDistributor($representationId)
->setFkOriginator($originatorId)
->setName($restRepresentativeCompanyUserTradeFairAttributesTransfer->getTradeFairName())
->setStartAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getStartAt())
->setEndAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getEndAt());

$response = $this->representativeCompanyUserTradeFairFacade->addRepresentativeCompanyUserTradeFair($representationTransfer);
} catch (Throwable $throwable) {
return $restRepresentativeCompanyUserTradeFairResponse->setError($this->createError($throwable->getMessage(), '500', 500));
}

return $restRepresentativeCompanyUserTradeFairResponse
->setIsSuccessful(true)
Expand All @@ -112,32 +117,37 @@ public function updateTradeFairRepresentation(
): RestRepresentativeCompanyUserTradeFairResponseTransfer {
$restRepresentativeCompanyUserTradeFairAttributesTransfer = $restRepresentativeCompanyUserTradeFairRequestTransfer->getAttributes();
$restRepresentativeCompanyUserTradeFairAttributesTransfer->requireUuid();
$restResponse = (new RestRepresentativeCompanyUserTradeFairResponseTransfer())->setIsSuccessful(false);

$error = $this->validate($restRepresentativeCompanyUserTradeFairRequestTransfer);

if ($error) {
return (new RestRepresentativeCompanyUserTradeFairResponseTransfer())->setError($error);
if ($error !== null) {
return $restResponse->setError($error);
}

$representationTransfer = $this->representativeCompanyUserTradeFairFacade->findTradeFairRepresentationByUuid($restRepresentativeCompanyUserTradeFairAttributesTransfer->getUuid());
try {
$representationTransfer = $this->representativeCompanyUserTradeFairFacade->findTradeFairRepresentationByUuid($restRepresentativeCompanyUserTradeFairAttributesTransfer->getUuid());

if (
$representationTransfer->getDistributor()->getCustomerReference() !== $restRepresentativeCompanyUserTradeFairAttributesTransfer->getCustomerReferenceRepresentative()
) {
$representationTransfer->setActive(false);
$this->representativeCompanyUserTradeFairFacade->updateRepresentativeCompanyUserTradeFair($representationTransfer);
if (
$representationTransfer->getDistributor()->getCustomerReference() !== $restRepresentativeCompanyUserTradeFairAttributesTransfer->getCustomerReferenceRepresentative()
) {
$representationTransfer->setActive(false);
$this->representativeCompanyUserTradeFairFacade->updateRepresentativeCompanyUserTradeFair($representationTransfer);

return $this->addTradeFairRepresentation($restRepresentativeCompanyUserTradeFairRequestTransfer);
}
return $this->addTradeFairRepresentation($restRepresentativeCompanyUserTradeFairRequestTransfer);
}

$representationTransfer
->setActive($this->getStatus($representationTransfer, $restRepresentativeCompanyUserTradeFairAttributesTransfer))
->setEndAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getEndAt())
->setStartAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getStartAt());
$response = $this->representativeCompanyUserTradeFairFacade->updateRepresentativeCompanyUserTradeFair($representationTransfer);
$representationTransfer
->setActive($this->getStatus($representationTransfer, $restRepresentativeCompanyUserTradeFairAttributesTransfer))
->setEndAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getEndAt())
->setStartAt($restRepresentativeCompanyUserTradeFairAttributesTransfer->getStartAt());
$response = $this->representativeCompanyUserTradeFairFacade->updateRepresentativeCompanyUserTradeFair($representationTransfer);
} catch (Throwable $throwable) {
return $restResponse->setError($this->createError($throwable->getMessage(), '500', 500));
}

return (new RestRepresentativeCompanyUserTradeFairResponseTransfer())
->setRepresentation($this->restDataMapper->mapResponse($response));
return $restResponse
->setRepresentation($this->restDataMapper->mapResponse($response))->setIsSuccessful(true);
}

/**
Expand All @@ -149,17 +159,18 @@ public function deleteTradeFairRepresentation(
RestRepresentativeCompanyUserTradeFairRequestTransfer $restRepresentativeCompanyUserTradeFairRequestTransfer
): RestRepresentativeCompanyUserTradeFairResponseTransfer {
$attributes = $restRepresentativeCompanyUserTradeFairRequestTransfer->getAttributes();
$restResponse = (new RestRepresentativeCompanyUserTradeFairResponseTransfer())->setIsSuccessful(false);

try {
$attributes->requireUuid();
$representation = $this->representativeCompanyUserTradeFairFacade->deleteRepresentativeCompanyUserTradeFair($attributes->getUuid());
$response = $this->restDataMapper->mapResponse($representation);
} catch (Exception $exception) {
} catch (Throwable $throwable) {
//ToDo Handle/Log
$response = null;
return $restResponse->setError($this->createError($throwable->getMessage(), '500', 500));
}

return (new RestRepresentativeCompanyUserTradeFairResponseTransfer())->setRepresentation($response);
return $restResponse->setRepresentation($response)->setIsSuccessful(true);
}

/**
Expand Down Expand Up @@ -191,20 +202,22 @@ public function getTradeFairRepresentation(
$filter = $this->createFilter($restRepresentativeCompanyUserTradeFairRequestTransfer, $attributes);

$collection = $this->representativeCompanyUserTradeFairFacade->getRepresentativeCompanyUserTradeFair($filter);
$pagination = $this->createPagination($collection);
$collectionTransfer = $this->restDataMapper->mapResponseCollection($collection);
$collectionTransfer->setPagination($pagination);

return (new RestRepresentativeCompanyUserTradeFairResponseTransfer())
->setCollection($this->restDataMapper->mapResponseCollection($collection))
->setPagination($this->createPagination($collection));
->setCollection($collectionTransfer);
}

/**
* @param \Generated\Shared\Transfer\RestRepresentativeCompanyUserTradeFairRequestTransfer $restRepresentativeCompanyUserTradeFairRequestTransfer
*
* @return string
* @return \Generated\Shared\Transfer\RestErrorMessageTransfer|null
*/
protected function validate(
RestRepresentativeCompanyUserTradeFairRequestTransfer $restRepresentativeCompanyUserTradeFairRequestTransfer
): string {
): ?RestErrorMessageTransfer {
$companyTypeManufacturer = $this->companyTypeFacade->getCompanyTypeManufacturer();

if (
Expand All @@ -215,16 +228,16 @@ protected function validate(
$companyTypeManufacturer->getIdCompanyType(),
)
) {
return RepresentativeCompanyUserTradeFairRestApiConfig::ERROR_MESSAGE_USER_IS_NOT_ALLOWED_TO_ADD_TRADE_FAIR_REPRESENTATION;
return $this->createError(RepresentativeCompanyUserTradeFairRestApiConfig::ERROR_MESSAGE_USER_IS_NOT_ALLOWED_TO_ADD_TRADE_FAIR_REPRESENTATION, '422');
}

if (
!$this->durationValidator->validate($restRepresentativeCompanyUserTradeFairRequestTransfer->getAttributes())
) {
return RepresentativeCompanyUserTradeFairRestApiConfig::ERROR_MESSAGE_REPRESENTATION_DURATION_EXCEEDED;
return $this->createError(RepresentativeCompanyUserTradeFairRestApiConfig::ERROR_MESSAGE_REPRESENTATION_DURATION_EXCEEDED, '422');
}

return '';
return null;
}

/**
Expand Down Expand Up @@ -291,4 +304,19 @@ public function createPagination(RepresentativeCompanyUserTradeFairCollectionTra
->setNumFound($total)
->setCurrentItemsPerPage($limit);
}

/**
* @param string $message
* @param string $code
* @param int $status
*
* @return \Generated\Shared\Transfer\RestErrorMessageTransfer
*/
protected function createError(string $message, string $code, int $status = 400): RestErrorMessageTransfer
{
return (new RestErrorMessageTransfer())
->setDetail($message)
->setCode($code)
->setStatus($status);
}
}

0 comments on commit 9f0c36c

Please sign in to comment.