Skip to content

Commit

Permalink
Merge pull request #122 from Facts-and-Files/feat/transcriptionProvider
Browse files Browse the repository at this point in the history
feat: transcription provider for htrdata
  • Loading branch information
trenc authored Sep 20, 2024
2 parents 2f98728 + abaf2df commit 1ef289a
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 3 deletions.
94 changes: 94 additions & 0 deletions src/app/Http/Controllers/TranscriptionProviderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\ResponseController;
use App\Models\TranscriptionProvider;
use App\Http\Resources\TranscriptionProviderResource;

class TranscriptionProviderController extends ResponseController
{
public function index(Request $request): JsonResponse
{
$queryColumns = [];

$initialSortColumn = 'TranscriptionProviderId';

$model = new TranscriptionProvider();

$data = $this->getDataByRequest($request, $model, $queryColumns, $initialSortColumn);

if (!$data) {
return $this->sendError('Invalid data', $request . ' not valid', 400);
}

$collection = TranscriptionProviderResource::collection($data);

return $this->sendResponseWithMeta($collection, 'TranscriptionProviders fetched.');
}

public function show(int $id): JsonResponse
{
try {
$data = TranscriptionProvider::findOrFail($id);
$resource = new TranscriptionProviderResource($data);

return $this->sendResponse($resource, 'TranscriptionProvider fetched.');
} catch (\Exception $exception) {
return $this->sendError('Not found', $exception->getMessage());
}
}

public function store(Request $request): JsonResponse
{
try {
$data = new TranscriptionProvider();
$data->fill($request->all());
$data->save();

return $this->sendResponse(new TranscriptionProviderResource($data), 'Transcription provider inserted.');
} catch (\Exception $exception) {
return $this->sendError('Invalid data', $exception->getMessage(), 400);
}
}


public function update(Request $request, int $id): JsonResponse
{
try {
$data = TranscriptionProvider::findOrfail($id);
} catch(\Exception $exception) {
return $this->sendError('Not found', $exception->getMessage(), 404);
}

try {
$data->fill($request->all());
$data->save();

return $this->sendResponse(new TranscriptionProviderResource($data), 'Transcription provider updated.');
} catch(\Exception $exception) {
return $this->sendError('Invalid data', $exception->getMessage(), 400);
}
}

public function destroy(int $id): JsonResponse
{
try {
$data = TranscriptionProvider::findOrfail($id);
} catch(\Exception $exception) {
return $this->sendError('Not found', $exception->getMessage(), 404);
}

try {
$resource = $data->toArray();
$resource = new TranscriptionProviderResource($resource);
$data->delete();

return $this->sendResponse($resource, 'Transcription provider deleted.');
} catch(\Exception $exception) {
return $this->sendError('Invalid data', $exception->getMessage(), 400);
}
}
}
13 changes: 13 additions & 0 deletions src/app/Http/Resources/TranscriptionProviderResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class TranscriptionProviderResource extends JsonResource
{
public function toArray($request): array
{
return parent::toArray($request);
}
}
16 changes: 15 additions & 1 deletion src/app/Models/HtrData.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ class HtrData extends Model
'Language'
];

protected $hidden = ['TranscriptionProviderId'];

protected $appends = [
'UserId',
'TranscriptionData',
'TranscriptionText',
'Language'
'Language',
'TranscriptionProvider',
];

public function transcriptionProvider(): BelongsTo
{
return $this->belongsTo(TranscriptionProvider::class, 'TranscriptionProviderId');
}

public function item(): BelongsTo
{
return $this->belongsTo(Item::class, 'ItemId');
Expand All @@ -59,6 +67,12 @@ public function language(): BelongsToMany

// we make usage of some custom accessors and mutators

public function getTranscriptionProviderAttribute(): string
{
$transcriptionProvider = $this->transcriptionProvider()->first();
return $transcriptionProvider ? $transcriptionProvider['Name'] : 'No provider';
}

public function getLanguageAttribute(): Collection
{
return $this->language()->get();
Expand Down
23 changes: 23 additions & 0 deletions src/app/Models/TranscriptionProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class TranscriptionProvider extends Model
{
const CREATED_AT = null;
const UPDATED_AT = null;

protected $table = 'TranscriptionProvider';

protected $primaryKey = 'TranscriptionProviderId';

protected $guarded = ['TranscriptionProviderId'];

public function htrData(): HasMany
{
return $this->hasMany(HtrData::class, 'TranscriptionProviderId');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
public function up()
{
Schema::create('TranscriptionProvider', function (Blueprint $table) {
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';

$table->smallIncrements('TranscriptionProviderId');
$table->string('Name');
});

DB::table('TranscriptionProvider')->insert([
['Name' => 'ReadCoop-Transkribus'],
['Name' => 'CrossLang-Occam']
]);
}

public function down()
{
Schema::dropIfExists('TranscriptionProvider');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
public function up()
{
Schema::table('HtrData', function (Blueprint $table) {

$table
->unsignedSmallInteger('TranscriptionProviderId')
->after('EuropeanaAnnotationId')
->nullable();

$table->foreign('TranscriptionProviderId')
->references('TranscriptionProviderId')
->on('TranscriptionProvider')
->nullOnDelete();
});

DB::table('HtrData')->update(['TranscriptionProviderId' => 1]);
}

public function down()
{
Schema::table('HtrData', function (Blueprint $table) {
$table->dropForeign(['TranscriptionProviderId']);
$table->dropColumn('TranscriptionProviderId');
});
}
};
7 changes: 7 additions & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use App\Http\Controllers\UserStatsController;
use App\Http\Controllers\ScoreController;
use App\Http\Controllers\StatisticsController;
use App\Http\Controllers\TranscriptionProviderController;

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -127,5 +128,11 @@
Route::put('/places/{id}', [PlaceController::class, 'update']);
Route::delete('/places/{id}', [PlaceController::class, 'destroy']);

Route::get('/transcription-providers', [TranscriptionProviderController::class, 'index']);
Route::post('/transcription-providers', [TranscriptionProviderController::class, 'store']);
Route::get('/transcription-providers/{id}', [TranscriptionProviderController::class, 'show']);
Route::put('/transcription-providers/{id}', [TranscriptionProviderController::class, 'update']);
Route::delete('/transcription-providers/{id}', [TranscriptionProviderController::class, 'destroy']);

Route::post('/import', [ImportController::class, 'store']);
});
7 changes: 6 additions & 1 deletion src/storage/api-docs/api-docs.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.3

info:
version: 1.50.0
version: 1.52.0
title: Transcribathon Platform API v2
description: This is the documentation of the Transcribathon API v2 used by [https:transcribathon.eu](https://transcribathon.eu/).<br />
For authorization you can use the the bearer token you are provided with.
Expand Down Expand Up @@ -159,6 +159,11 @@ paths:
/scores:
$ref: 'scores-path.yaml'

/transcription-providers:
$ref: 'transcription-providers-path.yaml'
/transcription-providers/{TranscriptionProviderId}:
$ref: 'transcription-providers-transcriptionProviderId-path.yaml'

/stories:
$ref: 'stories-path.yaml'
/stories/campaigns:
Expand Down
14 changes: 14 additions & 0 deletions src/storage/api-docs/htrdata-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ HtrDataPostRequestSchema:
type: integer
description: Array of LanguageId associated with this data
example: 4
TranscriptionProviderId:
type: integer
nullable: true
description: ID of the TranscriptionProvider
example: 1

HtrDataGetResponseSchema:
allOf:
Expand All @@ -80,6 +85,10 @@ HtrDataGetResponseSchema:
type: array
items:
$ref: 'language-reference-schema.yaml#/LanguageReferenceSchema'
TranscriptionProvider:
type: string
description: Name TranscriptionProvider
example: ReadCoop-Transkribus
- $ref: '#/HtrDataMinimalReferenceSchema'

HtrDataPutRequestSchema:
Expand All @@ -93,4 +102,9 @@ HtrDataPutRequestSchema:
type: integer
description: Array of LanguageId associated with this data
example: 4
TranscriptionProviderId:
type: integer
nullable: true
description: ID of the TranscriptionProvider
example: 1
- $ref: '#/HtrDataMinimalReferenceSchema'
62 changes: 62 additions & 0 deletions src/storage/api-docs/transcription-providers-path.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
get:
tags:
- htrdata
summary: Get all stored transcription providers
description: The index endpoint can be used to get all teams. The output is limited to 100 but can be filtered, sorted and paginated. The returned data is always an array even if the filter criteria is a unique identifier.
parameters:
- $ref: 'basic-query-parameter.yaml#/PaginationParameters/limit'
- $ref: 'basic-query-parameter.yaml#/PaginationParameters/page'
- in: query
name: orderBy
default: TranscriptionProviderId
description: Table column to order the return
schema:
type: string
- $ref: 'basic-query-parameter.yaml#/SortParameters/orderDir'
responses:
200:
description: Ok
content:
application/json:
schema:
allOf:
- $ref: 'responses.yaml#/BasicSuccessResponse'
- properties:
meta:
type: object
description: Meta data with pagination details
$ref: 'meta-responses.yaml#MetaPaginationResponse'
data:
type: array
description: A transcription provider entry objects as array
items:
$ref: 'transcription-providers-schema.yaml#/TranscriptionProvidersGetResponseSchema'
401:
$ref: 'responses.yaml#/401ErrorResponse'
post:
tags:
- htrdata
summary: Store a new transcription provider entry
requestBody:
required: true
content:
application/json:
schema:
$ref: 'transcription-providers-schema.yaml#/TranscriptionProvidersPostRequestSchema'
responses:
200:
description: Ok
content:
application/json:
schema:
allOf:
- $ref: 'responses.yaml#/BasicSuccessResponse'
- properties:
message:
example: Transcription provider inserted.
data:
$ref: 'transcription-providers-schema.yaml#/TranscriptionProvidersGetResponseSchema'
401:
$ref: 'responses.yaml#/401ErrorResponse'
400:
$ref: 'responses.yaml#/400ErrorResponse'
Loading

0 comments on commit 1ef289a

Please sign in to comment.