mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-06 15:59:51 +00:00
create API for news item + testing if fetch works : to be generalized to accomodate other types of dashboard items
This commit is contained in:
parent
7bdb5bfce6
commit
1c49eb492a
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Controller;
|
||||||
|
|
||||||
|
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||||
|
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
class NewsItemApiController extends ApiController
|
||||||
|
{
|
||||||
|
protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator, $_format)
|
||||||
|
{
|
||||||
|
return $query->addOrderBy('e.startDate', 'ASC');
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,7 @@ const store = createStore({
|
|||||||
workflows: {},
|
workflows: {},
|
||||||
workflowsCc: {},
|
workflowsCc: {},
|
||||||
errorMsg: [],
|
errorMsg: [],
|
||||||
|
newsItems: {},
|
||||||
loading: false
|
loading: false
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
@ -96,12 +97,26 @@ const store = createStore({
|
|||||||
},
|
},
|
||||||
catchError(state, error) {
|
catchError(state, error) {
|
||||||
state.errorMsg.push(error);
|
state.errorMsg.push(error);
|
||||||
|
},
|
||||||
|
addNewsItems(state, newsItems) {
|
||||||
|
state.newsItems = newsItems;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
getByTab({ commit, getters }, { tab, param }) {
|
getByTab({ commit, getters }, { tab, param }) {
|
||||||
switch (tab) {
|
switch (tab) {
|
||||||
case 'MyCustoms':
|
case 'MyCustoms':
|
||||||
|
const url = `/api/1.0/main/news.json`;
|
||||||
|
makeFetch('GET', url)
|
||||||
|
.then((response) => {
|
||||||
|
console.log('news', response.results)
|
||||||
|
commit('addNewsItems', response);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
commit('catchError', error);
|
||||||
|
throw error;
|
||||||
|
})
|
||||||
|
;
|
||||||
break;
|
break;
|
||||||
// case 'MyWorks':
|
// case 'MyWorks':
|
||||||
// if (!getters.isWorksLoaded) {
|
// if (!getters.isWorksLoaded) {
|
||||||
@ -221,7 +236,7 @@ const store = createStore({
|
|||||||
default:
|
default:
|
||||||
throw 'tab '+ tab;
|
throw 'tab '+ tab;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Serializer\Normalizer;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\Center;
|
||||||
|
use Chill\MainBundle\Entity\NewsItem;
|
||||||
|
use Chill\MainBundle\Repository\CenterRepository;
|
||||||
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||||
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||||
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||||
|
|
||||||
|
class NewsItemNormalizer implements NormalizerInterface
|
||||||
|
{
|
||||||
|
public function __construct(private readonly CenterRepository $repository) {}
|
||||||
|
|
||||||
|
public function normalize($newsItem, $format = null, array $context = [])
|
||||||
|
{
|
||||||
|
/* @var NewsItem $newsItem */
|
||||||
|
return [
|
||||||
|
'id' => $newsItem->getId(),
|
||||||
|
'type' => 'news',
|
||||||
|
'title' => $newsItem->getTitle(),
|
||||||
|
'content' => $newsItem->getContent(),
|
||||||
|
'startdate' => $newsItem->getStartDate(),
|
||||||
|
'enddate' => $newsItem->getEndDate()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function supportsNormalization($data, $format = null): bool
|
||||||
|
{
|
||||||
|
return $data instanceof NewsItem && 'json' === $format;
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,12 @@ servers:
|
|||||||
|
|
||||||
components:
|
components:
|
||||||
schemas:
|
schemas:
|
||||||
|
Date:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
datetime:
|
||||||
|
type: string
|
||||||
|
format: date-time
|
||||||
User:
|
User:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
@ -131,6 +137,22 @@ components:
|
|||||||
id:
|
id:
|
||||||
type: integer
|
type: integer
|
||||||
|
|
||||||
|
News:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: integer
|
||||||
|
type:
|
||||||
|
type: string
|
||||||
|
title:
|
||||||
|
type: string
|
||||||
|
content:
|
||||||
|
type: string
|
||||||
|
startdate:
|
||||||
|
$ref: "#/components/schemas/Date"
|
||||||
|
enddate:
|
||||||
|
$ref: "#/components/schemas/Date"
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
/1.0/search.json:
|
/1.0/search.json:
|
||||||
get:
|
get:
|
||||||
@ -842,4 +864,20 @@ paths:
|
|||||||
$ref: '#/components/schemas/Workflow'
|
$ref: '#/components/schemas/Workflow'
|
||||||
403:
|
403:
|
||||||
description: "Unauthorized"
|
description: "Unauthorized"
|
||||||
|
/1.0/main/news.json:
|
||||||
|
get:
|
||||||
|
tags:
|
||||||
|
- news
|
||||||
|
summary: Returns a list of news items
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: "ok"
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
$ref: '#/components/schemas/News'
|
||||||
|
403:
|
||||||
|
description: "Unauthorized"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user