diff --git a/src/App/Action/Comment/DeleteAction.php b/src/App/Action/Comment/DeleteAction.php new file mode 100644 index 0000000..232da6e --- /dev/null +++ b/src/App/Action/Comment/DeleteAction.php @@ -0,0 +1,37 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $id = $request->getAttribute('id'); + + $result = $this->entityService->delete($id); + + $return = new JsonResponse($result); + + if (false === $result) { + return $return->withStatus(500, 'Failed to delete record.'); + } + + return $return; + } +} diff --git a/src/App/Action/Comment/DeleteFactory.php b/src/App/Action/Comment/DeleteFactory.php new file mode 100644 index 0000000..268a759 --- /dev/null +++ b/src/App/Action/Comment/DeleteFactory.php @@ -0,0 +1,17 @@ +get(CommentService::class); + return new DeleteAction($entityService); + } +} diff --git a/src/App/Action/Comment/GetAction.php b/src/App/Action/Comment/GetAction.php new file mode 100644 index 0000000..7bdfee0 --- /dev/null +++ b/src/App/Action/Comment/GetAction.php @@ -0,0 +1,35 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $id = $request->getAttribute('id'); + $entity = $this->entityService->get($id); + + if (null === $entity) { + $ret = new JsonResponse(false); + return $ret->withStatus(404); + } + + return new JsonResponse($entity); + } +} diff --git a/src/App/Action/Comment/GetFactory.php b/src/App/Action/Comment/GetFactory.php new file mode 100644 index 0000000..7c6d601 --- /dev/null +++ b/src/App/Action/Comment/GetFactory.php @@ -0,0 +1,17 @@ +get(CommentService::class); + return new GetAction($entityService); + } +} diff --git a/src/App/Action/Comment/ListAction.php b/src/App/Action/Comment/ListAction.php new file mode 100644 index 0000000..fef8b95 --- /dev/null +++ b/src/App/Action/Comment/ListAction.php @@ -0,0 +1,27 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + return new JsonResponse($this->entityService->getList()); + } +} diff --git a/src/App/Action/Comment/ListFactory.php b/src/App/Action/Comment/ListFactory.php new file mode 100644 index 0000000..305f42e --- /dev/null +++ b/src/App/Action/Comment/ListFactory.php @@ -0,0 +1,17 @@ +get(CommentService::class); + return new ListAction($entityService); + } +} diff --git a/src/App/Action/Comment/PostAction.php b/src/App/Action/Comment/PostAction.php new file mode 100644 index 0000000..871f684 --- /dev/null +++ b/src/App/Action/Comment/PostAction.php @@ -0,0 +1,37 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $data = $this->getRequestData($request); + + $result = $this->entityService->create($data); + + if (false === $result) { + $result = new JsonResponse(false); + return $result->withStatus(500, 'Failed to insert record.'); + } + + return new JsonResponse($result); + } +} diff --git a/src/App/Action/Comment/PostFactory.php b/src/App/Action/Comment/PostFactory.php new file mode 100644 index 0000000..582712d --- /dev/null +++ b/src/App/Action/Comment/PostFactory.php @@ -0,0 +1,17 @@ +get(CommentService::class); + return new PostAction($entityService); + } +} diff --git a/src/App/Action/Comment/PutAction.php b/src/App/Action/Comment/PutAction.php new file mode 100644 index 0000000..826ad5d --- /dev/null +++ b/src/App/Action/Comment/PutAction.php @@ -0,0 +1,38 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $id = $request->getAttribute('id', false); + $data = $this->getRequestData($request); + + $result = $this->entityService->modify($id, $data); + + if (false === $result) { + $result = new JsonResponse(false); + return $result->withStatus(500, 'Failed to update record.'); + } + + return new JsonResponse($result); + } +} diff --git a/src/App/Action/Comment/PutFactory.php b/src/App/Action/Comment/PutFactory.php new file mode 100644 index 0000000..8da0be7 --- /dev/null +++ b/src/App/Action/Comment/PutFactory.php @@ -0,0 +1,17 @@ +get(CommentService::class); + return new PutAction($entityService); + } +} diff --git a/src/App/Action/User/DeleteAction.php b/src/App/Action/User/DeleteAction.php new file mode 100644 index 0000000..748c43c --- /dev/null +++ b/src/App/Action/User/DeleteAction.php @@ -0,0 +1,37 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $id = $request->getAttribute('id'); + + $result = $this->entityService->delete($id); + + $return = new JsonResponse($result); + + if (false === $result) { + return $return->withStatus(500, 'Failed to delete record.'); + } + + return $return; + } +} diff --git a/src/App/Action/User/DeleteFactory.php b/src/App/Action/User/DeleteFactory.php new file mode 100644 index 0000000..dc12151 --- /dev/null +++ b/src/App/Action/User/DeleteFactory.php @@ -0,0 +1,17 @@ +get(UserService::class); + return new DeleteAction($entityService); + } +} diff --git a/src/App/Action/User/GetAction.php b/src/App/Action/User/GetAction.php new file mode 100644 index 0000000..15b51b2 --- /dev/null +++ b/src/App/Action/User/GetAction.php @@ -0,0 +1,35 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $id = $request->getAttribute('id'); + $entity = $this->entityService->get($id); + + if (null === $entity) { + $ret = new JsonResponse(false); + return $ret->withStatus(404); + } + + return new JsonResponse($entity); + } +} diff --git a/src/App/Action/User/GetFactory.php b/src/App/Action/User/GetFactory.php new file mode 100644 index 0000000..084e628 --- /dev/null +++ b/src/App/Action/User/GetFactory.php @@ -0,0 +1,17 @@ +get(UserService::class); + return new GetAction($entityService); + } +} diff --git a/src/App/Action/User/ListAction.php b/src/App/Action/User/ListAction.php new file mode 100644 index 0000000..f727ade --- /dev/null +++ b/src/App/Action/User/ListAction.php @@ -0,0 +1,27 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + return new JsonResponse($this->entityService->getList()); + } +} diff --git a/src/App/Action/User/ListFactory.php b/src/App/Action/User/ListFactory.php new file mode 100644 index 0000000..f8f79d7 --- /dev/null +++ b/src/App/Action/User/ListFactory.php @@ -0,0 +1,17 @@ +get(UserService::class); + return new ListAction($entityService); + } +} diff --git a/src/App/Action/User/PostAction.php b/src/App/Action/User/PostAction.php new file mode 100644 index 0000000..b6dd119 --- /dev/null +++ b/src/App/Action/User/PostAction.php @@ -0,0 +1,37 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $data = $this->getRequestData($request); + + $result = $this->entityService->create($data); + + if (false === $result) { + $result = new JsonResponse(false); + return $result->withStatus(500, 'Failed to insert record.'); + } + + return new JsonResponse($result); + } +} diff --git a/src/App/Action/User/PostFactory.php b/src/App/Action/User/PostFactory.php new file mode 100644 index 0000000..e1f325b --- /dev/null +++ b/src/App/Action/User/PostFactory.php @@ -0,0 +1,17 @@ +get(UserService::class); + return new PostAction($entityService); + } +} diff --git a/src/App/Action/User/PutAction.php b/src/App/Action/User/PutAction.php new file mode 100644 index 0000000..6fd60c6 --- /dev/null +++ b/src/App/Action/User/PutAction.php @@ -0,0 +1,38 @@ +entityService = $entityService; + } + + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) + { + $id = $request->getAttribute('id', false); + $data = $this->getRequestData($request); + + $result = $this->entityService->modify($id, $data); + + if (false === $result) { + $result = new JsonResponse(false); + return $result->withStatus(500, 'Failed to update record.'); + } + + return new JsonResponse($result); + } +} diff --git a/src/App/Action/User/PutFactory.php b/src/App/Action/User/PutFactory.php new file mode 100644 index 0000000..25c82ff --- /dev/null +++ b/src/App/Action/User/PutFactory.php @@ -0,0 +1,17 @@ +get(UserService::class); + return new PutAction($entityService); + } +} diff --git a/src/App/Model/Language.php b/src/App/Model/Language.php new file mode 100644 index 0000000..b5c3d46 --- /dev/null +++ b/src/App/Model/Language.php @@ -0,0 +1,36 @@ + 'english', + self::LANG_GERMAN => 'german', + self::LANG_FRENCH => 'french', + self::LANG_SWEDISH => 'swedish', + ]; + + /** + * Returns all available + * @return string[] + */ + public static function getAllLanguages() + { + return self::$languages; + } + + public static function getLanguageByCode(string $language) + { + return self::$languages[$language] ?? 'unknown'; + } +} diff --git a/src/App/Service/Comment/CommentService.php b/src/App/Service/Comment/CommentService.php new file mode 100644 index 0000000..9ebe6d1 --- /dev/null +++ b/src/App/Service/Comment/CommentService.php @@ -0,0 +1,99 @@ +em = $em; + $this->hydrator = $hydrator; + } + + public function getList() + { + $qb = $this->em->createQueryBuilder(); + $entities = $qb->select('c,u,a') + ->from(Comment::class, 'c') + ->leftJoin('c.author', 'u') + ->leftJoin('c.article', 'a') + ->getQuery() + ->getArrayResult(); + return $entities; + } + + public function get($id) + { + $qb = $this->em->createQueryBuilder(); + + $entity = $qb->select('c,u,a') + ->from(Comment::class, 'c') + ->leftJoin('c.author', 'u') + ->leftJoin('c.article', 'a') + ->where('c.id = :cid') + ->setParameter('cid', $id) + ->getQuery() + ->getOneOrNullResult(Query::HYDRATE_ARRAY); + + return $entity; + } + + public function create($data) + { + $entity = $this->hydrator->hydrate($data, new Comment()); + try { + $this->em->persist($entity); + $this->em->flush(); + } catch (Exception $ex) { + return false; + } + return $entity; + } + + public function modify($id, $data) + { + if (null === ($entity = $this->em->find(Comment::class, $id))) { + return false; + } + + $entity = $this->hydrator->hydrate($data, $entity); + $this->em->persist($entity); + $this->em->flush(); + + return $entity; + } + + public function delete($id) + { + if (null === ($entity = $this->em->find(Comment::class, $id))) { + return false; + } + + try { + $this->em->remove($entity); + $this->em->flush(); + } catch (Exception $ex) { + return false; + } + + return true; + } +} diff --git a/src/App/Service/Comment/CommentServiceFactory.php b/src/App/Service/Comment/CommentServiceFactory.php new file mode 100644 index 0000000..fcda262 --- /dev/null +++ b/src/App/Service/Comment/CommentServiceFactory.php @@ -0,0 +1,16 @@ +getEntityManager($container); + $hydrator = $this->getDoctrineHydrator($container); + return new CommentService($em, $hydrator); + } +} diff --git a/src/App/Service/User/UserService.php b/src/App/Service/User/UserService.php new file mode 100644 index 0000000..0a1e444 --- /dev/null +++ b/src/App/Service/User/UserService.php @@ -0,0 +1,99 @@ +em = $em; + $this->hydrator = $hydrator; + } + + public function getList() + { + $qb = $this->em->createQueryBuilder(); + $entities = $qb->select('u,a,c') + ->from(User::class, 'u') + ->leftJoin('u.articles', 'a') + ->leftJoin('u.comments', 'c') + ->getQuery() + ->getArrayResult(); + return $entities; + } + + public function get($id) + { + $qb = $this->em->createQueryBuilder(); + + $entity = $qb->select('u,a,c') + ->from(User::class, 'u') + ->leftJoin('u.articles', 'a') + ->leftJoin('u.comments', 'c') + ->where('u.id = :uid') + ->setParameter('uid', $id) + ->getQuery() + ->getOneOrNullResult(Query::HYDRATE_ARRAY); + + return $entity; + } + + public function create($data) + { + $entity = $this->hydrator->hydrate($data, new User()); + try { + $this->em->persist($entity); + $this->em->flush(); + } catch (Exception $ex) { + return false; + } + return $entity; + } + + public function modify($id, $data) + { + if (null === ($entity = $this->em->find(User::class, $id))) { + return false; + } + + $entity = $this->hydrator->hydrate($data, $entity); + $this->em->persist($entity); + $this->em->flush(); + + return $entity; + } + + public function delete($id) + { + if (null === ($entity = $this->em->find(User::class, $id))) { + return false; + } + + try { + $this->em->remove($entity); + $this->em->flush(); + } catch (Exception $ex) { + return false; + } + + return true; + } +} diff --git a/src/App/Service/User/UserServiceFactory.php b/src/App/Service/User/UserServiceFactory.php new file mode 100644 index 0000000..4278673 --- /dev/null +++ b/src/App/Service/User/UserServiceFactory.php @@ -0,0 +1,16 @@ +getEntityManager($container); + $hydrator = $this->getDoctrineHydrator($container); + return new userService($em, $hydrator); + } +}