Skip to content

Commit 867a10f

Browse files
committed
Add User.preferences column and hidden-workers endpoint
1 parent fdb12c3 commit 867a10f

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20260501124430 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE user ADD preferences JSON DEFAULT NULL COMMENT \'(DC2Type:json)\'');
24+
}
25+
26+
public function down(Schema $schema): void
27+
{
28+
// this down() migration is auto-generated, please modify it to your needs
29+
$this->addSql('ALTER TABLE user DROP preferences');
30+
}
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use App\Entity\User;
6+
use Doctrine\ORM\EntityManagerInterface;
7+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8+
use Symfony\Component\HttpFoundation\JsonResponse;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\Response;
11+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12+
use Symfony\Component\Routing\Attribute\Route;
13+
use Symfony\Component\Security\Http\Attribute\IsGranted;
14+
15+
#[Route('/admin/user/preferences')]
16+
#[IsGranted('IS_AUTHENTICATED_FULLY')]
17+
class UserPreferenceController extends AbstractController
18+
{
19+
public function __construct(
20+
private readonly EntityManagerInterface $entityManager,
21+
) {
22+
}
23+
24+
#[Route('/hidden-workers', name: 'app_user_preferences_hidden_workers', methods: ['PATCH'])]
25+
public function setHiddenWorkers(Request $request): Response
26+
{
27+
/** @var User $user */
28+
$user = $this->getUser();
29+
30+
$payload = json_decode((string) $request->getContent(), true);
31+
32+
if (!is_array($payload) || !array_key_exists('hiddenWorkers', $payload)) {
33+
throw new BadRequestHttpException('Body must be JSON {"hiddenWorkers": [...]}.');
34+
}
35+
36+
$hiddenWorkers = $payload['hiddenWorkers'];
37+
if (!is_array($hiddenWorkers)) {
38+
throw new BadRequestHttpException('"hiddenWorkers" must be an array of strings.');
39+
}
40+
41+
$user->setHiddenWorkers(array_values(array_filter($hiddenWorkers, 'is_string')));
42+
43+
$this->entityManager->flush();
44+
45+
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
46+
}
47+
}

src/Entity/User.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class User implements UserInterface
2323
#[ORM\Column(length: 255)]
2424
private ?string $name = null;
2525

26+
/**
27+
* @var array<string, mixed>|null
28+
*/
29+
#[ORM\Column(type: 'json', nullable: true)]
30+
private ?array $preferences = null;
31+
2632
public function __construct()
2733
{
2834
}
@@ -91,4 +97,44 @@ public function setName(string $name): self
9197

9298
return $this;
9399
}
100+
101+
/**
102+
* @return array<string, mixed>
103+
*/
104+
public function getPreferences(): array
105+
{
106+
return $this->preferences ?? [];
107+
}
108+
109+
/**
110+
* @param array<string, mixed> $preferences
111+
*/
112+
public function setPreferences(array $preferences): self
113+
{
114+
$this->preferences = $preferences;
115+
116+
return $this;
117+
}
118+
119+
/**
120+
* @return string[]
121+
*/
122+
public function getHiddenWorkers(): array
123+
{
124+
$hidden = $this->getPreferences()['hiddenWorkers'] ?? [];
125+
126+
return is_array($hidden) ? array_values(array_filter($hidden, 'is_string')) : [];
127+
}
128+
129+
/**
130+
* @param string[] $hiddenWorkers
131+
*/
132+
public function setHiddenWorkers(array $hiddenWorkers): self
133+
{
134+
$preferences = $this->getPreferences();
135+
$preferences['hiddenWorkers'] = array_values(array_unique(array_filter($hiddenWorkers, 'is_string')));
136+
$this->preferences = $preferences;
137+
138+
return $this;
139+
}
94140
}

0 commit comments

Comments
 (0)