From 0586e04204885088f31ac9861446eb0759cc8d2f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 5 Jan 2023 20:41:27 +0100 Subject: [PATCH] tech relations --- app/Http/Controllers/TechniqueController.php | 2 +- app/Models/Technique.php | 7 ++++ app/Models/TechniqueRelation.php | 10 ++++++ ...90010_create_technique_relations_table.php | 34 +++++++++++++++++++ resources/js/components/techniques/Detail.js | 12 ++++++- resources/js/helpers/Technique.js | 15 ++++++++ resources/js/i18n/de.js | 1 + resources/js/i18n/en.js | 1 + 8 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 app/Models/TechniqueRelation.php create mode 100644 database/migrations/2023_01_05_190010_create_technique_relations_table.php diff --git a/app/Http/Controllers/TechniqueController.php b/app/Http/Controllers/TechniqueController.php index 268c3f6..9862325 100644 --- a/app/Http/Controllers/TechniqueController.php +++ b/app/Http/Controllers/TechniqueController.php @@ -34,7 +34,7 @@ class TechniqueController extends Controller public function single(Request $request, Technique $tech) { $this->authorize('view', $tech); - $tech->load('chapters'); + $tech->load(['chapters', 'relations']); return $tech->toJson(); } diff --git a/app/Models/Technique.php b/app/Models/Technique.php index 256afc0..f0a29d3 100644 --- a/app/Models/Technique.php +++ b/app/Models/Technique.php @@ -17,6 +17,13 @@ class Technique extends Model ->using(TechniqueChapter::class); } + public function relations() { + return $this + ->belongsToMany(Technique::class, 'technique_relations', 'from_id', 'to_id') + ->withPivot('type') + ->using(TechniqueRelation::class); + } + public function translations() { return $this->hasMany(TechniqueTranslation::class); } diff --git a/app/Models/TechniqueRelation.php b/app/Models/TechniqueRelation.php new file mode 100644 index 0000000..96c73c3 --- /dev/null +++ b/app/Models/TechniqueRelation.php @@ -0,0 +1,10 @@ +id(); + $table->foreignId('from_id')->references('id')->on('techniques')->constrained(); + $table->foreignId('to_id')->references('id')->on('techniques')->constrained(); + $table->string('type')->default('related'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('technique_relations'); + } +}; diff --git a/resources/js/components/techniques/Detail.js b/resources/js/components/techniques/Detail.js index 303921c..488205b 100644 --- a/resources/js/components/techniques/Detail.js +++ b/resources/js/components/techniques/Detail.js @@ -3,9 +3,15 @@ import React from 'react'; import { Container } from 'react-bootstrap'; import { withTranslation } from 'react-i18next'; +import List from './List'; import Outline from './Outline'; import RawHTML from '../common/RawHTML'; -import { getTranslation } from '../../helpers/Technique'; +import { + getRelations, + getTranslation, + hasRelations, + sorted, +} from '../../helpers/Technique'; import i18n from '../../i18n'; const Detail = ({ technique }) => @@ -24,6 +30,10 @@ const Detail = ({ technique }) => ) : null} + {hasRelations(technique, 'related') ? <> +

{i18n.t('techniques.seeAlso')}

+ + : null}
; Detail.propTypes = { diff --git a/resources/js/helpers/Technique.js b/resources/js/helpers/Technique.js index 7ec22d3..410f015 100644 --- a/resources/js/helpers/Technique.js +++ b/resources/js/helpers/Technique.js @@ -1,3 +1,12 @@ +import i18n from '../i18n'; + +export const getRelations = (tech, type) => { + const rs = (tech && tech.relations) || []; + return type ? rs.filter(r => r && r.pivot && r.pivot.type === type) : rs; +}; + +export const hasRelations = (tech, type) => getRelations(tech, type).length > 0; + export const getTranslation = (tech, prop, lang) => { const direct = tech.translations.find(t => t.locale === lang); if (direct) { @@ -13,6 +22,12 @@ export const getTranslation = (tech, prop, lang) => { export const compareTranslation = (prop, lang) => (a, b) => getTranslation(a, prop, lang).localeCompare(getTranslation(b, prop, lang)); +export const sorted = (techs) => [...techs].sort(compareTranslation('title', i18n.language)); + export default { + compareTranslation, + getRelations, getTranslation, + hasRelations, + sorted, }; diff --git a/resources/js/i18n/de.js b/resources/js/i18n/de.js index c28c1b2..f3fbe96 100644 --- a/resources/js/i18n/de.js +++ b/resources/js/i18n/de.js @@ -480,6 +480,7 @@ export default { }, techniques: { heading: 'Techniken', + seeAlso: 'Siehe auch', }, tournaments: { admins: 'Organisation', diff --git a/resources/js/i18n/en.js b/resources/js/i18n/en.js index dfff38b..7088dd4 100644 --- a/resources/js/i18n/en.js +++ b/resources/js/i18n/en.js @@ -480,6 +480,7 @@ export default { }, techniques: { heading: 'Techniques', + seeAlso: 'See also', }, tournaments: { admins: 'Admins', -- 2.39.2