Skip to content

Commit

Permalink
Merge pull request #10382 from Vitaliy-1/i10328_eloquent_announcements
Browse files Browse the repository at this point in the history
#10328 Refactor Announcements
  • Loading branch information
Vitaliy-1 authored Oct 1, 2024
2 parents d2ff88f + 4e0933e commit ffbc59a
Show file tree
Hide file tree
Showing 28 changed files with 1,053 additions and 858 deletions.
69 changes: 31 additions & 38 deletions api/v1/announcements/PKPAnnouncementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Route;
use PKP\announcement\Collector;
use PKP\announcement\Announcement;
use PKP\context\Context;
use PKP\core\exceptions\StoreTemporaryFileException;
use PKP\core\PKPApplication;
use PKP\core\PKPBaseController;
use PKP\core\PKPRequest;
use PKP\db\DAORegistry;
Expand Down Expand Up @@ -124,7 +125,7 @@ public function authorize(PKPRequest $request, array &$args, array $roleAssignme
*/
public function get(Request $illuminateRequest): JsonResponse
{
$announcement = Repo::announcement()->get((int) $illuminateRequest->route('announcementId'));
$announcement = Announcement::find((int) $illuminateRequest->route('announcementId'));

if (!$announcement) {
return response()->json([
Expand All @@ -133,7 +134,7 @@ public function get(Request $illuminateRequest): JsonResponse
}

// The assocId in announcements should always point to the contextId
if ($announcement->getData('assocId') !== $this->getRequest()->getContext()?->getId()) {
if ($announcement->assocId !== $this->getRequest()->getContext()?->getId()) {
return response()->json([
'error' => __('api.announcements.400.contextsNotMatched')
], Response::HTTP_BAD_REQUEST);
Expand All @@ -149,43 +150,38 @@ public function get(Request $illuminateRequest): JsonResponse
*/
public function getMany(Request $illuminateRequest): JsonResponse
{
$collector = Repo::announcement()->getCollector()
->limit(self::DEFAULT_COUNT)
->offset(0);
$announcements = Announcement::limit(self::DEFAULT_COUNT)->offset(0);

foreach ($illuminateRequest->query() as $param => $val) {
switch ($param) {
case 'typeIds':
$collector->filterByTypeIds(
$announcements->withTypeIds(
array_map('intval', paramToArray($val))
);
break;
case 'count':
$collector->limit(min((int) $val, self::MAX_COUNT));
$announcements->limit(min((int) $val, self::MAX_COUNT));
break;
case 'offset':
$collector->offset((int) $val);
$announcements->offset((int) $val);
break;
case 'searchPhrase':
$collector->searchPhrase($val);
$announcements->withSearchPhrase($val);
break;
}
}

if ($this->getRequest()->getContext()) {
$collector->filterByContextIds([$this->getRequest()->getContext()->getId()]);
$announcements->withContextIds([$this->getRequest()->getContext()->getId()]);
} else {
$collector->withSiteAnnouncements(Collector::SITE_ONLY);
$announcements->withContextIds([PKPApplication::SITE_CONTEXT_ID]);
}


Hook::run('API::announcements::params', [$collector, $illuminateRequest]);

$announcements = $collector->getMany();
Hook::run('API::announcements::params', [$announcements, $illuminateRequest]);

return response()->json([
'itemsMax' => $collector->getCount(),
'items' => Repo::announcement()->getSchemaMap()->summarizeMany($announcements)->values(),
'itemsMax' => $announcements->count(),
'items' => Repo::announcement()->getSchemaMap()->summarizeMany($announcements->get())->values(),
], Response::HTTP_OK);
}

Expand All @@ -209,27 +205,22 @@ public function add(Request $illuminateRequest): JsonResponse
return response()->json($errors, Response::HTTP_BAD_REQUEST);
}

$announcement = Repo::announcement()->newDataObject($params);

try {
$announcementId = Repo::announcement()->add($announcement);
$announcement = Announcement::create($params);
} catch (StoreTemporaryFileException $e) {
$announcementId = $e->dataObject->getId();
$announcementId = $e->getDataObjectId();
if ($announcementId) {
$announcement = Repo::announcement()->get($announcementId);
Repo::announcement()->delete($announcement);
Announcement::destroy([$announcementId]);
}
return response()->json([
'image' => [__('api.400.errorUploadingImage')]
], Response::HTTP_BAD_REQUEST);
}

$announcement = Repo::announcement()->get($announcementId);

$sendEmail = (bool) filter_var($params['sendEmail'], FILTER_VALIDATE_BOOLEAN);

if ($context) {
$this->notifyUsers($request, $context, $announcementId, $sendEmail);
$this->notifyUsers($request, $context, $announcement->id, $sendEmail);
}

return response()->json(Repo::announcement()->getSchemaMap()->map($announcement), Response::HTTP_OK);
Expand All @@ -243,27 +234,28 @@ public function edit(Request $illuminateRequest): JsonResponse
$request = $this->getRequest();
$context = $request->getContext();

$announcement = Repo::announcement()->get((int) $illuminateRequest->route('announcementId'));
/** @var Announcement $announcement */
$announcement = Announcement::find((int) $illuminateRequest->route('announcementId'));

if (!$announcement) {
return response()->json([
'error' => __('api.announcements.404.announcementNotFound')
], Response::HTTP_NOT_FOUND);
}

if ($announcement->getData('assocType') !== Application::get()->getContextAssocType()) {
if ($announcement->assocType !== Application::get()->getContextAssocType()) {
throw new Exception('Announcement has an assocType that did not match the context.');
}

// Don't allow to edit an announcement from one context from a different context's endpoint
if ($request->getContext()?->getId() !== $announcement->getData('assocId')) {
if ($request->getContext()?->getId() !== $announcement->assocId) {
return response()->json([
'error' => __('api.announcements.400.contextsNotMatched')
], Response::HTTP_FORBIDDEN);
}

$params = $this->convertStringsToSchema(PKPSchemaService::SCHEMA_ANNOUNCEMENT, $illuminateRequest->input());
$params['id'] = $announcement->getId();
$params['id'] = $announcement->id;
$params['typeId'] ??= null;

$primaryLocale = $context ? $context->getPrimaryLocale() : $request->getSite()->getPrimaryLocale();
Expand All @@ -275,15 +267,15 @@ public function edit(Request $illuminateRequest): JsonResponse
}

try {
Repo::announcement()->edit($announcement, $params);
$announcement->update($params);
} catch (StoreTemporaryFileException $e) {
Repo::announcement()->delete($announcement);
$announcement->delete(); // TODO do we really need to delete an announcement if the image upload fails?
return response()->json([
'image' => [__('api.400.errorUploadingImage')]
], Response::HTTP_BAD_REQUEST);
}

$announcement = Repo::announcement()->get($announcement->getId());
$announcement = Announcement::find($announcement->id);

return response()->json(Repo::announcement()->getSchemaMap()->map($announcement), Response::HTTP_OK);
}
Expand All @@ -295,28 +287,29 @@ public function delete(Request $illuminateRequest): JsonResponse
{
$request = $this->getRequest();

$announcement = Repo::announcement()->get((int) $illuminateRequest->route('announcementId'));
/** @var Announcement $announcement */
$announcement = Announcement::find((int) $illuminateRequest->route('announcementId'));

if (!$announcement) {
return response()->json([
'error' => __('api.announcements.404.announcementNotFound')
], Response::HTTP_NOT_FOUND);
}

if ($announcement->getData('assocType') !== Application::get()->getContextAssocType()) {
if ($announcement->assocType !== Application::get()->getContextAssocType()) {
throw new Exception('Announcement has an assocType that did not match the context.');
}

// Don't allow to delete an announcement from one context from a different context's endpoint
if ($request->getContext()?->getId() !== $announcement->getData('assocId')) {
if ($request->getContext()?->getId() !== $announcement->assocId) {
return response()->json([
'error' => __('api.announcements.400.contextsNotMatched')
], Response::HTTP_FORBIDDEN);
}

$announcementProps = Repo::announcement()->getSchemaMap()->map($announcement);

Repo::announcement()->delete($announcement);
$announcement->delete();

return response()->json($announcementProps, Response::HTTP_OK);
}
Expand Down
Loading

0 comments on commit ffbc59a

Please sign in to comment.