Skip to content

Commit

Permalink
Merge pull request #55 from maurobonfietti/v0.32.0
Browse files Browse the repository at this point in the history
Version 0.32.0
  • Loading branch information
maurobonfietti authored Nov 9, 2019
2 parents 4042e2d + 4611b7b commit 579e761
Show file tree
Hide file tree
Showing 22 changed files with 41 additions and 91 deletions.
33 changes: 2 additions & 31 deletions src/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Controller;

use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;

abstract class BaseController
Expand All @@ -13,42 +12,14 @@ abstract class BaseController
*/
protected $container;

/**
* @var Request $request
*/
protected $request;

/**
* @var Response $response
*/
protected $response;

/**
* @var array
*/
protected $args;

protected function setParams(Request $request, Response $response, array $args)
{
$this->request = $request;
$this->response = $response;
$this->args = $args;
}

/**
* @param string $status
* @param mixed $message
* @param int $code
* @return Response
*/
protected function jsonResponse(string $status, $message, int $code): Response
protected function jsonResponse(Response $response, string $status, $message, int $code): Response
{
$result = [
'code' => $code,
'status' => $status,
'message' => $message,
];

return $this->response->withJson($result, $code, JSON_PRETTY_PRINT);
return $response->withJson($result, $code, JSON_PRETTY_PRINT);
}
}
8 changes: 3 additions & 5 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class DefaultController extends BaseController
{
const API_VERSION = '0.31.0';
const API_VERSION = '0.32.0';

public function __construct(Container $container)
{
Expand All @@ -17,7 +17,6 @@ public function __construct(Container $container)

public function getHelp(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$url = getenv('APP_DOMAIN');
$endpoints = [
'tasks' => $url . '/api/v1/tasks',
Expand All @@ -33,12 +32,11 @@ public function getHelp(Request $request, Response $response, array $args): Resp
'timestamp' => time(),
];

return $this->jsonResponse('success', $message, 200);
return $this->jsonResponse($response, 'success', $message, 200);
}

public function getStatus(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$userService = $this->container->get('user_service');
$noteService = $this->container->get('note_service');
$taskService = $this->container->get('task_service');
Expand All @@ -53,6 +51,6 @@ public function getStatus(Request $request, Response $response, array $args): Re
'timestamp' => time(),
];

return $this->jsonResponse('success', $status, 200);
return $this->jsonResponse($response, 'success', $status, 200);
}
}
3 changes: 1 addition & 2 deletions src/Controller/Note/CreateNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class CreateNote extends BaseNote
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$note = $this->getNoteService()->createNote($input);

return $this->jsonResponse('success', $note, 201);
return $this->jsonResponse($response, 'success', $note, 201);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Note/DeleteNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class DeleteNote extends BaseNote
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$this->getNoteService()->deleteNote((int) $this->args['id']);
$this->getNoteService()->deleteNote((int) $args['id']);

return $this->jsonResponse('success', null, 204);
return $this->jsonResponse($response, 'success', null, 204);
}
}
3 changes: 1 addition & 2 deletions src/Controller/Note/GetAllNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class GetAllNotes extends BaseNote
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$notes = $this->getNoteService()->getNotes();

return $this->jsonResponse('success', $notes, 200);
return $this->jsonResponse($response, 'success', $notes, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Note/GetOneNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class GetOneNote extends BaseNote
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$note = $this->getNoteService()->getNote((int) $this->args['id']);
$note = $this->getNoteService()->getNote((int) $args['id']);

return $this->jsonResponse('success', $note, 200);
return $this->jsonResponse($response, 'success', $note, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Note/SearchNotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class SearchNotes extends BaseNote
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$notes = $this->getNoteService()->searchNotes($this->args['query']);
$notes = $this->getNoteService()->searchNotes($args['query']);

return $this->jsonResponse('success', $notes, 200);
return $this->jsonResponse($response, 'success', $notes, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Note/UpdateNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class UpdateNote extends BaseNote
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$note = $this->getNoteService()->updateNote($input, (int) $this->args['id']);
$note = $this->getNoteService()->updateNote($input, (int) $args['id']);

return $this->jsonResponse('success', $note, 200);
return $this->jsonResponse($response, 'success', $note, 200);
}
}
3 changes: 1 addition & 2 deletions src/Controller/Task/CreateTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class CreateTask extends BaseTask
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$task = $this->getTaskService()->createTask($input);

return $this->jsonResponse('success', $task, 201);
return $this->jsonResponse($response, 'success', $task, 201);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Task/DeleteTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class DeleteTask extends BaseTask
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$taskId = (int) $this->args['id'];
$taskId = (int) $args['id'];
$userId = (int) $input['decoded']->sub;
$task = $this->getTaskService()->deleteTask($taskId, $userId);

return $this->jsonResponse('success', $task, 204);
return $this->jsonResponse($response, 'success', $task, 204);
}
}
3 changes: 1 addition & 2 deletions src/Controller/Task/GetAllTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ class GetAllTasks extends BaseTask
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$userId = (int) $input['decoded']->sub;
$tasks = $this->getTaskService()->getTasks($userId);

return $this->jsonResponse('success', $tasks, 200);
return $this->jsonResponse($response, 'success', $tasks, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Task/GetOneTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class GetOneTask extends BaseTask
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$taskId = (int) $this->args['id'];
$taskId = (int) $args['id'];
$userId = (int) $input['decoded']->sub;
$task = $this->getTaskService()->getTask($taskId, $userId);

return $this->jsonResponse('success', $task, 200);
return $this->jsonResponse($response, 'success', $task, 200);
}
}
7 changes: 3 additions & 4 deletions src/Controller/Task/SearchTasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ class SearchTasks extends BaseTask
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$userId = (int) $input['decoded']->sub;
$query = '';
if (isset($this->args['query'])) {
$query = $this->args['query'];
if (isset($args['query'])) {
$query = $args['query'];
}
$status = $request->getParam('status', null);
$tasks = $this->getTaskService()->searchTasks($query, $userId, $status);

return $this->jsonResponse('success', $tasks, 200);
return $this->jsonResponse($response, 'success', $tasks, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/Task/UpdateTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class UpdateTask extends BaseTask
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$task = $this->getTaskService()->updateTask($input, (int) $this->args['id']);
$task = $this->getTaskService()->updateTask($input, (int) $args['id']);

return $this->jsonResponse('success', $task, 200);
return $this->jsonResponse($response, 'success', $task, 200);
}
}
4 changes: 2 additions & 2 deletions src/Controller/User/BaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ protected function getUserService(): UserService
return $this->container->get('user_service');
}

protected function checkUserPermissions($userIdLogged)
protected function checkUserPermissions($userId, $userIdLogged)
{
if ($this->args['id'] != $userIdLogged) {
if ($userId != $userIdLogged) {
throw new UserException('User permission failed.', 400);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Controller/User/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class CreateUser extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$user = $this->getUserService()->createUser($input);

return $this->jsonResponse('success', $user, 201);
return $this->jsonResponse($response, 'success', $user, 201);
}
}
7 changes: 3 additions & 4 deletions src/Controller/User/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class DeleteUser extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$userIdLogged = $input['decoded']->sub;
$this->checkUserPermissions($userIdLogged);
$user = $this->getUserService()->deleteUser((int) $this->args['id']);
$this->checkUserPermissions($args['id'], $userIdLogged);
$user = $this->getUserService()->deleteUser((int) $args['id']);

return $this->jsonResponse('success', $user, 204);
return $this->jsonResponse($response, 'success', $user, 204);
}
}
3 changes: 1 addition & 2 deletions src/Controller/User/GetAllUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class GetAllUsers extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$users = $this->getUserService()->getUsers();

return $this->jsonResponse('success', $users, 200);
return $this->jsonResponse($response, 'success', $users, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/User/GetOneUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class GetOneUser extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$user = $this->getUserService()->getUser((int) $this->args['id']);
$user = $this->getUserService()->getUser((int) $args['id']);

return $this->jsonResponse('success', $user, 200);
return $this->jsonResponse($response, 'success', $user, 200);
}
}
3 changes: 1 addition & 2 deletions src/Controller/User/LoginUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ class LoginUser extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$jwt = $this->getUserService()->loginUser($input);
$message = [
'Authorization' => 'Bearer ' . $jwt,
];

return $this->jsonResponse('success', $message, 200);
return $this->jsonResponse($response, 'success', $message, 200);
}
}
5 changes: 2 additions & 3 deletions src/Controller/User/SearchUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class SearchUsers extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$users = $this->getUserService()->searchUsers($this->args['query']);
$users = $this->getUserService()->searchUsers($args['query']);

return $this->jsonResponse('success', $users, 200);
return $this->jsonResponse($response, 'success', $users, 200);
}
}
7 changes: 3 additions & 4 deletions src/Controller/User/UpdateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class UpdateUser extends BaseUser
{
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->setParams($request, $response, $args);
$input = $request->getParsedBody();
$userIdLogged = $input['decoded']->sub;
$this->checkUserPermissions($userIdLogged);
$user = $this->getUserService()->updateUser($input, (int) $this->args['id']);
$this->checkUserPermissions($args['id'], $userIdLogged);
$user = $this->getUserService()->updateUser($input, (int) $args['id']);

return $this->jsonResponse('success', $user, 200);
return $this->jsonResponse($response, 'success', $user, 200);
}
}

0 comments on commit 579e761

Please sign in to comment.