Ajout endpoint création de projet
This commit is contained in:
parent
3510e66882
commit
dacf0a8aec
|
@ -2,15 +2,20 @@
|
|||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Project;
|
||||
use App\Entity\User;
|
||||
use App\Http\DataResponse;
|
||||
use App\Http\ErrorResponse;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @Route("/api/v1/projects", name="api_v1_list_projects")
|
||||
* @Route("/api/v1/projects", name="api_v1_list_projects", methods={"GET"})
|
||||
* @IsGranted("ROLE_DEVELOPER")
|
||||
*/
|
||||
public function listProjects()
|
||||
{
|
||||
|
@ -22,21 +27,7 @@ class ProjectController extends Controller
|
|||
|
||||
$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;
|
||||
$results[] = $p->toArray();
|
||||
}
|
||||
|
||||
return new DataResponse([
|
||||
|
@ -44,4 +35,64 @@ class ProjectController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/api/v1/projects/{projectId}", name="api_v1_show_project", methods={"GET"})
|
||||
* @IsGranted("ROLE_USER")
|
||||
*/
|
||||
function showProject($projectId) {
|
||||
|
||||
/** @var Project|null */
|
||||
$project = $this->getDoctrine()
|
||||
->getRepository(Project::class)
|
||||
->find($projectId)
|
||||
;
|
||||
|
||||
if ($project == null) {
|
||||
throw $this->createNotFoundException('Project does not exist.');
|
||||
}
|
||||
|
||||
return new DataResponse([
|
||||
'project' => $project->toArray(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/api/v1/projects", name="api_v1_create_project", methods={"POST"})
|
||||
* @IsGranted("ROLE_DEVELOPER")
|
||||
*/
|
||||
public function createProject(Request $request)
|
||||
{
|
||||
|
||||
$content = $request->getContent();
|
||||
$projectData = json_decode($content, true);
|
||||
|
||||
$newProject = new Project();
|
||||
|
||||
if (!isset($projectData['name'])) {
|
||||
return new ErrorResponse(0, "You must provide a project name");
|
||||
}
|
||||
$newProject->setName($projectData['name']);
|
||||
|
||||
if(isset($projectData['users']) && is_array($projectData['users'])) {
|
||||
$userRepository = $this->getDoctrine()->getRepository(User::class);
|
||||
foreach($projectData['users'] as $userId) {
|
||||
/** @var User */
|
||||
$user = $userRepository->find($userId);
|
||||
if ($user == null) {
|
||||
return new ErrorResponse(1, "Invalid user id");
|
||||
}
|
||||
|
||||
$newProject->addUser($user);
|
||||
}
|
||||
}
|
||||
|
||||
$manager = $this->getDoctrine()->getManager();
|
||||
$manager->persist($newProject);
|
||||
$manager->flush();
|
||||
|
||||
return new DataResponse([
|
||||
'project' => $newProject->toArray(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -112,4 +112,21 @@ class Project
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toArray(): array {
|
||||
$project = [
|
||||
'id' => $this->getId(),
|
||||
'name' => $this->getName(),
|
||||
];
|
||||
|
||||
$project['users'] = $this->getUsers()->map(function($user) {
|
||||
return ['id' => $user->getId(), 'username' => $user->getUsername()];
|
||||
})->toArray();
|
||||
|
||||
$project['requests'] = $this->getRequests()->map(function($req) {
|
||||
return ['id' => $req->getId(), 'title' => $req->getTitle() ];
|
||||
})->toArray();
|
||||
|
||||
return $project;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue