You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.1 KiB

<?php
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreSignupChannelRequest;
use App\Http\Requests\Admin\UpdateSignupChannelRequest;
use App\Models\Competition;
use App\Models\SignupChannel;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
class SignupChannelController extends Controller
{
public function index(Competition $competition): JsonResponse
{
$channels = $competition->signupChannels()
->orderByDesc('id')
->get()
->map(fn (SignupChannel $channel) => $this->toSummary($channel, $competition))
->values();
return response()->json([
'data' => $channels,
]);
}
public function store(StoreSignupChannelRequest $request, Competition $competition): JsonResponse
{
/** @var SignupChannel $channel */
$channel = $competition->signupChannels()->create($request->validated());
return response()->json($this->toDetail($channel, $competition), 201);
}
public function show(Competition $competition, SignupChannel $signupChannel): JsonResponse
{
$this->assertChannelBelongs($competition, $signupChannel);
return response()->json($this->toDetail($signupChannel, $competition));
}
public function update(
UpdateSignupChannelRequest $request,
Competition $competition,
SignupChannel $signupChannel
): JsonResponse {
$this->assertChannelBelongs($competition, $signupChannel);
$signupChannel->fill($request->validated());
$signupChannel->save();
return response()->json($this->toDetail($signupChannel, $competition));
}
public function destroy(Competition $competition, SignupChannel $signupChannel): Response
{
$this->assertChannelBelongs($competition, $signupChannel);
$signupChannel->delete();
return response()->noContent();
}
/**
* @return array<string, mixed>
*/
private function toSummary(SignupChannel $channel, Competition $competition): array
{
return [
'id' => $channel->id,
'competition_id' => $channel->competition_id,
'competition_name' => $competition->name,
'channel_code' => $channel->channel_code,
'channel_name' => $channel->channel_name,
'status' => $channel->status,
'success_callback_url' => $channel->success_callback_url,
'remark' => $channel->remark,
'created_at' => $channel->created_at?->toIso8601String(),
'updated_at' => $channel->updated_at?->toIso8601String(),
];
}
/**
* @return array<string, mixed>
*/
private function toDetail(SignupChannel $channel, Competition $competition): array
{
$row = $this->toSummary($channel, $competition);
$row['shared_secret'] = $channel->shared_secret;
return $row;
}
private function assertChannelBelongs(Competition $competition, SignupChannel $channel): void
{
if ((int) $channel->competition_id !== (int) $competition->id) {
abort(404);
}
}
}