Skip to content

Commit

Permalink
Merge pull request #10 from delaynore/feature/concept-relations
Browse files Browse the repository at this point in the history
Feature/concept relations
  • Loading branch information
delaynore committed May 22, 2024
2 parents 1c4fb13 + 84ba932 commit 4e971ae
Show file tree
Hide file tree
Showing 34 changed files with 921 additions and 162 deletions.
6 changes: 6 additions & 0 deletions app/Http/Controllers/AttributeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Requests\StoreAttributeRequest;
use App\Http\Requests\UpdateAttributeRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;

Expand Down Expand Up @@ -61,6 +62,7 @@ public function store(Request $request)
*/
public function edit(Attribute $attribute)
{
Gate::authorize('redactor');
return view('attribute.edit', compact('attribute'));
}

Expand All @@ -69,6 +71,8 @@ public function edit(Attribute $attribute)
*/
public function update(Request $request, Attribute $attribute)
{
Gate::authorize('redactor');

$validated = $request->validate([
'name' => ['required', 'max:255', Rule::unique('attributes')->ignore($attribute->id)],
'type' => [new Enum(DataType::class)],
Expand All @@ -83,6 +87,8 @@ public function update(Request $request, Attribute $attribute)
*/
public function destroy(Attribute $attribute)
{
Gate::authorize('admin');

if ($attribute->concepts()->count() > 0) {
return redirect()->back()->with('attribute.delete.error', 'Атрибут не может быть удален, так как он используется в словарях. Удалите его из словарей');
}
Expand Down
51 changes: 49 additions & 2 deletions app/Http/Controllers/ConceptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
namespace App\Http\Controllers;

use App\Models\Concept;
use App\Models\ConceptRelation;
use App\Models\Dictionary;
use App\Models\RelationType;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;

Expand Down Expand Up @@ -68,7 +72,49 @@ public function show(string $dictionary, string $concept)
$concept = Concept::findOrFail($concept);
$dictionary = Dictionary::findOrFail($dictionary);
$concepts = $dictionary->rootConcepts();
return view('concept.show', compact('concept', 'dictionary', 'concepts'));

$query = ConceptRelation::where('fk_concept_1_id', $concept->id)->orWhere('fk_concept_2_id', $concept->id);
$conceptRelationTypes = $query->distinct('fk_relation_type_id')->get('fk_relation_type_id')->pluck('fk_relation_type_id')->reverse();

$conceptRelations = [];

foreach ($conceptRelationTypes as $conceptRelationType) {

$relatedConcepts1 = DB::table('concept_relations')
->join('concepts as concept_1', 'concept_relations.fk_concept_1_id', '=', 'concept_1.id')
->join('concepts as concept_2', 'concept_relations.fk_concept_2_id', '=', 'concept_2.id')
->join('relation_types', 'concept_relations.fk_relation_type_id', '=', 'relation_types.id')
->select(
'concept_relations.id as relation_id',
'concept_2.id AS concept_id',
'concept_2.name AS concept_name'
)
->where('concept_1.id', '=', $concept->id)
->where('concept_relations.fk_relation_type_id', '=', $conceptRelationType)
->get();

$relatedConcepts2 = DB::table('concept_relations')
->join('concepts as concept_1', 'concept_relations.fk_concept_1_id', '=', 'concept_1.id')
->join('concepts as concept_2', 'concept_relations.fk_concept_2_id', '=', 'concept_2.id')
->join('relation_types', 'concept_relations.fk_relation_type_id', '=', 'relation_types.id')
->select(
'concept_relations.id as relation_id',
'concept_1.id AS concept_id',
'concept_1.name AS concept_name'
)
->where('concept_2.id', '=', $concept->id)
->where('concept_relations.fk_relation_type_id', '=', $conceptRelationType)
->get();


$merged = array_merge($relatedConcepts1->toArray(), $relatedConcepts2->toArray());
array_push($conceptRelations, [
RelationType::find($conceptRelationType),
$merged
]);
}

return view('concept.show', compact('concept', 'dictionary', 'concepts', 'conceptRelations'));
}

/**
Expand Down Expand Up @@ -143,7 +189,8 @@ public function examples(string $dictionary, string $concept)
return view('concept.examples', compact('dictionary', 'concept', 'concepts'));
}

public function attachments(string $dictionary, string $concept){
public function attachments(string $dictionary, string $concept)
{
$concepts = Dictionary::findOrFail($dictionary)->rootConcepts();
$concept = Concept::findOrFail($concept);
$dictionary = Dictionary::findOrFail($dictionary);
Expand Down
88 changes: 69 additions & 19 deletions app/Http/Controllers/ConceptRelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,50 @@

namespace App\Http\Controllers;

use App\Models\Concept;
use App\Models\ConceptRelation;
use App\Http\Requests\StoreConceptRelationRequest;
use App\Http\Requests\UpdateConceptRelationRequest;
use App\Models\Dictionary;
use App\Models\RelationType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ConceptRelationController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
public function create(Dictionary $dictionary, Concept $concept)
{
//
$relationTypes = RelationType::all();
$concepts = $dictionary->concepts()->orderBy('name')->get();
return view('concept-relation.create', compact('dictionary','concept', 'relationTypes', 'concepts'));
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreConceptRelationRequest $request)
public function store(Request $request, Dictionary $dictionary, Concept $concept)
{
//
$validated = $request->validate([
'fk_concept_1_id' => 'required|uuid|exists:concepts,id',
'fk_concept_2_id' => 'required|uuid|exists:concepts,id|different:fk_concept_1_id',
'fk_relation_type_id'=> 'required|uuid|exists:relation_types,id',
]);
$sameRelation = ConceptRelation::where('fk_relation_type_id', $validated['fk_relation_type_id'])->where('fk_concept_1_id', $validated['fk_concept_1_id'])
->where('fk_concept_2_id', $validated['fk_concept_2_id'])
->orWhere('fk_concept_2_id', $validated['fk_concept_1_id'])->where('fk_concept_1_id', $validated['fk_concept_2_id']);
if ($sameRelation->count() > 0) {
$messages = ['max' => __('dashboard.relation.create.errors.exist')];
Validator::validate($request->all(), [
'fk_relation_type_id' => 'max:0'
], $messages);
}
ConceptRelation::create($validated);

return redirect()->route('concept.show', compact('dictionary', 'concept'))->with('success', __('dashboard.relation.messages.created'));
}

/**
Expand All @@ -43,24 +59,58 @@ public function show(ConceptRelation $conceptRelation)
/**
* Show the form for editing the specified resource.
*/
public function edit(ConceptRelation $conceptRelation)
public function edit(string $dictionary, string $concept, string $conceptRelation)
{
//
$dictionary = Dictionary::findOrFail($dictionary);
Gate::authorize('must-be-owner', $dictionary);

$relationTypes = RelationType::all();
$concepts = $dictionary->concepts()->orderBy('name')->get();
$conceptRelation = ConceptRelation::findOrFail($conceptRelation);

return view('concept-relation.edit', compact('dictionary', 'concept', 'conceptRelation', 'relationTypes', 'concepts'));
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateConceptRelationRequest $request, ConceptRelation $conceptRelation)
public function update(Request $request, string $dictionary, string $concept, string $conceptRelation)
{
//
$dictionary = Dictionary::findOrFail($dictionary);
Gate::authorize('must-be-owner', $dictionary);

$validated = $request->validate([
'fk_concept_1_id' => 'required|uuid|exists:concepts,id',
'fk_concept_2_id' => 'required|uuid|exists:concepts,id|different:fk_concept_1_id',
'fk_relation_type_id'=> 'required|uuid|exists:relation_types,id',
]);
$sameRelation = ConceptRelation::where('fk_relation_type_id', $validated['fk_relation_type_id'])->where('fk_concept_1_id', $validated['fk_concept_1_id'])
->where('fk_concept_2_id', $validated['fk_concept_2_id'])
->orWhere('fk_concept_2_id', $validated['fk_concept_1_id'])->where('fk_concept_1_id', $validated['fk_concept_2_id']);
if ($sameRelation->count() > 1) {
$messages = ['max' => __('dashboard.relation.create.errors.exist')];
Validator::validate($request->all(), [
'fk_relation_type_id' => 'max:0'
], $messages);
}

$conceptRelation = ConceptRelation::findOrFail($conceptRelation);
$conceptRelation->updateOrFail($validated);
return redirect()->route('concept.show', compact('dictionary', 'concept'))->with('success',__('dashboard.relation.messages.updated'));
}

/**
* Remove the specified resource from storage.
*/
public function destroy(ConceptRelation $conceptRelation)
public function destroy(string $dictionary, string $concept, string $conceptRelation)
{
//
$dictionary = Dictionary::findOrFail($dictionary);
Gate::authorize('must-be-owner', $dictionary);

$conceptRelation = ConceptRelation::findOrFail($conceptRelation);

$conceptRelation->delete();

return redirect()->back()->with('error',__('dashboard.relation.messages.deleted'));
}
}
71 changes: 61 additions & 10 deletions app/Http/Controllers/RelationTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Models\RelationType;
use App\Http\Requests\StoreRelationTypeRequest;
use App\Http\Requests\UpdateRelationTypeRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class RelationTypeController extends Controller
{
Expand All @@ -13,23 +15,50 @@ class RelationTypeController extends Controller
*/
public function index()
{
//
$query = RelationType::query();
if (request('search')) {
$query->where('name', 'ilike', '%' . request('search') . '%');
}

$relationTypes = $query->paginate(15);

return view("relation-type.index", compact('relationTypes'));
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
Gate::authorize('redactor');

return view('relation-type.create');
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreRelationTypeRequest $request)
public function store(Request $request)
{
//
Gate::authorize('redactor');

$validated = $this->validate($request, [
'name' => 'required|string|unique:relation_types,name|unique:relation_types,name_plural',
'description' => 'nullable|string',
'name_plural' => 'nullable|string|unique:relation_types,name|unique:relation_types,name_plural'
]);

if($validated['name_plural'] == null) {
$validated['name_plural'] = $validated['name'];
}

RelationType::create($validated);

return redirect()->route('relation-type.index')
->with(
'success',
__('relation-type-page.messages.created')
);
}

/**
Expand All @@ -43,24 +72,46 @@ public function show(RelationType $relationType)
/**
* Show the form for editing the specified resource.
*/
public function edit(RelationType $relationType)
public function edit(string $relationType)
{
//
Gate::authorize('redactor');
$relationType = RelationType::findOrFail($relationType);

return view('relation-type.edit', compact('relationType'));
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateRelationTypeRequest $request, RelationType $relationType)
public function update(Request $request, string $relationType)
{
//
Gate::authorize('redactor');
$validated = $this->validate($request, [
'name' => 'required|string|unique:relation_types,name,' . $relationType.'|unique:relation_types,name_plural,'. $relationType,
'description' => 'nullable|string',
'name_plural' => 'nullable|string|unique:relation_types,name_plural,'. $relationType.'|unique:relation_types,name,' . $relationType
]);

RelationType::findOrFail($relationType)->update($validated);

return redirect()->route('relation-type.index')
->with(
'success',
__('relation-type-page.messages.updated')
);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(RelationType $relationType)
public function destroy(string $relationType)
{
//
Gate::authorize('admin');
RelationType::findOrFail($relationType)->delete();
return redirect()->route('relation-type.index')
->with(
'success',
__('relation-type-page.messages.deleted')
);
}
}
6 changes: 6 additions & 0 deletions app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class TagController extends Controller
{
Expand Down Expand Up @@ -54,6 +55,7 @@ public function store(Request $request)
*/
public function edit(Tag $tag)
{
Gate::authorize('redactor');
return view('tag.edit', compact('tag'));
}

Expand All @@ -62,6 +64,8 @@ public function edit(Tag $tag)
*/
public function update(Request $request, Tag $tag)
{
Gate::authorize('redactor');

$validated = $request->validate([
'name' => 'required|unique:tags,name,' . $tag->id . '|max:50',
]);
Expand All @@ -80,6 +84,8 @@ public function update(Request $request, Tag $tag)
*/
public function destroy(Tag $tag)
{
Gate::authorize('admin');

$tag->deleteOrFail();

return redirect()->back()
Expand Down
Loading

0 comments on commit 4e971ae

Please sign in to comment.