mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-23 19:37:23 +08:00
[exam] finish assign and remove
This commit is contained in:
@@ -45,7 +45,7 @@ class Test extends Command
|
||||
public function handle()
|
||||
{
|
||||
$rep = new ExamRepository();
|
||||
$r = $rep->addProgress(1, 1, 250, 1);
|
||||
$r = $rep->addProgress(15, 1, 250, 1);
|
||||
dd($r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\UnauthorizedException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Throwable;
|
||||
@@ -47,6 +49,10 @@ class Handler extends ExceptionHandler
|
||||
return response()->json(fail($e->getMessage(), $e->guards()), 401);
|
||||
});
|
||||
|
||||
$this->renderable(function (UnauthorizedException $e) {
|
||||
return response()->json(fail($e->getMessage(), request()->all()), 403);
|
||||
});
|
||||
|
||||
$this->renderable(function (ValidationException $exception) {
|
||||
$errors = $exception->errors();
|
||||
$msg = Arr::first(Arr::first($errors));
|
||||
@@ -54,7 +60,9 @@ class Handler extends ExceptionHandler
|
||||
});
|
||||
|
||||
$this->renderable(function (NotFoundHttpException $e) {
|
||||
return response()->json(fail('No query result.', request()->all()), 404);
|
||||
if ($e->getPrevious() && $e->getPrevious() instanceof ModelNotFoundException) {
|
||||
return response()->json(fail('No query result.', request()->all()), 404);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -76,11 +84,23 @@ class Handler extends ExceptionHandler
|
||||
}
|
||||
return new JsonResponse(
|
||||
fail($msg, $data),
|
||||
$this->isHttpException($e) ? $e->getStatusCode() : 500,
|
||||
$this->getHttpStatusCode($e),
|
||||
$this->isHttpException($e) ? $e->getHeaders() : [],
|
||||
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
|
||||
);
|
||||
}
|
||||
|
||||
protected function getHttpStatusCode(Throwable $e)
|
||||
{
|
||||
if ($e instanceof \InvalidArgumentException || $e instanceof NexusException) {
|
||||
return 200;
|
||||
}
|
||||
if ($this->isHttpException($e)) {
|
||||
return $e->getStatusCode();
|
||||
}
|
||||
return 500;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
class NexusException extends \Exception
|
||||
{
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use App\Http\Resources\ExamUserResource;
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Repositories\ExamRepository;
|
||||
use App\Repositories\UserRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
@@ -44,9 +45,13 @@ class ExamUserController extends Controller
|
||||
'uid' => 'required',
|
||||
];
|
||||
$request->validate($rules);
|
||||
$result = $this->repository->assignToUser($request->uid, $request->exam_id, $request->begin, $request->end);
|
||||
$resource = new ExamResource($result);
|
||||
return $this->success($resource);
|
||||
$timeRange = $request->get('time_range', []);
|
||||
$begin = isset($timeRange[0]) ? Carbon::parse($timeRange[0])->toDateTimeString() : null;
|
||||
$end = isset($timeRange[1])? Carbon::parse($timeRange[1])->toDateTimeString() : null;
|
||||
|
||||
$result = $this->repository->assignToUser($request->uid, $request->exam_id, $begin, $end);
|
||||
$resource = new ExamUserResource($result);
|
||||
return $this->success($resource, 'Assign exam success!');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -114,11 +114,13 @@ class UserController extends Controller
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
public function matchExams()
|
||||
public function matchExams(Request $request)
|
||||
{
|
||||
$id = Auth::id();
|
||||
$request->validate([
|
||||
'uid' => 'required',
|
||||
]);
|
||||
$examRepository = new ExamRepository();
|
||||
$result = $examRepository->listMatchExam($id);
|
||||
$result = $examRepository->listMatchExam($request->uid);
|
||||
$resource = ExamResource::collection($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
@@ -62,5 +62,6 @@ class Kernel extends HttpKernel
|
||||
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
|
||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
|
||||
'permission' => \App\Http\Middleware\Permission::class,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\UnauthorizedException;
|
||||
|
||||
class Permission
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$user = $request->user();
|
||||
$targetClass = User::CLASS_MODERATOR;
|
||||
$log = sprintf('user: %s, class: %s, target class: %s', $user->id, $user->class, $targetClass);
|
||||
if (!$user || $user->class < $targetClass) {
|
||||
do_log("$log, denied!");
|
||||
throw new UnauthorizedException('Unauthorized!');
|
||||
}
|
||||
do_log("$log, pass!");
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace App\Models;
|
||||
|
||||
class ExamUser extends NexusModel
|
||||
{
|
||||
protected $fillable = ['exam_id', 'uid', 'status', 'progress'];
|
||||
protected $fillable = ['exam_id', 'uid', 'status', 'progress', 'begin', 'end'];
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Exceptions\NexusException;
|
||||
use App\Models\Exam;
|
||||
use App\Models\ExamProgress;
|
||||
use App\Models\ExamUser;
|
||||
@@ -166,17 +167,17 @@ class ExamRepository extends BaseRepository
|
||||
$exams = $this->listMatchExam($uid);
|
||||
if ($exams->count() > 1) {
|
||||
do_log(last_query());
|
||||
throw new \LogicException("Match exam more than 1.");
|
||||
throw new NexusException("Match exam more than 1.");
|
||||
}
|
||||
$exam = $exams->first();
|
||||
}
|
||||
if (!$exam) {
|
||||
throw new \LogicException("No valid exam.");
|
||||
throw new NexusException("No valid exam.");
|
||||
}
|
||||
$user = User::query()->findOrFail($uid);
|
||||
$exists = $user->exams()->where('exam_id', $exam->id)->exists();
|
||||
if ($exists) {
|
||||
throw new \LogicException("Exam: {$exam->id} already assign to user: {$user->id}");
|
||||
throw new NexusException("Exam: {$exam->id} already assign to user: {$user->id}");
|
||||
}
|
||||
$data = [
|
||||
'exam_id' => $exam->id,
|
||||
|
||||
Reference in New Issue
Block a user