api point for dashboard items

This commit is contained in:
Julie Lenaerts 2023-11-08 12:17:34 +01:00
parent 312a43c093
commit b172ebdf76
3 changed files with 84 additions and 19 deletions

View File

@ -0,0 +1,65 @@
<?php
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\NewsItem;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
class DashboardApiController extends ApiController
{
public function __construct(private readonly EntityManagerInterface $em)
{
}
/**
* 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
{
//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');
}
$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

@ -792,11 +792,11 @@ class ChillMainExtension extends Extension implements
], ],
], ],
], ],
[ /* [
'class' => \Chill\MainBundle\Entity\NewsItem::class, 'class' => \Chill\MainBundle\Entity\DashboardItem::class,
'controller' => \Chill\MainBundle\Controller\NewsItemApiController::class, 'controller' => \Chill\MainBundle\Controller\DashboardApiController::class,
'name' => 'news-item', 'name' => 'news-item',
'base_path' => '/api/1.0/main/news', 'base_path' => '/api/1.0/main/dashboard-item',
'base_role' => 'ROLE_USER', 'base_role' => 'ROLE_USER',
'actions' => [ 'actions' => [
'_index' => [ '_index' => [
@ -812,7 +812,7 @@ class ChillMainExtension extends Extension implements
], ],
], ],
], ],
], ],*/
], ],
]); ]);
} }

View File

@ -137,7 +137,7 @@ components:
id: id:
type: integer type: integer
News: DashboardItem:
type: object type: object
properties: properties:
id: id:
@ -146,15 +146,6 @@ components:
type: string type: string
metadata: metadata:
type: object type: object
properties:
title:
type: string
content:
type: string
startdate:
$ref: "#/components/schemas/Date"
enddate:
$ref: "#/components/schemas/Date"
paths: paths:
@ -868,11 +859,20 @@ paths:
$ref: '#/components/schemas/Workflow' $ref: '#/components/schemas/Workflow'
403: 403:
description: "Unauthorized" description: "Unauthorized"
/1.0/main/news.json: /1.0/main/dashboard-item/{user_id}.json:
get: get:
tags: tags:
- news - dashboard item
summary: Returns a list of news items 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
responses: responses:
200: 200:
description: "ok" description: "ok"
@ -881,7 +881,7 @@ paths:
schema: schema:
type: array type: array
items: items:
$ref: '#/components/schemas/News' $ref: '#/components/schemas/DashboardItem'
403: 403:
description: "Unauthorized" description: "Unauthorized"