exam-index

This commit is contained in:
xiaomlove
2021-04-20 20:18:02 +08:00
parent 38afa63e3d
commit 15bd8e800b
6 changed files with 80 additions and 4 deletions

View File

@@ -95,4 +95,10 @@ class ExamController extends Controller
//
}
public function indexes()
{
$result = $this->repository->listIndexes();
return $this->success($result);
}
}

View File

@@ -3,11 +3,38 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Exam extends NexusModel
{
use HasFactory;
protected $fillable = ['name', 'description', 'begin', 'end', 'status'];
const STATUS_ENABLED = 0;
const STATUS_DISABLED = 1;
public static $status = [
self::STATUS_ENABLED => ['text' => '启用中'],
self::STATUS_DISABLED => ['text' => '已禁用'],
];
const INDEX_UPLOADED = 1;
const INDEX_SEED_TIME = 2;
const INDEX_DOWNLOADED = 3;
const INDEX_LEECH_TIME = 4;
const INDEX_BONUS = 5;
public static $indexes = [
self::INDEX_UPLOADED => ['text' => 'Uploaded'],
self::INDEX_SEED_TIME => ['text' => 'Seed time'],
self::INDEX_DOWNLOADED => ['text' => 'Download'],
self::INDEX_LEECH_TIME => ['text' => 'Leech time'],
self::INDEX_BONUS => ['text' => 'Bonus'],
];
public function getStatusTextAttribute()
{
return self::$status[$this->status]['text'] ?? '';
}
}

View File

@@ -78,7 +78,7 @@ class User extends Authenticatable
*
* @var array
*/
protected $fillable = ['username', 'email', 'password', 'secret', 'editsecret', 'added'];
protected $fillable = ['username', 'email', 'passhash', 'secret', 'editsecret', 'added'];
/**
* The attributes that should be hidden for arrays.
@@ -96,6 +96,6 @@ class User extends Authenticatable
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}

View File

@@ -4,6 +4,8 @@ namespace App\Repositories;
use App\Models\Exam;
use App\Models\Setting;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class ExamRepository extends BaseRepository
{
@@ -28,4 +30,45 @@ class ExamRepository extends BaseRepository
return $exam;
}
public function listIndexes()
{
$out = [];
foreach(Exam::$indexes as $key => $value) {
$out[$key] = $value['text'];
}
return $out;
}
public function listMatchExam($uid)
{
$now = Carbon::now();
$user = User::query()->findOrFail($uid, ['id', 'username', 'added', 'class']);
$exams = Exam::query()
->where('begin', '<=', $now)
->where('end', '>=', $now)
->where('status', Exam::STATUS_ENABLED)
->get();
$result = [];
$logPrefix = "uid: $uid";
foreach ($exams as $exam) {
$filters = $exam->filters;
if (!in_array($user->class, $filters['classes'])) {
Log::info("$logPrefix, class: {$user->class} not in: " . json_encode($filters));
continue;
}
$added = $user->added->toDateTimeString();
if (!empty($filters['register_time_begin']) && $added < $filters['register_time_begin']) {
Log::info("$logPrefix, added: $added not after: " . $filters['register_time_begin']);
continue;
}
if (!empty($filters['register_time_end']) && $added > $filters['register_time_end']) {
Log::info("$logPrefix, added: $added not before: " . $filters['register_time_end']);
continue;
}
$result[] = $exam;
}
return $result;
}
}

View File

@@ -440,7 +440,6 @@ function api(...$args)
$msg = $args[1];
$data = $args[2];
}
if (defined('LARAVEL_START')) {
$start = LARAVEL_START;
if ($data instanceof \Illuminate\Http\Resources\Json\ResourceCollection || $data instanceof \Illuminate\Http\Resources\Json\JsonResource) {

View File

@@ -18,5 +18,6 @@ Route::group([], function () {
Route::resource('agent-allow', \App\Http\Controllers\AgentAllowController::class);
Route::resource('user', \App\Http\Controllers\UserController::class);
Route::resource('exam', \App\Http\Controllers\ExamController::class);
Route::get('exam-index', [\App\Http\Controllers\ExamController::class, 'indexes']);
Route::get('class', [\App\Http\Controllers\UserController::class, 'classes']);
});