Skip to content

Commit

Permalink
Adding a custom endpoint to support CRUD for entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
swarad07 committed Dec 21, 2021
1 parent bc8d3cb commit 276498c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 6 deletions.
21 changes: 15 additions & 6 deletions sitedash.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sitedash.dashboard:

# Entity permissions.
entity.sitedash_entity.canonical:
path: '/sitedash_entity/{sitedash_entity}'
path: '/admin/sitedash_entity/{sitedash_entity}'
defaults:
# Calls the view controller, defined in the annotation of the sitedash entity
_entity_view: 'sitedash_entity'
Expand All @@ -18,7 +18,7 @@ entity.sitedash_entity.canonical:
_entity_access: 'sitedash_entity.view'

entity.sitedash_entity.collection:
path: '/sitedash_entity/list'
path: '/admin/sitedash_entity/list'
defaults:
# Calls the list controller, defined in the annotation of the sitedash entity.
_entity_list: 'sitedash_entity'
Expand All @@ -28,7 +28,7 @@ entity.sitedash_entity.collection:
_permission: 'administer sitedash entity'

sitedash.sitedash_add:
path: '/sitedash_entity/add'
path: '/admin/sitedash_entity/add'
defaults:
# Calls the form.add controller, defined in the sitedash entity.
_entity_form: sitedash_entity.add
Expand All @@ -37,7 +37,7 @@ sitedash.sitedash_add:
_entity_create_access: 'sitedash_entity'

entity.sitedash_entity.edit_form:
path: '/sitedash_entity/{sitedash_entity}/edit'
path: '/admin/sitedash_entity/{sitedash_entity}/edit'
defaults:
# Calls the form.edit controller, defined in the sitedash entity.
_entity_form: sitedash_entity.edit
Expand All @@ -46,7 +46,7 @@ entity.sitedash_entity.edit_form:
_entity_access: 'sitedash_entity.edit'

entity.sitedash_entity.delete_form:
path: '/sitedash/{sitedash_entity}/delete'
path: '/admin/sitedash/{sitedash_entity}/delete'
defaults:
# Calls the form.delete controller, defined in the sitedash entity.
_entity_form: sitedash_entity.delete
Expand All @@ -55,9 +55,18 @@ entity.sitedash_entity.delete_form:
_entity_access: 'sitedash_entity.delete'

sitedash.sitedash_settings:
path: 'admin/structure/sitedash_entity_settings'
path: '/admin/structure/sitedash_entity_settings'
defaults:
_form: '\Drupal\sitedash\Form\SitedashSettingsForm'
_title: 'Sitedash Settings'
requirements:
_permission: 'administer sitedash entity'

sitedash.endpoint:
path: '/sitedash/api/v1/operations'
defaults:
_controller: '\Drupal\sitedash\Controller\SiteDashAPIController::getData'
_title: 'Sitedash API'
methods: [GET, POST, DELETE]
requirements:
_permission: 'administer site configuration'
77 changes: 77 additions & 0 deletions src/Controller/SiteDashAPIController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace Drupal\sitedash\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\sitedash\Entity\Sitedash;
use Laminas\Diactoros\Response\JsonResponse;

/**
* A custom API endpoint for Sitedash portal.
*/
class SiteDashAPIController extends ControllerBase {

/**
* Returns a JSON response.
*/
public function getData() {
// This condition checks the `Content-type` and makes sure to
// decode JSON string from the request body into array.
$response = [];
$request = \Drupal::request();
$response['method'] = $request->getMethod();
$data = $request->getContent();
$decoded_data = json_decode($data, TRUE);
// Basic Checks.
if ($decoded_data === NULL) {
$response['error_data'] = $this->t('Not an acceptable JSON Data.');
return new JsonResponse($response);
}

// Create/Update.
if ($response['method'] === 'POST') {
// If id then we do update.
if (!isset($decoded_data['id'])) {
$sitedash_entity = Sitedash::create([
'name' => $decoded_data['name'],
'siteFavicon' => $decoded_data['faviconUrl'],
'siteUrl' => $decoded_data['url'],
'siteAPIUrl' => $decoded_data['endpoint'],
'siteToken' => $decoded_data['token'],
]);
}
else {
$sitedash_entity = Sitedash::load($decoded_data['id']);
if (isset($sitedash_entity)) {
$sitedash_entity->set('name', $decoded_data['name']);
$sitedash_entity->set('siteFavicon', $decoded_data['faviconUrl']);
$sitedash_entity->set('siteUrl', $decoded_data['url']);
$sitedash_entity->set('siteAPIUrl', $decoded_data['endpoint']);
$sitedash_entity->set('siteToken', $decoded_data['token']);
}
else {
$response['error_data'] = $this->t('No entity found.');
return new JsonResponse($response);
}
}
// Expected: SAVED_NEW = 1 and SAVED_UPDATED = 2.
$entity_saved_flag = $sitedash_entity->save();
$response['entity_saved_flag'] = $entity_saved_flag;
return new JsonResponse($response);
}

// Delete.
if ($response['method'] === 'DELETE') {
$sitedash_entity = \Drupal::entityTypeManager()->getStorage('sitedash_entity')->load($decoded_data['id']);
try {
$flag = $sitedash_entity->delete();
$response['entity_saved_flag'] = 'Entity deleted';
return new JsonResponse($response);
}
catch (\Exception $e) {
$response['error_data'] = $e->getMessage();
return new JsonResponse($response);
}
}
}

}

0 comments on commit 276498c

Please sign in to comment.