From 5a400fd1627349cd82eb8f3b9d26cafee6af7a48 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 8 Nov 2023 15:40:58 +0100 Subject: [PATCH] change logic of dashboard item to return user config, reinstate news items api --- .../Controller/DashboardApiController.php | 65 +++++------ .../ChillMainExtension.php | 27 ++++- .../Entity/DashboardConfigItem.php | 106 ++++++++++++++++++ .../ChillMainBundle/Entity/DashboardItem.php | 53 --------- .../ChillMainBundle/Entity/NewsItem.php | 20 ---- .../DashboardWidget/News/News.vue | 0 .../Normalizer/NewsItemNormalizer.php | 15 +-- .../ChillMainBundle/chill.api.specs.yaml | 53 ++++++--- .../migrations/Version20231108115104.php | 39 ------- ...08103723.php => Version20231108141141.php} | 15 ++- 10 files changed, 210 insertions(+), 183 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Entity/DashboardConfigItem.php delete mode 100644 src/Bundle/ChillMainBundle/Entity/DashboardItem.php create mode 100644 src/Bundle/ChillMainBundle/Resources/public/vuejs/HomepageWidget/DashboardWidget/News/News.vue delete mode 100644 src/Bundle/ChillMainBundle/migrations/Version20231108115104.php rename src/Bundle/ChillMainBundle/migrations/{Version20231108103723.php => Version20231108141141.php} (56%) diff --git a/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php b/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php index ed0f55cfd..bc045a596 100644 --- a/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/DashboardApiController.php @@ -11,55 +11,40 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Security\Core\Security; -class DashboardApiController extends ApiController +class DashboardApiController { - public function __construct(private readonly EntityManagerInterface $em) + public function __construct(private readonly Security $security) { } - /** - * Give an answer to a calendar invite. - * - * @Route("/api/1.0/main/dashboard-item/{user_id}.json", methods={"get"}) - */ - public function getDataForDashboard(int $userId): JsonResponse + public function setCrudConfig() { + return null; + } - //with the userId get the dashboard config for that user? - $user = $this->security->getUser(); - if (!$user instanceof User) { - throw new AccessDeniedHttpException('You must be an authenticated user'); - } + /** + * Give the user dashboard configuration + * + * @Route("/api/1.0/main/dashboard-config-item.json", methods={"post"}) + */ + public function getDashboardConfiguration(): JsonResponse + { + $data = [ + [ + 'position' => 'top-left', + 'id' => 1, + 'type' => 'news', + 'metadata' => [ + // arbitrary data that will be store "some time" + 'only_unread' => false, + ] + ] + ]; - $config = ['types' => ['news']]; - - //based on the user dashboard config fetch the items to be displayed - - $data = []; - - foreach ($config['types'] as $type) - { - switch ($type) { - case 'news': - $qb = $this->em->createQueryBuilder(); - $qb->select('n') - ->from(NewsItem::class) - ->where( - $qb->expr()->lt('n.endDate', ':today') - ); - - $qb->setParameter('today', new \DateTimeImmutable('now')); - - $newsItems = $qb->getQuery()->getResult(); - $data[] = $newsItems; - - break; - } - } - - return new JsonResponse($data, Response::HTTP_ACCEPTED, [], true); + return new JsonResponse($data); } } diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 4355e0f56..7e44ceb6b 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -793,10 +793,31 @@ class ChillMainExtension extends Extension implements ], ], [ - 'class' => \Chill\MainBundle\Entity\DashboardItem::class, + 'class' => \Chill\MainBundle\Entity\DashboardConfigItem::class, 'controller' => \Chill\MainBundle\Controller\DashboardApiController::class, - 'name' => 'dashboard-item', - 'base_path' => '/api/1.0/main/dashboard-item', + 'name' => 'dashboard-config-item', + 'base_path' => '/api/1.0/main/dashboard-config-item', + 'base_role' => 'ROLE_USER', + 'actions' => [ + '_index' => [ + 'methods' => [ + Request::METHOD_GET => true, + Request::METHOD_HEAD => true, + ], + ], + '_entity' => [ + 'methods' => [ + Request::METHOD_GET => true, + Request::METHOD_HEAD => true, + ], + ], + ], + ], + [ + 'class' => \Chill\MainBundle\Entity\NewsItem::class, + 'controller' => \Chill\MainBundle\Controller\NewsItemApiController::class, + 'name' => 'news-items', + 'base_path' => '/api/1.0/main/news', 'base_role' => 'ROLE_USER', 'actions' => [ '_index' => [ diff --git a/src/Bundle/ChillMainBundle/Entity/DashboardConfigItem.php b/src/Bundle/ChillMainBundle/Entity/DashboardConfigItem.php new file mode 100644 index 000000000..208c864e7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Entity/DashboardConfigItem.php @@ -0,0 +1,106 @@ +id; + } + + public function getType(): string + { + return $this->type; + } + + public function setType(string $type): self + { + $this->type = $type; + + return $this; + } + + public function getPosition(): string + { + return $this->position; + } + + public function setPosition(string $position): void + { + $this->position = $position; + } + + public function getUser(): User + { + return $this->user; + } + + public function setUser(User $user): void + { + $this->user = $user; + } + + public function getMetadata(): array + { + return $this->metadata; + } + + public function setMetadata(array $metadata): void + { + $this->metadata = $metadata; + } +} diff --git a/src/Bundle/ChillMainBundle/Entity/DashboardItem.php b/src/Bundle/ChillMainBundle/Entity/DashboardItem.php deleted file mode 100644 index d849407bc..000000000 --- a/src/Bundle/ChillMainBundle/Entity/DashboardItem.php +++ /dev/null @@ -1,53 +0,0 @@ -id; - } - - public function getType(): string - { - return $this->type; - } - - public function setType(string $type): self - { - $this->type = $type; - - return $this; - } -} diff --git a/src/Bundle/ChillMainBundle/Entity/NewsItem.php b/src/Bundle/ChillMainBundle/Entity/NewsItem.php index c58acf48e..4febb9d98 100644 --- a/src/Bundle/ChillMainBundle/Entity/NewsItem.php +++ b/src/Bundle/ChillMainBundle/Entity/NewsItem.php @@ -57,15 +57,6 @@ class NewsItem implements TrackCreationInterface, TrackUpdateInterface */ private string $content = ''; - /** - * @ORM\OneToOne (targetEntity="DashboardItem", inversedBy="newsItem") - * - * @groups({"write", "read"}) - * - * @Assert\NotNull - */ - private ?DashboardItem $dashboardItem = null; - /** * @ORM\Column(type="date_immutable", nullable=false) */ @@ -193,15 +184,4 @@ class NewsItem implements TrackCreationInterface, TrackUpdateInterface return $this->id; } - public function getType(): string - { - return $this->type; - } - - public function setType(string $type): self - { - $this->type = $type; - - return $this; - } } diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/HomepageWidget/DashboardWidget/News/News.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/HomepageWidget/DashboardWidget/News/News.vue new file mode 100644 index 000000000..e69de29bb diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php index 306e8d182..fd3c510b7 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/NewsItemNormalizer.php @@ -24,18 +24,15 @@ class NewsItemNormalizer implements NormalizerInterface { public function __construct(private readonly CenterRepository $repository) {} - public function normalize($dashboardItem, $format = null, array $context = []) + public function normalize($newsItem, $format = null, array $context = []) { - /* @var DashboardItem $dashboardItem */ + /* @var NewsItem $newsItem */ return [ 'id' => $newsItem->getId(), - 'type' => $dashboardItem->getType(), - 'metadata' => [ - 'title' => $newsItem->getTitle(), - 'content' => $newsItem->getContent(), - 'startdate' => $newsItem->getStartDate(), - 'enddate' => $newsItem->getEndDate() - ], + 'title' => $newsItem->getTitle(), + 'content' => $newsItem->getContent(), + 'startdate' => $newsItem->getStartDate(), + 'enddate' => $newsItem->getEndDate() ]; } diff --git a/src/Bundle/ChillMainBundle/chill.api.specs.yaml b/src/Bundle/ChillMainBundle/chill.api.specs.yaml index 936712a11..8edbd93f6 100644 --- a/src/Bundle/ChillMainBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillMainBundle/chill.api.specs.yaml @@ -137,7 +137,7 @@ components: id: type: integer - DashboardItem: + DashboardConfigItem: type: object properties: id: @@ -146,6 +146,24 @@ components: type: string metadata: type: object + userId: + type: integer + position: + type: string + + NewsItem: + type: object + properties: + id: + type: integer + title: + type: string + content: + type: string + startDate: + $ref: "#/components/schemas/Date" + endDate: + $ref: "#/components/schemas/Date" paths: @@ -859,20 +877,26 @@ paths: $ref: '#/components/schemas/Workflow' 403: description: "Unauthorized" - /1.0/main/dashboard-item/{user_id}.json: + /1.0/main/dashboard-config-item.json: get: tags: - - dashboard item - summary: Returns a list of dashboard items for the user in question - parameters: - - name: user_id - in: path - required: true - description: The user id - schema: - type: integer - format: integer - minimum: 1 + - dashboard config item + summary: Returns the dashboard configuration for the current user. + responses: + 200: + description: "ok" + content: + application/json: + schema: + $ref: '#/components/schemas/DashboardConfigItem' + 403: + description: "Unauthorized" + + /1.0/main/news.json: + get: + tags: + - news items + summary: Returns a list of news items responses: 200: description: "ok" @@ -881,7 +905,6 @@ paths: schema: type: array items: - $ref: '#/components/schemas/DashboardItem' + $ref: '#/components/schemas/NewsItem' 403: description: "Unauthorized" - diff --git a/src/Bundle/ChillMainBundle/migrations/Version20231108115104.php b/src/Bundle/ChillMainBundle/migrations/Version20231108115104.php deleted file mode 100644 index d33001598..000000000 --- a/src/Bundle/ChillMainBundle/migrations/Version20231108115104.php +++ /dev/null @@ -1,39 +0,0 @@ -addSql('CREATE SEQUENCE chill_main_dashboard_item_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); - $this->addSql('CREATE TABLE chill_main_dashboard_item (id INT NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(id))'); - $this->addSql('ALTER TABLE chill_main_news ADD dashboardItem_id INT DEFAULT NULL'); - $this->addSql('ALTER TABLE chill_main_news DROP type'); - $this->addSql('ALTER TABLE chill_main_news ADD CONSTRAINT FK_96922AFBCBDA857A FOREIGN KEY (dashboardItem_id) REFERENCES chill_main_dashboard_item (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); - $this->addSql('CREATE UNIQUE INDEX UNIQ_96922AFBCBDA857A ON chill_main_news (dashboardItem_id)'); - $this->addSql('ALTER TABLE chill_main_notification ALTER addressesemails DROP DEFAULT'); - } - - public function down(Schema $schema): void - { - $this->addSql('ALTER TABLE chill_main_news DROP CONSTRAINT FK_96922AFBCBDA857A'); - $this->addSql('DROP SEQUENCE chill_main_dashboard_item_id_seq CASCADE'); - $this->addSql('DROP TABLE chill_main_dashboard_item'); - $this->addSql('ALTER TABLE chill_main_news ADD type VARCHAR(255) NOT NULL'); - $this->addSql('ALTER TABLE chill_main_news DROP dashboardItem_id'); - } -} diff --git a/src/Bundle/ChillMainBundle/migrations/Version20231108103723.php b/src/Bundle/ChillMainBundle/migrations/Version20231108141141.php similarity index 56% rename from src/Bundle/ChillMainBundle/migrations/Version20231108103723.php rename to src/Bundle/ChillMainBundle/migrations/Version20231108141141.php index f2f924bc3..ffcc6130b 100644 --- a/src/Bundle/ChillMainBundle/migrations/Version20231108103723.php +++ b/src/Bundle/ChillMainBundle/migrations/Version20231108141141.php @@ -8,34 +8,41 @@ use Doctrine\DBAL\Schema\Schema; use Doctrine\Migrations\AbstractMigration; /** - * Create news item entity + * Create dashboard config item and news item */ -final class Version20231108103723 extends AbstractMigration +final class Version20231108141141 extends AbstractMigration { public function getDescription(): string { - return 'Create news item entity'; + return 'Create dashboard config item and news item'; } public function up(Schema $schema): void { + $this->addSql('CREATE SEQUENCE chill_main_dashboard_config_item_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); $this->addSql('CREATE SEQUENCE chill_main_news_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); - $this->addSql('CREATE TABLE chill_main_news (id INT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, type VARCHAR(255) NOT NULL, startDate DATE NOT NULL, endDate DATE DEFAULT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, createdBy_id INT DEFAULT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE TABLE chill_main_dashboard_config_item (id INT NOT NULL, user_id INT DEFAULT NULL, type VARCHAR(255) NOT NULL, position VARCHAR(255) NOT NULL, metadata JSON NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_CF59DFD6A76ED395 ON chill_main_dashboard_config_item (user_id)'); + $this->addSql('CREATE TABLE chill_main_news (id INT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL, startDate DATE NOT NULL, endDate DATE DEFAULT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, createdBy_id INT DEFAULT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))'); $this->addSql('CREATE INDEX IDX_96922AFB3174800F ON chill_main_news (createdBy_id)'); $this->addSql('CREATE INDEX IDX_96922AFB65FF1AEC ON chill_main_news (updatedBy_id)'); $this->addSql('COMMENT ON COLUMN chill_main_news.startDate IS \'(DC2Type:date_immutable)\''); $this->addSql('COMMENT ON COLUMN chill_main_news.endDate IS \'(DC2Type:date_immutable)\''); $this->addSql('COMMENT ON COLUMN chill_main_news.createdAt IS \'(DC2Type:datetime_immutable)\''); $this->addSql('COMMENT ON COLUMN chill_main_news.updatedAt IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('ALTER TABLE chill_main_dashboard_config_item ADD CONSTRAINT FK_CF59DFD6A76ED395 FOREIGN KEY (user_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE chill_main_news ADD CONSTRAINT FK_96922AFB3174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE chill_main_news ADD CONSTRAINT FK_96922AFB65FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); } public function down(Schema $schema): void { + $this->addSql('DROP SEQUENCE chill_main_dashboard_config_item_id_seq CASCADE'); $this->addSql('DROP SEQUENCE chill_main_news_id_seq CASCADE'); + $this->addSql('ALTER TABLE chill_main_dashboard_config_item DROP CONSTRAINT FK_CF59DFD6A76ED395'); $this->addSql('ALTER TABLE chill_main_news DROP CONSTRAINT FK_96922AFB3174800F'); $this->addSql('ALTER TABLE chill_main_news DROP CONSTRAINT FK_96922AFB65FF1AEC'); + $this->addSql('DROP TABLE chill_main_dashboard_config_item'); $this->addSql('DROP TABLE chill_main_news'); } }