Ajout endpoint API /projects + fixtures Project

This commit is contained in:
2020-02-19 11:12:13 +01:00
parent da5583c797
commit 9b46dab404
4 changed files with 99 additions and 11 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace App\Controller;
use App\Entity\Project;
use App\Http\DataResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
class ProjectController extends Controller
{
/**
* @Route("/api/v1/projects", name="api_v1_list_projects")
*/
public function listProjects()
{
/** @var array */
$projects = $this->getDoctrine()
->getRepository(Project::class)
->findAll()
;
$results = [];
foreach($projects as $p) {
$project = [
'id' => $p->getId(),
'name' => $p->getName(),
];
$project['users'] = $p->getUsers()->map(function($user) {
return ['id' => $user->getId(), 'username' => $user->getUsername()];
})->toArray();
$project['requests'] = $p->getRequests()->map(function($req) {
return ['id' => $req->getId()];
})->toArray();
$results[] = $project;
}
return new DataResponse([
'projects' => $results,
]);
}
}