1
0
Fork 0
party-rank-server/app/Http/Controllers/Api/UserScoreController.php

73 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\UserScoreResource;
use App\Admin\Services\UserScoreService;
use App\Enums\CheckStatus;
class UserScoreController extends Controller
{
public function index(Request $request)
{
$user = auth('api')->user();
$list = $user->scores()->filter($request->all())->sort()->paginate($request->input('per_page'));
return $this->response()->success(UserScoreResource::collection($list));
}
public function show($id)
{
$user = auth('api')->user();
$info = $user->scores()->with(['type', 'checkUser'])->findOrFail($id);
return $this->response()->success(UserScoreResource::make($info));
}
public function store(Request $request)
{
$user = auth('api')->user();
$params = $request->all();
$params['user_id'] = $user->id;
$service = UserScoreService::make();
$result = $service->store($params);
if ($result) {
return $this->response()->success();
}
return $this->response()->fail($service->getError());
}
public function update($id, Request $request)
{
$user = auth('api')->user();
$info = $user->scores()->findOrFail($id);
if ($info->check_status == CheckStatus::Success) {
return $this->response()->fail('审核已通过, 无法修改');
}
$params = $request->only(['title', 'content', 'images', 'file']);
if ($info->check_status == CheckStatus::Fail) {
$params['created_at'] = now();
$params['check_status'] = CheckStatus::None;
$params['check_at'] = null;
$params['check_user_id'] = null;
}
$info->update($params);
return $this->response()->success();
}
public function destroy($id)
{
$user = auth('api')->user();
$info = $user->scores()->findOrFail($id);
if ($info->check_status == CheckStatus::Success) {
return $this->response()->fail('审核已通过, 无法删除');
}
UserScoreService::make()->delete($id);
return $this->response()->success();
}
}