diff options
author | Marvin Borner | 2018-08-24 13:17:32 +0200 |
---|---|---|
committer | Marvin Borner | 2018-08-24 13:17:32 +0200 |
commit | 0ee38e98532a9daf7ba08ab65b7f73d6505e0aea (patch) | |
tree | 6ff916179bb4ebe14369ea99b84109dc6e52ba13 /api | |
parent | 3045cb39be8b9c9cb3ca6ed643ccceac0042f0c0 (diff) |
Began API for posts
Diffstat (limited to 'api')
-rw-r--r-- | api/Posts/Console/AddPostCommand.php | 59 | ||||
-rw-r--r-- | api/Posts/Controllers/PostController.php | 57 | ||||
-rw-r--r-- | api/Posts/Events/PostWasCreated.php | 16 | ||||
-rw-r--r-- | api/Posts/Events/PostWasDeleted.php | 16 | ||||
-rw-r--r-- | api/Posts/Events/PostWasUpdated.php | 16 | ||||
-rw-r--r-- | api/Posts/Exceptions/PostNotFoundException.php | 13 | ||||
-rw-r--r-- | api/Posts/Models/PostImage.php | 21 | ||||
-rw-r--r-- | api/Posts/Models/PostText.php | 21 | ||||
-rw-r--r-- | api/Posts/PostServiceProvider.php | 23 | ||||
-rw-r--r-- | api/Posts/Repositories/PostRepository.php | 35 | ||||
-rw-r--r-- | api/Posts/Requests/CreatePostRequest.php | 30 | ||||
-rw-r--r-- | api/Posts/Services/PostService.php | 88 | ||||
-rw-r--r-- | api/Posts/routes.php | 7 |
13 files changed, 402 insertions, 0 deletions
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 @@ +<?php + +namespace Api\Posts\Console; + +use Api\Posts\Repositories\PostRepository; +use Illuminate\Console\Command; + +class AddPostCommand extends Command +{ + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'posts:add {name} {email} {password}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Adds a new post'; + + /** + * Post repository to persist post in database + * + * @var PostRepository + */ + protected $postRepository; + + /** + * Create a new command instance. + * + * @param PostRepository $postRepository + * @return void + */ + public function __construct(PostRepository $postRepository) + { + parent::__construct(); + + $this->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 @@ +<?php + +namespace Api\Posts\Controllers; + +use Illuminate\Http\Request; +use Infrastructure\Http\Controller; +use Api\Posts\Requests\CreatePostRequest; +use Api\Posts\Services\PostService; + +class PostController extends Controller +{ + private $postService; + + public function __construct(PostService $postService) + { + $this->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 @@ +<?php + +namespace Api\Posts\Events; + +use Infrastructure\Events\Event; +use Api\Posts\Models\Post; + +class PostWasCreated extends Event +{ + public $post; + + public function __construct(Post $post) + { + $this->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 @@ +<?php + +namespace Api\Posts\Events; + +use Infrastructure\Events\Event; +use Api\Posts\Models\Post; + +class PostWasDeleted extends Event +{ + public $post; + + public function __construct(Post $post) + { + $this->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 @@ +<?php + +namespace Api\Posts\Events; + +use Infrastructure\Events\Event; +use Api\Posts\Models\Post; + +class PostWasUpdated extends Event +{ + public $post; + + public function __construct(Post $post) + { + $this->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 @@ +<?php + +namespace Api\Posts\Exceptions; + +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + +class PostNotFoundException extends NotFoundHttpException +{ + public function __construct() + { + parent::__construct('The post was not found.'); + } +} diff --git a/api/Posts/Models/PostImage.php b/api/Posts/Models/PostImage.php new file mode 100644 index 0000000..637d1ec --- /dev/null +++ b/api/Posts/Models/PostImage.php @@ -0,0 +1,21 @@ +<?php + +namespace Api\Posts\Models; + +use Laravel\Passport\HasApiTokens; +use Illuminate\Notifications\Notifiable; +use Illuminate\Database\Eloquent\Model; + +class PostImage extends Model +{ + use HasApiTokens, Notifiable; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'user_id', 'description', 'image_path', + ]; +} diff --git a/api/Posts/Models/PostText.php b/api/Posts/Models/PostText.php new file mode 100644 index 0000000..2e4a4bb --- /dev/null +++ b/api/Posts/Models/PostText.php @@ -0,0 +1,21 @@ +<?php + +namespace Api\Posts\Models; + +use Laravel\Passport\HasApiTokens; +use Illuminate\Notifications\Notifiable; +use Illuminate\Database\Eloquent\Model; + +class PostText extends Model +{ + use HasApiTokens, Notifiable; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'user_id', 'text' + ]; +} diff --git a/api/Posts/PostServiceProvider.php b/api/Posts/PostServiceProvider.php new file mode 100644 index 0000000..181b308 --- /dev/null +++ b/api/Posts/PostServiceProvider.php @@ -0,0 +1,23 @@ +<?php + +namespace Api\Posts; + +use Infrastructure\Events\EventServiceProvider; +use Api\Posts\Events\PostWasCreated; +use Api\Posts\Events\PostWasDeleted; +use Api\Posts\Events\PostWasUpdated; + +class PostServiceProvider extends EventServiceProvider +{ + protected $listen = [ + PostWasCreated::class => [ + // 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 @@ +<?php + +namespace Api\Posts\Repositories; + +use Api\Posts\Models\Post; +use Infrastructure\Database\Eloquent\Repository; + +class PostRepository extends Repository +{ + public function getModel() + { + return new Post(); + } + + public function create(array $data) + { + $post = $this->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 @@ +<?php + +namespace Api\Posts\Requests; + +use Infrastructure\Http\ApiRequest; + +class CreatePostRequest extends ApiRequest +{ + public function authorize() + { + return true; + } + + public function rules() + { + return [ + 'post' => '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 @@ +<?php + +namespace Api\Posts\Services; + +use Exception; +use Illuminate\Auth\AuthManager; +use Illuminate\Database\DatabaseManager; +use Illuminate\Events\Dispatcher; +use Api\Posts\Exceptions\PostNotFoundException; +use Api\Posts\Events\PostWasCreated; +use Api\Posts\Events\PostWasDeleted; +use Api\Posts\Events\PostWasUpdated; +use Api\Posts\Repositories\PostRepository; + +class PostService +{ + private $auth; + + private $database; + + private $dispatcher; + + private $postRepository; + + public function __construct( + AuthManager $auth, + DatabaseManager $database, + Dispatcher $dispatcher, + PostRepository $postRepository + ) { + $this->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 @@ +<?php + +$router->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'); |