feat(story-2.2+2.3): device setup page, account linking, naming & configuration
Story 2.2 — /setup/{mac} Twig page (no Vue, works JS-disabled):
- Register tab: creates account + logs in + links device → /setup/{mac}/configure
- Login tab: manual credential check via UserPasswordHasherInterface + Security::login()
+ links device → /setup/{mac}/configure
- Re-provisioning: DeviceService.linkToUser() atomically transfers ownership + stubs
purgeDeviceHistory() (completed in Epic 3 when Image/Approval entities exist)
Story 2.3 — /setup/{mac}/configure (requires auth):
- GET: device name, orientation (landscape/portrait), rotation interval (6/12/24/48/168h),
uniqueness window (5/10/20/50 cycles)
- POST: validates name, saves to Device entity, redirects to Vue SPA
- Device entity: mac, name, orientation (Orientation enum), rotationIntervalHours,
uniquenessWindow, user (ManyToOne), linkedAt
- PATCH /api/devices/{id}: Vue SPA can edit any device field (Story 2.3 "edit from app")
- GET /api/devices: list authenticated user's devices
- Migration: create device table with Orientation enum column
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Device;
|
||||
use App\Entity\User;
|
||||
use App\Enum\Orientation;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[Route('/api/devices')]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
class DeviceApiController extends AbstractController
|
||||
{
|
||||
#[Route('', name: 'api_devices_list', methods: ['GET'])]
|
||||
public function list(EntityManagerInterface $em): JsonResponse
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$devices = $em->getRepository(Device::class)->findBy(['user' => $user]);
|
||||
|
||||
return $this->json(array_map($this->serialize(...), $devices));
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_device_update', methods: ['PATCH'])]
|
||||
public function update(int $id, Request $request, EntityManagerInterface $em): JsonResponse
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $this->getUser();
|
||||
$device = $em->getRepository(Device::class)->findOneBy(['id' => $id, 'user' => $user]);
|
||||
|
||||
if (!$device) {
|
||||
return $this->json(['error' => 'Device not found'], Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
$body = json_decode($request->getContent(), true) ?? [];
|
||||
|
||||
if (isset($body['name'])) {
|
||||
$name = trim((string) $body['name']);
|
||||
if (empty($name)) {
|
||||
return $this->json(['error' => 'Name cannot be empty'], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
$device->setName($name);
|
||||
}
|
||||
|
||||
if (isset($body['orientation'])) {
|
||||
$orientation = Orientation::tryFrom($body['orientation']);
|
||||
if (!$orientation) {
|
||||
return $this->json(['error' => 'Invalid orientation'], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
$device->setOrientation($orientation);
|
||||
}
|
||||
|
||||
if (isset($body['rotationIntervalHours'])) {
|
||||
$device->setRotationIntervalHours(max(1, (int) $body['rotationIntervalHours']));
|
||||
}
|
||||
|
||||
if (isset($body['uniquenessWindow'])) {
|
||||
$device->setUniquenessWindow(max(1, (int) $body['uniquenessWindow']));
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
||||
return $this->json($this->serialize($device));
|
||||
}
|
||||
|
||||
private function serialize(Device $d): array
|
||||
{
|
||||
return [
|
||||
'id' => $d->getId(),
|
||||
'mac' => $d->getMac(),
|
||||
'name' => $d->getName(),
|
||||
'orientation' => $d->getOrientation()->value,
|
||||
'rotationIntervalHours' => $d->getRotationIntervalHours(),
|
||||
'uniquenessWindow' => $d->getUniquenessWindow(),
|
||||
'linkedAt' => $d->getLinkedAt()->format(\DateTimeInterface::ATOM),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user