Refactor custom fields api to doctrine
[MAILPOET-2453]
This commit is contained in:
committed by
Rostislav Wolný
parent
49f92ef77c
commit
ff9bd232ec
@ -4,7 +4,11 @@ namespace MailPoet\API\JSON\v1;
|
||||
|
||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||
use MailPoet\API\JSON\Error as APIError;
|
||||
use MailPoet\API\JSON\Response;
|
||||
use MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder;
|
||||
use MailPoet\Config\AccessControl;
|
||||
use MailPoet\CustomFields\CustomFieldsRepository;
|
||||
use MailPoet\Entities\CustomFieldEntity;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
@ -13,22 +17,33 @@ class CustomFields extends APIEndpoint {
|
||||
'global' => AccessControl::PERMISSION_MANAGE_FORMS,
|
||||
];
|
||||
|
||||
function getAll() {
|
||||
$collection = CustomField::orderByAsc('created_at')->findMany();
|
||||
$custom_fields = array_map(function($custom_field) {
|
||||
return $custom_field->asArray();
|
||||
}, $collection);
|
||||
/** @var CustomFieldsRepository */
|
||||
private $custom_fields_repository;
|
||||
|
||||
return $this->successResponse($custom_fields);
|
||||
/** @var CustomFieldsResponseBuilder */
|
||||
private $custom_fields_response_builder;
|
||||
|
||||
public function __construct(
|
||||
CustomFieldsRepository $custom_fields_repository,
|
||||
CustomFieldsResponseBuilder $custom_fields_response_builder
|
||||
) {
|
||||
$this->custom_fields_repository = $custom_fields_repository;
|
||||
$this->custom_fields_response_builder = $custom_fields_response_builder;
|
||||
}
|
||||
|
||||
function getAll() {
|
||||
$collection = $this->custom_fields_repository->findBy([], ['created_at' => 'asc']);
|
||||
return $this->successResponse($this->custom_fields_response_builder->buildBatch($collection));
|
||||
}
|
||||
|
||||
function delete($data = []) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : null);
|
||||
$custom_field = CustomField::findOne($id);
|
||||
if ($custom_field instanceof CustomField) {
|
||||
$custom_field->delete();
|
||||
$custom_field = $this->custom_fields_repository->findOneById($id);
|
||||
if ($custom_field instanceof CustomFieldEntity) {
|
||||
$this->custom_fields_repository->remove($custom_field);
|
||||
$this->custom_fields_repository->flush();
|
||||
|
||||
return $this->successResponse($custom_field->asArray());
|
||||
return $this->successResponse($this->custom_fields_response_builder->build($custom_field));
|
||||
} else {
|
||||
return $this->errorResponse([
|
||||
APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet'),
|
||||
@ -37,24 +52,21 @@ class CustomFields extends APIEndpoint {
|
||||
}
|
||||
|
||||
function save($data = []) {
|
||||
$custom_field = CustomField::createOrUpdate($data);
|
||||
$errors = $custom_field->getErrors();
|
||||
|
||||
if (!empty($errors)) {
|
||||
return $this->badRequest($errors);
|
||||
try {
|
||||
$custom_field = $this->custom_fields_repository->createOrUpdate($data);
|
||||
$custom_field = $this->custom_fields_repository->findOneById($custom_field->getId());
|
||||
if(!$custom_field instanceof CustomFieldEntity) return $this->errorResponse();
|
||||
return $this->successResponse($this->custom_fields_response_builder->build($custom_field));
|
||||
} catch (\Exception $e) {
|
||||
return $this->errorResponse($errors = [], $meta = [], $status = Response::STATUS_BAD_REQUEST);
|
||||
}
|
||||
$custom_field = CustomField::findOne($custom_field->id);
|
||||
if(!$custom_field instanceof CustomField) return $this->errorResponse();
|
||||
$response = $custom_field->asArray();
|
||||
$response['id'] = (int)$response['id'];
|
||||
return $this->successResponse($response);
|
||||
}
|
||||
|
||||
function get($data = []) {
|
||||
$id = (isset($data['id']) ? (int)$data['id'] : null);
|
||||
$custom_field = CustomField::findOne($id);
|
||||
if ($custom_field instanceof CustomField) {
|
||||
return $this->successResponse($custom_field->asArray());
|
||||
$custom_field = $this->custom_fields_repository->findOneById($id);
|
||||
if ($custom_field instanceof CustomFieldEntity) {
|
||||
return $this->successResponse($this->custom_fields_response_builder->build($custom_field));
|
||||
}
|
||||
return $this->errorResponse([
|
||||
APIError::NOT_FOUND => WPFunctions::get()->__('This custom field does not exist.', 'mailpoet'),
|
||||
|
Reference in New Issue
Block a user