Skip to content

Commit

Permalink
feat: enable DataCite metadata update
Browse files Browse the repository at this point in the history
fixes #1125
  • Loading branch information
NRayya committed Apr 5, 2024
1 parent 4f9aa27 commit 48e1506
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
37 changes: 37 additions & 0 deletions app/Http/Controllers/API/Schemas/DataCite/DOIController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers\API\Schemas\DataCite;

use App\Http\Controllers\Controller;
use App\Services\DOI\DOIService;
use Illuminate\Http\Request;

class DOIController extends Controller
{
/**
* Create a new class instance.
*
* @param App\Services\DOI\DOIService $doiService
* @return void
*/
public function __construct(DOIService $doiService)
{
$this->doiService = $doiService;
}

/**
* Update Model's DataCite metadata
*
* @param Illuminate\Http\Request $request
* @param string $identifier
* @return void
*/
public function update(Request $request, $identifier)
{
$resolvedModel = resolveIdentifier($identifier);
$model = $resolvedModel['model'];

// Call the updateDOI function
$model->updateDOIMetadata($this->doiService);
}
}
19 changes: 18 additions & 1 deletion app/Models/HasDOI.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,25 @@ public function generateDOI($doiService)
}
}

public function updateDOI($doiService)
/**
* Update Model's DataCite metadata
*
* @param mixed $doiService
* @return void
*/
public function updateDOIMetadata($doiService)
{
$doi_host = env('DOI_HOST', null);

if (! is_null($doi_host)) {
$doi = $this->doi;
if ($doi !== null) {
$attributes = $this->getMetadata();
$doiResponse = $doiService->updateDOI($doi, $attributes);
$this->datacite_schema = $doiResponse;
$this->save();
}
}
}

public function getIdentifier($model, $key)
Expand Down
2 changes: 1 addition & 1 deletion app/Services/DOI/DOIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function createDOI($suffix, $attributes = []);

public function getDOI($doi);

public function updateDOI($doi);
public function updateDOI($doi, $attributes = []);

public function deleteDOI($doi);

Expand Down
30 changes: 28 additions & 2 deletions app/Services/DOI/DataCite.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,35 @@ public function createDOI($suffix, $metadata = [])
return json_decode($contents, true);
}

public function updateDOI($doi)
/**
* Update DataCite metadata based on DOI
*
* @param string $doi
* @param array $metadata
* @return array $contents
*/
public function updateDOI($doi, $metadata = [])
{
return 'Datacite';
foreach ($metadata as $key => $value) {
$attributes[$key] = $value;
}

$body = [
'data' => [
'type' => 'dois',
'attributes' => $attributes,
],
];

$response = $this->client->put('/dois/'.urlencode($doi),
[RequestOptions::JSON => $body]
);

$stream = $response->getBody();
$contents = $stream->getContents();
$contents = json_decode($contents, true);

return $contents;
}

public function deleteDOI($doi)
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Controllers\API\Schemas\Bioschemas\BioschemasController;
use App\Http\Controllers\API\Schemas\Bioschemas\DataCatalogController;
use App\Http\Controllers\API\Schemas\DataCite\DataCiteController;
use App\Http\Controllers\API\Schemas\DataCite\DOIController;
use App\Http\Controllers\API\SearchController;
use Illuminate\Support\Facades\Route;

Expand Down Expand Up @@ -54,6 +55,7 @@
Route::prefix('datacite')->group(function () {
Route::get('/{username}/{project}/{study?}/{dataset?}', [DataCiteController::class, 'modelSchemaByName']);
Route::get('/{id}', [DataCiteController::class, 'modelSchemaByID']);
Route::put('/{id}', [DOIController::class, 'update']);
});
});
});

0 comments on commit 48e1506

Please sign in to comment.