change logic of dashboard item to return user config, reinstate news items api

This commit is contained in:
Julie Lenaerts 2023-11-08 15:40:58 +01:00
parent 01a5c291e0
commit 5a400fd162
10 changed files with 210 additions and 183 deletions

View File

@ -11,55 +11,40 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route; 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)
{ {
} }
public function setCrudConfig()
{
return null;
}
/** /**
* Give an answer to a calendar invite. * Give the user dashboard configuration
* *
* @Route("/api/1.0/main/dashboard-item/{user_id}.json", methods={"get"}) * @Route("/api/1.0/main/dashboard-config-item.json", methods={"post"})
*/ */
public function getDataForDashboard(int $userId): JsonResponse public function getDashboardConfiguration(): JsonResponse
{ {
$data = [
[
'position' => 'top-left',
'id' => 1,
'type' => 'news',
'metadata' => [
// arbitrary data that will be store "some time"
'only_unread' => false,
]
]
];
//with the userId get the dashboard config for that user? return new JsonResponse($data);
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException('You must be an authenticated user');
}
$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);
} }
} }

View File

@ -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, 'controller' => \Chill\MainBundle\Controller\DashboardApiController::class,
'name' => 'dashboard-item', 'name' => 'dashboard-config-item',
'base_path' => '/api/1.0/main/dashboard-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', 'base_role' => 'ROLE_USER',
'actions' => [ 'actions' => [
'_index' => [ '_index' => [

View File

@ -0,0 +1,106 @@
<?php
namespace Chill\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*
* @ORM\Table(name="chill_main_dashboard_config_item")
*/
class DashboardConfigItem
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*
* @Serializer\Groups({"dashboardConfigItem:read", "read"})
*/
private ?int $id;
/**
* @ORM\Column(type="string")
*
* @Serializer\Groups({"dashboardConfigItem:read", "read"})
*
* @Assert\NotNull
*/
private string $type = '';
/**
* @ORM\Column(type="string")
*
* @Serializer\Groups({"dashboardConfigItem:read", "read"})
*
* @Assert\NotNull
*/
private string $position = '';
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private User $user;
/**
*
* @ORM\Column(type="json")
*
* @Serializer\Groups({"dashboardConfigItem:read"})
*
*/
private array $metadata = [];
public function getId(): ?int
{
return $this->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;
}
}

View File

@ -1,53 +0,0 @@
<?php
namespace Chill\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
*
* @ORM\Table(name="chill_main_dashboard_item")
*/
class DashboardItem
{
/**
* @ORM\Id
*
* @ORM\GeneratedValue
*
* @ORM\Column(type="integer")
*
* @Serializer\Groups({"dashboardItem:read", "read"})
*/
private ?int $id;
/**
* @ORM\Column(type="string")
*
* @Serializer\Groups({"dashboardItem:read", "read"})
*
* @Assert\NotNull
*/
private string $type = '';
public function getId(): ?int
{
return $this->id;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
}

View File

@ -57,15 +57,6 @@ class NewsItem implements TrackCreationInterface, TrackUpdateInterface
*/ */
private string $content = ''; 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) * @ORM\Column(type="date_immutable", nullable=false)
*/ */
@ -193,15 +184,4 @@ class NewsItem implements TrackCreationInterface, TrackUpdateInterface
return $this->id; return $this->id;
} }
public function getType(): string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
} }

View File

@ -24,18 +24,15 @@ class NewsItemNormalizer implements NormalizerInterface
{ {
public function __construct(private readonly CenterRepository $repository) {} 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 [ return [
'id' => $newsItem->getId(), 'id' => $newsItem->getId(),
'type' => $dashboardItem->getType(),
'metadata' => [
'title' => $newsItem->getTitle(), 'title' => $newsItem->getTitle(),
'content' => $newsItem->getContent(), 'content' => $newsItem->getContent(),
'startdate' => $newsItem->getStartDate(), 'startdate' => $newsItem->getStartDate(),
'enddate' => $newsItem->getEndDate() 'enddate' => $newsItem->getEndDate()
],
]; ];
} }

View File

@ -137,7 +137,7 @@ components:
id: id:
type: integer type: integer
DashboardItem: DashboardConfigItem:
type: object type: object
properties: properties:
id: id:
@ -146,6 +146,24 @@ components:
type: string type: string
metadata: metadata:
type: object 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: paths:
@ -859,20 +877,26 @@ paths:
$ref: '#/components/schemas/Workflow' $ref: '#/components/schemas/Workflow'
403: 403:
description: "Unauthorized" description: "Unauthorized"
/1.0/main/dashboard-item/{user_id}.json: /1.0/main/dashboard-config-item.json:
get: get:
tags: tags:
- dashboard item - dashboard config item
summary: Returns a list of dashboard items for the user in question summary: Returns the dashboard configuration for the current user.
parameters: responses:
- name: user_id 200:
in: path description: "ok"
required: true content:
description: The user id application/json:
schema: schema:
type: integer $ref: '#/components/schemas/DashboardConfigItem'
format: integer 403:
minimum: 1 description: "Unauthorized"
/1.0/main/news.json:
get:
tags:
- news items
summary: Returns a list of news items
responses: responses:
200: 200:
description: "ok" description: "ok"
@ -881,7 +905,6 @@ paths:
schema: schema:
type: array type: array
items: items:
$ref: '#/components/schemas/DashboardItem' $ref: '#/components/schemas/NewsItem'
403: 403:
description: "Unauthorized" description: "Unauthorized"

View File

@ -1,39 +0,0 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Create dashboard item entity and link to news item
*/
final class Version20231108115104 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create dashboard item entity and link to news item';
}
public function up(Schema $schema): void
{
$this->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');
}
}

View File

@ -8,34 +8,41 @@ use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration; 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 public function getDescription(): string
{ {
return 'Create news item entity'; return 'Create dashboard config item and news item';
} }
public function up(Schema $schema): void 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 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_96922AFB3174800F ON chill_main_news (createdBy_id)');
$this->addSql('CREATE INDEX IDX_96922AFB65FF1AEC ON chill_main_news (updatedBy_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.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.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.createdAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_main_news.updatedAt 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_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'); $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 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('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_96922AFB3174800F');
$this->addSql('ALTER TABLE chill_main_news DROP CONSTRAINT FK_96922AFB65FF1AEC'); $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'); $this->addSql('DROP TABLE chill_main_news');
} }
} }