Skip to content

Commit

Permalink
585 add functional areas to mrf (#599)
Browse files Browse the repository at this point in the history
* initial backend code complete

* imporve validation before manuscript can be submitted

* initial front-end progress

* refactor to use defineModel

* frontend functional area
  • Loading branch information
vincentauger authored Apr 25, 2024
1 parent adbcab9 commit 6eb893a
Show file tree
Hide file tree
Showing 25 changed files with 2,400 additions and 2,537 deletions.
18 changes: 18 additions & 0 deletions app/Http/Controllers/FunctionalAreaController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers;

use App\Http\Resources\FunctionalAreaResource;
use App\Models\FunctionalArea;
use Illuminate\Http\Resources\Json\ResourceCollection;

class FunctionalAreaController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): ResourceCollection
{
return FunctionalAreaResource::collection(FunctionalArea::all());
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/ManuscriptRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function update(Request $request, ManuscriptRecord $manuscriptRecord): Js
$validated = $request->validate([
'title' => 'string|max:255',
'region_id' => 'numeric|exists:regions,id',
'functional_area_id' => 'numeric|exists:functional_areas,id',
'type' => [new Enum(ManuscriptRecordType::class)],
'abstract' => 'nullable|string',
'pls' => 'nullable|string',
Expand Down
29 changes: 29 additions & 0 deletions app/Http/Resources/FunctionalAreaResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class FunctionalAreaResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'data' => [
'id' => $this->id,
'name_en' => $this->name_en,
'name_fr' => $this->name_fr,
],
'can' => [
'update' => false,
'delete' => false,
],
];
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/ManuscriptRecordResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function toArray($request)
'title' => $this->title,
'region_id' => $this->region_id,
'user_id' => $this->user_id,
'functional_area_id' => $this->functional_area_id,

// text fields / send empty string if null.
'abstract' => $this->abstract ?? '',
Expand All @@ -44,6 +45,7 @@ public function toArray($request)

//relationships - if loaded
'region' => RegionResource::make($this->whenLoaded('region')),
'functional_area' => FunctionalAreaResource::make($this->whenLoaded('functionalArea')),
'manuscript_authors' => ManuscriptAuthorResource::collection($this->whenLoaded('manuscriptAuthors')),
'user' => UserResource::make($this->whenLoaded('user')),
'publication' => $this->when($this->status === ManuscriptRecordStatus::ACCEPTED, PublicationResource::make($this->whenLoaded('publication'))),
Expand Down
17 changes: 17 additions & 0 deletions app/Models/FunctionalArea.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models;

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

class FunctionalArea extends Model
{
use HasFactory;

public function manuscriptRecords(): HasMany
{
return $this->hasMany('App\Models\ManuscriptRecord');
}
}
15 changes: 13 additions & 2 deletions app/Models/ManuscriptRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function region(): BelongsTo
return $this->belongsTo('App\Models\Region');
}

/**
* A manuscript has a functional area.
*/
public function functionalArea(): BelongsTo
{
return $this->belongsTo('App\Models\FunctionalArea');
}

/**
* A manuscripts has several ManuscriptAuthors
*/
Expand Down Expand Up @@ -161,7 +169,7 @@ public function getManuscriptFile($uuid)
public function deleteManuscriptFile($uuid, $force = false)
{
$media = $this->getMedia('manuscript')->where('uuid', $uuid)->first();
if (! $media) {
if (!$media) {
throw new FileNotFoundException('File not found.');
}
if ($force) {
Expand Down Expand Up @@ -201,6 +209,9 @@ public function validateIsFilled(bool $noExceptions = false): bool
'abstract' => 'required',
'pls' => 'required',
'relevant_to' => 'required',
'region_id' => 'required|exists:regions,id',
'functional_area_id' => 'required|exists:functional_areas,id',
'relevant_to' => 'required',
]);

$validator->after(function ($validator) {
Expand All @@ -212,7 +223,7 @@ public function validateIsFilled(bool $noExceptions = false): bool
}
});

if (! $noExceptions) {
if (!$noExceptions) {
$validator->validate();
}

Expand Down
24 changes: 24 additions & 0 deletions database/factories/FunctionalAreaFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\FunctionalArea>
*/
class FunctionalAreaFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name_en' => $this->faker->word,
'name_fr' => $this->faker->word,
];
}
}
4 changes: 3 additions & 1 deletion database/factories/ManuscriptRecordFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\ManuscriptRecordStatus;
use App\Enums\ManuscriptRecordType;
use App\Models\FunctionalArea;
use App\Models\FundingSource;
use App\Models\ManuscriptAuthor;
use App\Models\User;
Expand Down Expand Up @@ -44,11 +45,12 @@ public function filled()
'relevant_to' => $this->faker->paragraph(),
'potential_public_interest' => $this->faker->boolean(),
'additional_information' => $this->faker->paragraph(),
'functional_area_id' => FunctionalArea::factory()->create()->id,
])->afterCreating(function ($manuscript) {
$manuscript->manuscriptAuthors()->save(ManuscriptAuthor::factory()->make(['is_corresponding_author' => true])); // create a corresponding author
$manuscript->manuscriptAuthors()->saveMany(ManuscriptAuthor::factory()->count(3)->make()); // create 3 other authors
$manuscript->fundingSources()->saveMany(FundingSource::factory()->count(3)->make()); // create 3 funding sources
$manuscript->addMedia(getcwd().'/database/factories/files/BieberFever.pdf')->preservingOriginal()->toMediaCollection('manuscript'); // add a manuscript file
$manuscript->addMedia(getcwd() . '/database/factories/files/BieberFever.pdf')->preservingOriginal()->toMediaCollection('manuscript'); // add a manuscript file
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('functional_areas', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name_en');
$table->string('name_fr');
});

// seed the table
Artisan::call('db:seed', ['--class' => 'FunctionalAreaSeeder']);
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('functional_areas');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('manuscript_records', function (Blueprint $table) {
$table->foreignId('functional_area_id')->nullable()->constrained();
});
}
};
43 changes: 43 additions & 0 deletions database/seeders/FunctionalAreaSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class FunctionalAreaSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
/**
* Functional Areas
*
* Fisheries Science
* Aquatic Ecosystem Science
* Aquatic Animal Health
* Biotechnology and Genomics
* Aquaculture Science
* Oceans and Climate Change Science
* Hydrographic Services, Data and Science
* Aquatic invasive species
*
*/
$functionalAreas = [
['name_en' => 'Fisheries Science', 'name_fr' => 'Sciences halieutiques'],
['name_en' => 'Aquatic Ecosystem Science', 'name_fr' => 'Science écosystèmes aquatiques'],
['name_en' => 'Aquatic Animal Health', 'name_fr' => 'Santé animaux aquatiques'],
['name_en' => 'Biotechnology and Genomics', 'name_fr' => 'Biotechnologie et génomique'],
['name_en' => 'Aquaculture Science', 'name_fr' => 'Science de l\'aquaculture'],
['name_en' => 'Oceans and Climate Change Science', 'name_fr' => 'Science des océans & changements climatiques'],
['name_en' => 'Hydrographic Services, Data and Science', 'name_fr' => 'Services hydrographiques, données et science'],
['name_en' => 'Aquatic invasive species', 'name_fr' => 'Espèces aquatiques envahissantes'],
['name_en' => 'Species at Risk', 'name_fr' => 'Espèces en péril']
];

foreach ($functionalAreas as $functionalArea) {
\App\Models\FunctionalArea::create($functionalArea);
}
}
}
Loading

0 comments on commit 6eb893a

Please sign in to comment.