diff --git a/src/Controller/ProjectController.php b/src/Controller/ProjectController.php new file mode 100644 index 0000000..e81c38d --- /dev/null +++ b/src/Controller/ProjectController.php @@ -0,0 +1,58 @@ +createForm(ProjectType::class, $project); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + + $project->setOwner($this->getUser()); + $project->setGeneratedKey(); + + $this->getDoctrine()->getManager()->persist($project); + $this->getDoctrine()->getManager()->flush(); + + return $this->redirectToRoute('projects_show'); + } + + return $this->render("project/create.html.twig", [ + 'form' => $form->createView() + ]); + } + + /** + * + * @Route("/projects", name="projects_show") + * @return Response + */ + public function show(Request $request): Response + { + $projects = $this->getDoctrine()->getRepository(Project::class) + ->findBy([]); + + return $this->render("project/projectsList.html.twig", [ + 'projects' => $projects + ]); + } +} \ No newline at end of file diff --git a/src/Entity/Project.php b/src/Entity/Project.php new file mode 100644 index 0000000..5c67d42 --- /dev/null +++ b/src/Entity/Project.php @@ -0,0 +1,92 @@ +name; + } + + public function setName($name): void + { + $this->name = $name; + } + + public function getId(): ?int + { + return $this->id; + } + + public function getGeneratedKey(): ?string + { + return $this->generatedKey; + } + + public function setGeneratedKey(): void + { + $isUnique = true; + $newKey = strval(random_int(100000, 999999)); + foreach (self::$generatedKeys as $key) + { + if ($key === $newKey) + { + $isUnique = false; + break; + } + } + if ($isUnique) + { + $this->generatedKey = $newKey; + self::$generatedKeys[] = $newKey; + } + else + { + $this->setGeneratedKey(); + } + } + + public function getOwner() + { + return $this->owner; + } + + public function setOwner(UserInterface $owner): void + { + $this->owner = $owner; + } +} \ No newline at end of file diff --git a/src/Repository/ProjectRepository.php b/src/Repository/ProjectRepository.php new file mode 100644 index 0000000..5ae1268 --- /dev/null +++ b/src/Repository/ProjectRepository.php @@ -0,0 +1,16 @@ +add('name', TextType::class) + ->add('save', SubmitType::class); + } +} \ No newline at end of file diff --git a/templates/project/create.html.twig b/templates/project/create.html.twig new file mode 100644 index 0000000..e2b7544 --- /dev/null +++ b/templates/project/create.html.twig @@ -0,0 +1,16 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
| # | +Key | +Name | +
|---|---|---|
| {{ project.id }} | +{{ project.generatedKey }} | +{{ project.name }} | +