Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js mvc #93

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions app/Controller/Web/Session.php → app/Controller/Api/Session.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
namespace Notes\Controller\Web;
namespace Notes\Controller\Api;

use Notes\View\View as View;
use Notes\Service\Session as SessionService;
use Notes\Model\Session as SessionModel;
use Notes\Model\User as UserModel;
use Notes\Response\Response as Response;
use Notes\Request\Request as Request;
use Notes\Exception\ModelNotFoundException as ModelNotFoundException;
Expand All @@ -17,31 +18,34 @@ public function __construct($request)
$this->request = $request;
$this->view = new View();
}
public function get()
{
$this->view->render("Login.php");
}

public function post()
{
$input = $this->request->getUrlParams();
$input = $this->request->getData();

$userModel = new UserModel();
$userModel->setId($input['id']);
$userModel->setFirstName($input['firstName']);
$userModel->setLastName($input['lastName']);
$userModel->setEmail($input['email']);
$userModel->setPassword($input['password']);
$userModel->setCreatedOn($input['createdOn']);

$sessionService = new SessionService();
try {
$response = $sessionService->login($input);
$response = $sessionService->login($userModel);
if ($response instanceof SessionModel) {
setcookie('userId', $response->getUserId(), time() + (86400 * 30), "/");
setcookie('authToken', $response->getAuthToken(), time() + (86400 * 30), "/");
$app = \Slim\Slim::getInstance('developer');
$app->redirect("/notes");

$objResponse = new Response($response->toArray(), 1, "SUCCESS");

echo $objResponse->getResponse();
}
} catch (\InvalidArgumentException $error) {
$response = $error->getMessage();
$objResponse = new Response($response);
$this->view->render("Login.php", $objResponse->getResponse());
} catch (ModelNotFoundException $error) {
$response = $error->getMessage();
$objResponse = new Response($response);
$this->view->render("Login.php", $objResponse->getResponse());
$objResponse = new Response($response, 0, "FAILURE");
echo $objResponse->getResponse();
}
}
}
20 changes: 20 additions & 0 deletions app/Controller/Web/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Notes\Controller\Web;

use Notes\View\View as View;
use Notes\Request\Request as Request;

class Login
{
protected $request;
protected $view;
public function __construct($request)
{
$this->request = $request;
$this->view = new View();
}
public function get()
{
$this->view->render("Login.php");
}
}
2 changes: 2 additions & 0 deletions app/Controller/Web/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Notes\View\View as View;
use Notes\Service\User as UserService;
use Notes\Factory\User as UserFactory;
use Notes\Model\User as UserModel;
use Notes\Response\Response as Response;
use Notes\Exception\ModelNotFoundException as ModelNotFoundException;
Expand All @@ -23,6 +24,7 @@ public function get()
public function post()
{
$input = $this->request->getUrlParams();

$userService = new UserService();
try {
$response = $userService->create($input);
Expand Down
37 changes: 18 additions & 19 deletions app/Domain/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,74 @@

use Notes\Validator\InputValidator as InputValidator;

use Notes\Factory\User as UserFactory;

class Session
{
public function __construct()
{
$this->validator = new InputValidator();
}

public function create($userInput)
public function create($userModel)
{
$sessionModel = new sessionModel();
$userDomain = new UserDomain();
$userModelRead = $userDomain->readByUserNameAndPassword($userInput);
$userModelRead = $userDomain->readByUserNameAndPassword($userModel);

$sessionModel->setUserId($userModelRead->getId());

$randomNumber = rand();

$password = $userModelRead->getPassword();

$sessionModel->createAuthToken($password, $randomNumber);

$sessionModel->setCreatedOn(date("Y-m-d H:i:s"));

if ($this->validator->notNull($sessionModel->getUserId())
&& $this->validator->validNumber($sessionModel->getUserId())
&& $this->validator->notNull($sessionModel->getAuthToken())) {
$this->validator->notNull($sessionModel->getUserId());
$this->validator->validNumber($sessionModel->getUserId());
$this->validator->notNull($sessionModel->getAuthToken());

$sessionMapper = new SessionMapper();
$sessionModel = $sessionMapper->create($sessionModel);
return $sessionModel;
}
}
public function read($sessionModel)
{
if ($this->validator->notNull($sessionModel->getId())
&& $this->validator->validNumber($sessionModel->getId())) {
$this->validator->notNull($sessionModel->getId());
$this->validator->validNumber($sessionModel->getId());
$sessionMapper = new SessionMapper();

$sessionModel = $sessionMapper->read($sessionModel);

return $sessionModel;
}
}

public function getSessionByAuthTokenAndUserId($sessionModel)
{
if ($this->validator->notNull($sessionModel->getUserId())
&& $this->validator->validNumber($sessionModel->getUserId())
&& $this->validator->notNull($sessionModel->getAuthToken())) {
$this->validator->notNull($sessionModel->getUserId());
$this->validator->validNumber($sessionModel->getUserId());
$this->validator->notNull($sessionModel->getAuthToken());
$sessionMapper = new SessionMapper();

$sessionModel = $sessionMapper->read($sessionModel);

return $sessionModel;
}
}

public function delete($sessionModel)
{
if ($this->validator->notNull($sessionModel->getId())
&& $this->validator->validNumber($sessionModel->getId())
&& $this->validator->notNull($sessionModel->getUserId())
&& $this->validator->validNumber($sessionModel->getUserId())) {
$this->validator->notNull($sessionModel->getId());
$this->validator->validNumber($sessionModel->getId());
$this->validator->notNull($sessionModel->getUserId());
$this->validator->validNumber($sessionModel->getUserId());
$sessionModel->setIsExpired(1);
$sessionModel->setExpiredOn(date("Y-m-d H:i:s"));
$sessionMapper = new SessionMapper();

$sessionModel = $sessionMapper->update($sessionModel);

return $sessionModel;
}
}
}
16 changes: 8 additions & 8 deletions app/Domain/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public function create($input)
{
$userFactory = new UserFactory();
$userModel = $userFactory->create($input);
$userModel->setCreatedOn(date("Y-m-d H:i:s"));

$date = date("Y-m-d H:i:s");
$userModel->setCreatedOn($date);

$userMapper = new UserMapper();
$userModel = $userMapper->create($userModel);
return $userModel;
Expand All @@ -44,15 +47,12 @@ public function read($input)

}

public function readByUsernameandPassword($input)
public function readByUsernameandPassword($userModel)
{
$userMapper = new UserMapper();
$userModel = $userMapper->read($userModel);

$userFactory = new UserFactory();
$userModel = $userFactory->create($input);

$userMapper = new UserMapper();
$userModel = $userMapper->read($userModel);
return $userModel;
return $userModel;

}
public function update($input)
Expand Down
12 changes: 6 additions & 6 deletions app/Model/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

class Session extends Model
{
private $id;
private $userId;
private $authToken;
private $createdOn;
private $expiredOn;
private $isExpired;
protected $id;
protected $userId;
protected $authToken;
protected $createdOn;
protected $expiredOn;
protected $isExpired;

public function setId($id)
{
Expand Down
2 changes: 1 addition & 1 deletion app/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Response
private $status;
private $message;
private $version = '1.0.0';
private $data;
protected $data;
private $debugData;

public function __construct($data = null, $status = '200', $message = 'OK', $debugData = null)
Expand Down
13 changes: 9 additions & 4 deletions app/Routes/Routes.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Notes\Controller\Web;

use Notes\Controller\Api\UserTag as UserTag;
use Notes\Controller\Api\Session as Session;

use Notes\Request\Request as Request;

$application->get('/:route', function ($route) use ($application) {
Expand All @@ -13,15 +15,18 @@

$application->get('/login', function () use ($application) {
$request = new Request();
$sessionController = new Session($request);
$sessionController->get();
$loginController = new Login($request);
$loginController->get();
});

$application->post('/login', function () use ($application) {
$application->post('/api/session', function () use ($application) {
$request = $application->request();

$objRequest = new Request();
$objRequest->setUrlParams($request->post());

$objRequest->setData($request->getBody());
$objRequest->setHeaders($request->headers);
$objRequest->setCookies($request->cookies);

$sessionController = new Session($objRequest);
$sessionController->post();
Expand Down
4 changes: 2 additions & 2 deletions app/Service/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public function __construct()

}

public function login($userInput)
public function login($userModel)
{
$sessionDomain = new SessionDomain();

$session = $sessionDomain->create($userInput);
$session = $sessionDomain->create($userModel);

return $session;
}
Expand Down
4 changes: 2 additions & 2 deletions app/Service/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public function __construct()
{

}
public function create($request)
public function create($input)
{
$userDomain=new UserDomain();

$response=$userDomain->create($request);
$response=$userDomain->create($input);

return $response;

Expand Down
Loading