From 0ee38e98532a9daf7ba08ab65b7f73d6505e0aea Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 24 Aug 2018 13:17:32 +0200 Subject: Began API for posts --- api/Posts/Services/PostService.php | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 api/Posts/Services/PostService.php (limited to 'api/Posts/Services') diff --git a/api/Posts/Services/PostService.php b/api/Posts/Services/PostService.php new file mode 100644 index 0000000..7fc5dbb --- /dev/null +++ b/api/Posts/Services/PostService.php @@ -0,0 +1,88 @@ +auth = $auth; + $this->database = $database; + $this->dispatcher = $dispatcher; + $this->postRepository = $postRepository; + } + + public function getAll($options = []) + { + return $this->postRepository->get($options); + } + + public function getById($postId, array $options = []) + { + $post = $this->getRequestedPost($postId); + + return $post; + } + + public function create($data) + { + $post = $this->postRepository->create($data); + + $this->dispatcher->fire(new PostWasCreated($post)); + + return $post; + } + + public function update($postId, array $data) + { + $post = $this->getRequestedPost($postId); + + $this->postRepository->update($post, $data); + + $this->dispatcher->fire(new PostWasUpdated($post)); + + return $post; + } + + public function delete($postId) + { + $post = $this->getRequestedPost($postId); + + $this->postRepository->delete($postId); + + $this->dispatcher->fire(new PostWasDeleted($post)); + } + + private function getRequestedPost($postId) + { + $post = $this->postRepository->getById($postId); + + if (is_null($post)) { + throw new PostNotFoundException(); + } + + return $post; + } +} -- cgit v1.2.3