Ajout endpoint API /projects + fixtures Project

This commit is contained in:
wpetit 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,
]);
}
}

View File

@ -12,7 +12,9 @@ class RequestController
*/
public function listRequests()
{
return new DataResponse([]);
return new DataResponse([
]);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\DataFixtures;
use App\Entity\Project;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class ProjectFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager)
{
$dev1 = $manager
->getRepository(User::class)
->findOneByUsername('dev1')
;
$project1 = new Project();
$project1->setName("Test");
$project1->addUser($dev1);
$manager->persist($project1);
$manager->flush();
}
public function getDependencies()
{
return [
UserFixtures::class,
];
}
}

View File

@ -19,12 +19,12 @@ class Project
private $id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Request", mappedBy="project", orphanRemoval=true)
* @ORM\OneToMany(targetEntity="App\Entity\Request", mappedBy="project", orphanRemoval=true, fetch="LAZY")
*/
private $request;
private $requests;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="projects")
* @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="projects", fetch="LAZY")
*/
private $users;
@ -35,7 +35,7 @@ class Project
public function __construct()
{
$this->request = new ArrayCollection();
$this->requests = new ArrayCollection();
$this->users = new ArrayCollection();
}
@ -47,15 +47,15 @@ class Project
/**
* @return Collection|Request[]
*/
public function getRequest(): Collection
public function getRequests(): Collection
{
return $this->request;
return $this->requests;
}
public function addRequest(Request $request): self
{
if (!$this->request->contains($request)) {
$this->request[] = $request;
if (!$this->requests->contains($request)) {
$this->requests[] = $request;
$request->setProject($this);
}
@ -64,8 +64,8 @@ class Project
public function removeRequest(Request $request): self
{
if ($this->request->contains($request)) {
$this->request->removeElement($request);
if ($this->requests->contains($request)) {
$this->requests->removeElement($request);
// set the owning side to null (unless already changed)
if ($request->getProject() === $this) {
$request->setProject(null);