public function NotificationWidgetUpdateResource::post in Notifications widget 8
File
- src/
Plugin/ rest/ resource/ NotificationWidgetUpdateResource.php, line 92
Class
- NotificationWidgetUpdateResource
- Provides a resource to get update the status of notification items.
Namespace
Drupal\notifications_widget\Plugin\rest\resourceCode
public function post($notificationData) {
$result = [];
// Get logged user session.
$currentUser = $this->currentUser;
$uid = $currentUser
->id();
// Use current user after pass authentication to validate access.
if (!$this->currentUser
->hasPermission('access content')) {
throw new AccessDeniedHttpException();
}
$action = $notificationData['notification_action'];
$id = is_numeric($notificationData['nasId']) ? $notificationData['nasId'] : NULL;
$notificationId = $notificationData['notiId'];
switch ($action) {
case 'read':
$keys = [
'id' => $id,
];
$fields = [
'notification_id' => $notificationId,
'uid' => $uid,
'status' => 1,
'created' => \Drupal::time()
->getRequestTime(),
];
$tableName = 'notifications_actions';
break;
case 'delete':
$keys = [
'id' => $id,
];
$fields = [
'notification_id' => $notificationId,
'uid' => $uid,
'status' => 2,
'created' => \Drupal::time()
->getRequestTime(),
];
$tableName = 'notifications_actions';
break;
case 'clearall':
$keys = [
'id' => NULL,
];
$fields = [
'notification_id' => $notificationId,
'uid' => $uid,
'created' => \Drupal::time()
->getRequestTime(),
];
$tableName = 'notifications_clear_all';
break;
}
try {
$this->database
->merge($tableName)
->key($keys)
->fields($fields)
->execute();
$result = [
'status' => $this
->t('success updated'),
];
} catch (Exception $e) {
// Exception handling if something else gets thrown.
$this->loggerFactory
->error($e
->getMessage());
}
$response = new ResourceResponse($result);
$response
->addCacheableDependency($result);
return $response;
}