aboutsummaryrefslogtreecommitdiffhomepage
path: root/api/Users
diff options
context:
space:
mode:
authorMarvin Borner2018-07-20 16:34:32 +0200
committerMarvin Borner2018-07-20 16:34:32 +0200
commit74cb1477bb921a2378ea22a552b71a48c11e0931 (patch)
tree621ab17315be667c16dad8f3d5f44d67a7a47e8f /api/Users
parent400591b34d4b0a6288834539808a9dede8a60e3a (diff)
Better API (integrated oauth completely)
Diffstat (limited to 'api/Users')
-rw-r--r--api/Users/Console/AddUserCommand.php59
-rw-r--r--api/Users/Controllers/UserController.php57
-rw-r--r--api/Users/Events/UserWasCreated.php16
-rw-r--r--api/Users/Events/UserWasDeleted.php16
-rw-r--r--api/Users/Events/UserWasUpdated.php16
-rw-r--r--api/Users/Exceptions/UserNotFoundException.php13
-rw-r--r--api/Users/Models/User.php30
-rw-r--r--api/Users/Repositories/UserRepository.php35
-rw-r--r--api/Users/Requests/CreateUserRequest.php30
-rw-r--r--api/Users/Services/UserService.php88
-rw-r--r--api/Users/UserServiceProvider.php23
-rw-r--r--api/Users/routes.php7
12 files changed, 390 insertions, 0 deletions
diff --git a/api/Users/Console/AddUserCommand.php b/api/Users/Console/AddUserCommand.php
new file mode 100644
index 0000000..021d5aa
--- /dev/null
+++ b/api/Users/Console/AddUserCommand.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Api\Users\Console;
+
+use Api\Users\Repositories\UserRepository;
+use Illuminate\Console\Command;
+
+class AddUserCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'users:add {name} {email} {password}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Adds a new user';
+
+ /**
+ * User repository to persist user in database
+ *
+ * @var UserRepository
+ */
+ protected $userRepository;
+
+ /**
+ * Create a new command instance.
+ *
+ * @param UserRepository $userRepository
+ * @return void
+ */
+ public function __construct(UserRepository $userRepository)
+ {
+ parent::__construct();
+
+ $this->userRepository = $userRepository;
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = $this->userRepository->create([
+ 'name' => $this->argument('name'),
+ 'email' => $this->argument('email'),
+ 'password' => $this->argument('password')
+ ]);
+
+ $this->info(sprintf('A user was created with ID %s', $user->id));
+ }
+} \ No newline at end of file
diff --git a/api/Users/Controllers/UserController.php b/api/Users/Controllers/UserController.php
new file mode 100644
index 0000000..5178dcb
--- /dev/null
+++ b/api/Users/Controllers/UserController.php
@@ -0,0 +1,57 @@
+<?php
+
+namespace Api\Users\Controllers;
+
+use Illuminate\Http\Request;
+use Infrastructure\Http\Controller;
+use Api\Users\Requests\CreateUserRequest;
+use Api\Users\Services\UserService;
+
+class UserController extends Controller
+{
+ private $userService;
+
+ public function __construct(UserService $userService)
+ {
+ $this->userService = $userService;
+ }
+
+ public function getAll()
+ {
+ $resourceOptions = $this->parseResourceOptions();
+
+ $data = $this->userService->getAll($resourceOptions);
+ $parsedData = $this->parseData($data, $resourceOptions, 'users');
+
+ return $this->response($parsedData);
+ }
+
+ public function getById($userId)
+ {
+ $resourceOptions = $this->parseResourceOptions();
+
+ $data = $this->userService->getById($userId, $resourceOptions);
+ $parsedData = $this->parseData($data, $resourceOptions, 'user');
+
+ return $this->response($parsedData);
+ }
+
+ public function create(CreateUserRequest $request)
+ {
+ $data = $request->get('user', []);
+
+ return $this->response($this->userService->create($data), 201);
+ }
+
+ public function update($userId, Request $request)
+ {
+ $data = $request->get('user', []);
+
+ return $this->response($this->userService->update($userId, $data));
+ }
+
+ public function delete($userId)
+ {
+ return $this->response($this->userService->delete($userId));
+ }
+}
diff --git a/api/Users/Events/UserWasCreated.php b/api/Users/Events/UserWasCreated.php
new file mode 100644
index 0000000..3975ad3
--- /dev/null
+++ b/api/Users/Events/UserWasCreated.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Api\Users\Events;
+
+use Infrastructure\Events\Event;
+use Api\Users\Models\User;
+
+class UserWasCreated extends Event
+{
+ public $user;
+
+ public function __construct(User $user)
+ {
+ $this->user = $user;
+ }
+}
diff --git a/api/Users/Events/UserWasDeleted.php b/api/Users/Events/UserWasDeleted.php
new file mode 100644
index 0000000..bfd7cff
--- /dev/null
+++ b/api/Users/Events/UserWasDeleted.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Api\Users\Events;
+
+use Infrastructure\Events\Event;
+use Api\Users\Models\User;
+
+class UserWasDeleted extends Event
+{
+ public $user;
+
+ public function __construct(User $user)
+ {
+ $this->user = $user;
+ }
+}
diff --git a/api/Users/Events/UserWasUpdated.php b/api/Users/Events/UserWasUpdated.php
new file mode 100644
index 0000000..02658d1
--- /dev/null
+++ b/api/Users/Events/UserWasUpdated.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Api\Users\Events;
+
+use Infrastructure\Events\Event;
+use Api\Users\Models\User;
+
+class UserWasUpdated extends Event
+{
+ public $user;
+
+ public function __construct(User $user)
+ {
+ $this->user = $user;
+ }
+}
diff --git a/api/Users/Exceptions/UserNotFoundException.php b/api/Users/Exceptions/UserNotFoundException.php
new file mode 100644
index 0000000..13fb40e
--- /dev/null
+++ b/api/Users/Exceptions/UserNotFoundException.php
@@ -0,0 +1,13 @@
+<?php
+
+namespace Api\Users\Exceptions;
+
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+
+class UserNotFoundException extends NotFoundHttpException
+{
+ public function __construct()
+ {
+ parent::__construct('The user was not found.');
+ }
+}
diff --git a/api/Users/Models/User.php b/api/Users/Models/User.php
new file mode 100644
index 0000000..9e5b0b7
--- /dev/null
+++ b/api/Users/Models/User.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Api\Users\Models;
+
+use Laravel\Passport\HasApiTokens;
+use Illuminate\Notifications\Notifiable;
+use Illuminate\Foundation\Auth\User as Authenticatable;
+
+class User extends Authenticatable
+{
+ use HasApiTokens, Notifiable;
+
+ /**
+ * The attributes that are mass assignable.
+ *
+ * @var array
+ */
+ protected $fillable = [
+ 'name', 'email', 'password',
+ ];
+
+ /**
+ * The attributes that should be hidden for arrays.
+ *
+ * @var array
+ */
+ protected $hidden = [
+ 'password', 'remember_token',
+ ];
+}
diff --git a/api/Users/Repositories/UserRepository.php b/api/Users/Repositories/UserRepository.php
new file mode 100644
index 0000000..68416df
--- /dev/null
+++ b/api/Users/Repositories/UserRepository.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Api\Users\Repositories;
+
+use Api\Users\Models\User;
+use Infrastructure\Database\Eloquent\Repository;
+
+class UserRepository extends Repository
+{
+ public function getModel()
+ {
+ return new User();
+ }
+
+ public function create(array $data)
+ {
+ $user = $this->getModel();
+
+ $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
+
+ $user->fill($data);
+ $user->save();
+
+ return $user;
+ }
+
+ public function update(User $user, array $data)
+ {
+ $user->fill($data);
+
+ $user->save();
+
+ return $user;
+ }
+}
diff --git a/api/Users/Requests/CreateUserRequest.php b/api/Users/Requests/CreateUserRequest.php
new file mode 100644
index 0000000..c168eb2
--- /dev/null
+++ b/api/Users/Requests/CreateUserRequest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Api\Users\Requests;
+
+use Infrastructure\Http\ApiRequest;
+
+class CreateUserRequest extends ApiRequest
+{
+ public function authorize()
+ {
+ return true;
+ }
+
+ public function rules()
+ {
+ return [
+ 'user' => 'array|required',
+ 'user.email' => 'required|email',
+ 'user.name' => 'required|string',
+ 'user.password' => 'required|string|min:8'
+ ];
+ }
+
+ public function attributes()
+ {
+ return [
+ 'user.email' => 'the user\'s email'
+ ];
+ }
+}
diff --git a/api/Users/Services/UserService.php b/api/Users/Services/UserService.php
new file mode 100644
index 0000000..923acd0
--- /dev/null
+++ b/api/Users/Services/UserService.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Api\Users\Services;
+
+use Exception;
+use Illuminate\Auth\AuthManager;
+use Illuminate\Database\DatabaseManager;
+use Illuminate\Events\Dispatcher;
+use Api\Users\Exceptions\UserNotFoundException;
+use Api\Users\Events\UserWasCreated;
+use Api\Users\Events\UserWasDeleted;
+use Api\Users\Events\UserWasUpdated;
+use Api\Users\Repositories\UserRepository;
+
+class UserService
+{
+ private $auth;
+
+ private $database;
+
+ private $dispatcher;
+
+ private $userRepository;
+
+ public function __construct(
+ AuthManager $auth,
+ DatabaseManager $database,
+ Dispatcher $dispatcher,
+ UserRepository $userRepository
+ ) {
+ $this->auth = $auth;
+ $this->database = $database;
+ $this->dispatcher = $dispatcher;
+ $this->userRepository = $userRepository;
+ }
+
+ public function getAll($options = [])
+ {
+ return $this->userRepository->get($options);
+ }
+
+ public function getById($userId, array $options = [])
+ {
+ $user = $this->getRequestedUser($userId);
+
+ return $user;
+ }
+
+ public function create($data)
+ {
+ $user = $this->userRepository->create($data);
+
+ $this->dispatcher->fire(new UserWasCreated($user));
+
+ return $user;
+ }
+
+ public function update($userId, array $data)
+ {
+ $user = $this->getRequestedUser($userId);
+
+ $this->userRepository->update($user, $data);
+
+ $this->dispatcher->fire(new UserWasUpdated($user));
+
+ return $user;
+ }
+
+ public function delete($userId)
+ {
+ $user = $this->getRequestedUser($userId);
+
+ $this->userRepository->delete($userId);
+
+ $this->dispatcher->fire(new UserWasDeleted($user));
+ }
+
+ private function getRequestedUser($userId)
+ {
+ $user = $this->userRepository->getById($userId);
+
+ if (is_null($user)) {
+ throw new UserNotFoundException();
+ }
+
+ return $user;
+ }
+}
diff --git a/api/Users/UserServiceProvider.php b/api/Users/UserServiceProvider.php
new file mode 100644
index 0000000..af59a79
--- /dev/null
+++ b/api/Users/UserServiceProvider.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Api\Users;
+
+use Infrastructure\Events\EventServiceProvider;
+use Api\Users\Events\UserWasCreated;
+use Api\Users\Events\UserWasDeleted;
+use Api\Users\Events\UserWasUpdated;
+
+class UserServiceProvider extends EventServiceProvider
+{
+ protected $listen = [
+ UserWasCreated::class => [
+ // listeners for when a user is created
+ ],
+ UserWasDeleted::class => [
+ // listeners for when a user is deleted
+ ],
+ UserWasUpdated::class => [
+ // listeners for when a user is updated
+ ]
+ ];
+}
diff --git a/api/Users/routes.php b/api/Users/routes.php
new file mode 100644
index 0000000..f24f0c0
--- /dev/null
+++ b/api/Users/routes.php
@@ -0,0 +1,7 @@
+<?php
+
+$router->get('/users', 'UserController@getAll');
+$router->get('/users/{id}', 'UserController@getById');
+$router->post('/users', 'UserController@create');
+$router->put('/users/{id}', 'UserController@update');
+$router->delete('/users/{id}', 'UserController@delete');