generated from liutk/owl-admin-base
62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Train;
|
|
|
|
use App\Admin\Services\Train\PaperService;
|
|
use App\Enums\ExamStatus;
|
|
use App\Exceptions\RuntimeException;
|
|
use App\Http\Controllers\Api\Controller;
|
|
use App\Http\Resources\TrainPaperResource;
|
|
use App\Models\Train\Paper;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* 培训-考试
|
|
*/
|
|
class ExaminationController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$user = $this->guard()->user();
|
|
$list = Paper::with(['examination'])
|
|
->where('employee_id', $user->id)
|
|
->whereHas('examination', fn ($q) => $q->where('exam_status', ExamStatus::Published))
|
|
->orderBy('created_at', 'desc')
|
|
->paginate($request->input('per_page'));
|
|
|
|
return TrainPaperResource::collection($list);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$user = $this->guard()->user();
|
|
$info = Paper::with(['examination'])->where('employee_id', $user->id)->findOrFail($id);
|
|
|
|
return TrainPaperResource::make($info);
|
|
}
|
|
|
|
public function answer($id, Request $request, PaperService $service)
|
|
{
|
|
$request->validate([
|
|
'answers' => ['required', 'array'],
|
|
]);
|
|
$user = $this->guard()->user();
|
|
$info = Paper::with(['examination'])->where('employee_id', $user->id)->findOrFail($id);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
if (! $service->answer($info, $request->input('answers'))) {
|
|
throw new RuntimeException($service->getError());
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return TrainPaperResource::make($info);
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
throw new RuntimeException($e->getMessage());
|
|
}
|
|
}
|
|
}
|