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/Console/AddPostCommand.php | 59 +++++++++++++++++ api/Posts/Controllers/PostController.php | 57 +++++++++++++++++ api/Posts/Events/PostWasCreated.php | 16 +++++ api/Posts/Events/PostWasDeleted.php | 16 +++++ api/Posts/Events/PostWasUpdated.php | 16 +++++ api/Posts/Exceptions/PostNotFoundException.php | 13 ++++ api/Posts/Models/PostImage.php | 21 ++++++ api/Posts/Models/PostText.php | 21 ++++++ api/Posts/PostServiceProvider.php | 23 +++++++ api/Posts/Repositories/PostRepository.php | 35 ++++++++++ api/Posts/Requests/CreatePostRequest.php | 30 +++++++++ api/Posts/Services/PostService.php | 88 ++++++++++++++++++++++++++ api/Posts/routes.php | 7 ++ 13 files changed, 402 insertions(+) create mode 100644 api/Posts/Console/AddPostCommand.php create mode 100644 api/Posts/Controllers/PostController.php create mode 100644 api/Posts/Events/PostWasCreated.php create mode 100644 api/Posts/Events/PostWasDeleted.php create mode 100644 api/Posts/Events/PostWasUpdated.php create mode 100644 api/Posts/Exceptions/PostNotFoundException.php create mode 100644 api/Posts/Models/PostImage.php create mode 100644 api/Posts/Models/PostText.php create mode 100644 api/Posts/PostServiceProvider.php create mode 100644 api/Posts/Repositories/PostRepository.php create mode 100644 api/Posts/Requests/CreatePostRequest.php create mode 100644 api/Posts/Services/PostService.php create mode 100644 api/Posts/routes.php (limited to 'api/Posts') diff --git a/api/Posts/Console/AddPostCommand.php b/api/Posts/Console/AddPostCommand.php new file mode 100644 index 0000000..977afb6 --- /dev/null +++ b/api/Posts/Console/AddPostCommand.php @@ -0,0 +1,59 @@ +postRepository = $postRepository; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $post = $this->postRepository->create([ + 'name' => $this->argument('name'), + 'email' => $this->argument('email'), + 'password' => $this->argument('password') + ]); + + $this->info(sprintf('A post was created with ID %s', $post->id)); + } +} \ No newline at end of file diff --git a/api/Posts/Controllers/PostController.php b/api/Posts/Controllers/PostController.php new file mode 100644 index 0000000..105c7c2 --- /dev/null +++ b/api/Posts/Controllers/PostController.php @@ -0,0 +1,57 @@ +postService = $postService; + } + + public function getAll() + { + $resourceOptions = $this->parseResourceOptions(); + + $data = $this->postService->getAll($resourceOptions); + $parsedData = $this->parseData($data, $resourceOptions, 'posts'); + + return $this->response($parsedData); + } + + public function getById($postId) + { + $resourceOptions = $this->parseResourceOptions(); + + $data = $this->postService->getById($postId, $resourceOptions); + $parsedData = $this->parseData($data, $resourceOptions, 'post'); + + return $this->response($parsedData); + } + + public function create(CreatePostRequest $request) + { + $data = $request->get('post', []); + + return $this->response($this->postService->create($data), 201); + } + + public function update($postId, Request $request) + { + $data = $request->get('post', []); + + return $this->response($this->postService->update($postId, $data)); + } + + public function delete($postId) + { + return $this->response($this->postService->delete($postId)); + } +} diff --git a/api/Posts/Events/PostWasCreated.php b/api/Posts/Events/PostWasCreated.php new file mode 100644 index 0000000..8cdcbf8 --- /dev/null +++ b/api/Posts/Events/PostWasCreated.php @@ -0,0 +1,16 @@ +post = $post; + } +} diff --git a/api/Posts/Events/PostWasDeleted.php b/api/Posts/Events/PostWasDeleted.php new file mode 100644 index 0000000..5def797 --- /dev/null +++ b/api/Posts/Events/PostWasDeleted.php @@ -0,0 +1,16 @@ +post = $post; + } +} diff --git a/api/Posts/Events/PostWasUpdated.php b/api/Posts/Events/PostWasUpdated.php new file mode 100644 index 0000000..441e884 --- /dev/null +++ b/api/Posts/Events/PostWasUpdated.php @@ -0,0 +1,16 @@ +post = $post; + } +} diff --git a/api/Posts/Exceptions/PostNotFoundException.php b/api/Posts/Exceptions/PostNotFoundException.php new file mode 100644 index 0000000..2f9c55d --- /dev/null +++ b/api/Posts/Exceptions/PostNotFoundException.php @@ -0,0 +1,13 @@ + [ + // listeners for when a post is created + ], + PostWasDeleted::class => [ + // listeners for when a post is deleted + ], + PostWasUpdated::class => [ + // listeners for when a post is updated + ] + ]; +} diff --git a/api/Posts/Repositories/PostRepository.php b/api/Posts/Repositories/PostRepository.php new file mode 100644 index 0000000..671412f --- /dev/null +++ b/api/Posts/Repositories/PostRepository.php @@ -0,0 +1,35 @@ +getModel(); + + $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT); + + $post->fill($data); + $post->save(); + + return $post; + } + + public function update(Post $post, array $data) + { + $post->fill($data); + + $post->save(); + + return $post; + } +} diff --git a/api/Posts/Requests/CreatePostRequest.php b/api/Posts/Requests/CreatePostRequest.php new file mode 100644 index 0000000..be777ac --- /dev/null +++ b/api/Posts/Requests/CreatePostRequest.php @@ -0,0 +1,30 @@ + 'array|required', + 'post.email' => 'required|email', + 'post.name' => 'required|string', + 'post.password' => 'required|string|min:8' + ]; + } + + public function attributes() + { + return [ + 'post.email' => 'the post\'s email' + ]; + } +} 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; + } +} diff --git a/api/Posts/routes.php b/api/Posts/routes.php new file mode 100644 index 0000000..c34c120 --- /dev/null +++ b/api/Posts/routes.php @@ -0,0 +1,7 @@ +get('/posts', 'PostController@getAll'); +$router->get('/posts/{id}', 'PostController@getById'); +$router->post('/posts', 'PostController@create'); +$router->put('/posts/{id}', 'PostController@update'); +$router->delete('/posts/{id}', 'PostController@delete'); -- cgit v1.2.3