Roman_applicant-site/app/Http/Controllers/Api/CalculatorController.php

48 lines
1.4 KiB
PHP
Raw Normal View History

2024-03-07 13:58:54 +03:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
2024-03-07 15:09:01 +03:00
use App\Models\Department;
2024-03-07 13:58:54 +03:00
use App\Models\Direction;
use App\Models\EntranceExamination;
2024-03-07 15:09:01 +03:00
use App\Models\Faculty;
2024-03-07 13:58:54 +03:00
use Illuminate\Http\Request;
class CalculatorController extends Controller
{
public function findDirectionFromSubjects(Request $request)
{
2024-03-07 15:09:01 +03:00
$getJSON = $request->input()['predmets'];
$getsSubjects = json_decode($getJSON);
2024-03-07 13:58:54 +03:00
$countUserSubjects = count($getsSubjects);
2024-03-07 15:09:01 +03:00
$filteredDirectionIds = EntranceExamination::all()
2024-03-07 13:58:54 +03:00
->select("direction_id", "subject_id")
->groupBy('direction_id')
->map(function ($direction) {
return $direction->map(fn($item) => $item['subject_id']);
})
2024-03-07 15:09:01 +03:00
->filter(fn($direction) => count($direction) <= $countUserSubjects)
2024-03-07 13:58:54 +03:00
->keys();
2024-03-07 15:09:01 +03:00
$directions = Direction::whereIn('id', $filteredDirectionIds)->get();
$generateHtml = function ($acc, $direction) {
$department = Department::find($direction->department_id);
$faculty = Faculty::find($department->faculty_id);
return "{$acc} <tr class=\"\">
<td id=\"faculty\"> {$faculty->name} </td>
<td> {$direction->name} </td>
<td class=\"text-end\"> {$direction->period} </td>
</tr>";
};
$html = $directions->reduce($generateHtml, '');
return response()->json(['html' => $html]);
2024-03-07 13:58:54 +03:00
}
}