Implement NewsletterTempates::get() API using Doctrine
[MAILPOET-2647]
This commit is contained in:
@@ -5,6 +5,23 @@ namespace MailPoet\API\JSON\ResponseBuilders;
|
|||||||
use MailPoet\Entities\NewsletterTemplateEntity;
|
use MailPoet\Entities\NewsletterTemplateEntity;
|
||||||
|
|
||||||
class NewsletterTemplatesResponseBuilder {
|
class NewsletterTemplatesResponseBuilder {
|
||||||
|
const DATE_FORMAT = 'Y-m-d H:i:s';
|
||||||
|
|
||||||
|
public function build(NewsletterTemplateEntity $template): array {
|
||||||
|
return [
|
||||||
|
'id' => $template->getId(),
|
||||||
|
'categories' => $template->getCategories(),
|
||||||
|
'thumbnail' => $template->getThumbnail(),
|
||||||
|
'name' => $template->getName(),
|
||||||
|
'description' => $template->getDescription(),
|
||||||
|
'readonly' => $template->getReadonly(),
|
||||||
|
'body' => $template->getBody(),
|
||||||
|
'created_at' => $template->getCreatedAt()->format(self::DATE_FORMAT),
|
||||||
|
'updated_at' => $template->getUpdatedAt()->format(self::DATE_FORMAT),
|
||||||
|
'newsletter_id' => ($newsletter = $template->getNewsletter()) ? $newsletter->getId() : null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param NewsletterTemplateEntity[] $newsletterTemplates
|
* @param NewsletterTemplateEntity[] $newsletterTemplates
|
||||||
* @return mixed[]
|
* @return mixed[]
|
||||||
|
@@ -34,17 +34,18 @@ class NewsletterTemplates extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function get($data = []) {
|
public function get($data = []) {
|
||||||
$id = (isset($data['id']) ? (int)$data['id'] : false);
|
$template = isset($data['id'])
|
||||||
$template = NewsletterTemplate::findOne($id);
|
? $this->newsletterTemplatesRepository->findOneById((int)$data['id'])
|
||||||
if ($template instanceof NewsletterTemplate) {
|
: null;
|
||||||
return $this->successResponse(
|
|
||||||
$template->asArray()
|
if (!$template) {
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return $this->errorResponse([
|
return $this->errorResponse([
|
||||||
APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet'),
|
APIError::NOT_FOUND => WPFunctions::get()->__('This template does not exist.', 'mailpoet'),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$data = $this->newsletterTemplatesResponseBuilder->build($template);
|
||||||
|
return $this->successResponse($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAll() {
|
public function getAll() {
|
||||||
|
Reference in New Issue
Block a user