mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 12:30:49 +08:00
medal management
This commit is contained in:
103
app/Http/Controllers/MedalController.php
Normal file
103
app/Http/Controllers/MedalController.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\MedalResource;
|
||||
use App\Repositories\MedalRepository;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class MedalController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(MedalRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = MedalResource::collection($result);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('medal.admin.list.page_title'),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string',
|
||||
'price' => 'required|integer|min:1',
|
||||
'image_large' => 'required|url',
|
||||
'image_small' => 'required|url',
|
||||
'duration' => 'nullable|integer|min:-1',
|
||||
];
|
||||
$request->validate($rules);
|
||||
$result = $this->repository->store($request->all());
|
||||
$resource = new MedalResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$result = $this->repository->getDetail($id);
|
||||
$resource = new MedalResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string',
|
||||
'price' => 'required|integer|min:1',
|
||||
'image_large' => 'required|url',
|
||||
'image_small' => 'required|url',
|
||||
'duration' => 'nullable|integer|min:-1',
|
||||
];
|
||||
$request->validate($rules);
|
||||
$result = $this->repository->update($request->all(), $id);
|
||||
$resource = new MedalResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result, 'Delete medal success!');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
106
app/Http/Controllers/UserMedalController.php
Normal file
106
app/Http/Controllers/UserMedalController.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\MedalResource;
|
||||
use App\Models\UserMedal;
|
||||
use App\Repositories\MedalRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class UserMedalController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(MedalRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = MedalResource::collection($result);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('medal.admin.list.page_title'),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string',
|
||||
'price' => 'required|integer|min:1',
|
||||
'image_large' => 'required|url',
|
||||
'image_small' => 'required|url',
|
||||
'duration' => 'nullable|integer|min:-1',
|
||||
];
|
||||
$request->validate($rules);
|
||||
$result = $this->repository->store($request->all());
|
||||
$resource = new MedalResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$result = $this->repository->getDetail($id);
|
||||
$resource = new MedalResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$rules = [
|
||||
'name' => 'required|string',
|
||||
'price' => 'required|integer|min:1',
|
||||
'image_large' => 'required|url',
|
||||
'image_small' => 'required|url',
|
||||
'duration' => 'nullable|integer|min:-1',
|
||||
];
|
||||
$request->validate($rules);
|
||||
$result = $this->repository->update($request->all(), $id);
|
||||
$resource = new MedalResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$userMedal = UserMedal::query()->findOrFail($id);
|
||||
$result = $userMedal->delete();
|
||||
return $this->success($result, 'Remove user medal success!');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
31
app/Http/Resources/MedalResource.php
Normal file
31
app/Http/Resources/MedalResource.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class MedalResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'get_type' => $this->get_type,
|
||||
'get_type_text' => $this->getTypeText,
|
||||
'image_large' => $this->image_large,
|
||||
'image_small' => $this->image_small,
|
||||
'price' => $this->price,
|
||||
'duration' => $this->duration,
|
||||
'description' => $this->description,
|
||||
'expire_at' => $this->whenPivotLoaded('user_medals', function () {return $this->pivot->expire_at;}),
|
||||
'user_medal_id' => $this->whenPivotLoaded('user_medals', function () {return $this->pivot->id;}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ class UserResource extends JsonResource
|
||||
'leechtime' => $this->leechtime,
|
||||
'leechtime_text' => mkprettytime($this->leechtime),
|
||||
'inviter' => new UserResource($this->whenLoaded('inviter')),
|
||||
'valid_medals' => MedalResource::collection($this->whenLoaded('valid_medals')),
|
||||
];
|
||||
if ($request->routeIs('user.me')) {
|
||||
$out['downloaded_human'] = mksize($this->downloaded);
|
||||
|
||||
Reference in New Issue
Block a user