diff --git a/app/Controller/Web/Session.php b/app/Controller/Api/Session.php old mode 100644 new mode 100755 similarity index 53% rename from app/Controller/Web/Session.php rename to app/Controller/Api/Session.php index 342a99f1..bcad8c07 --- a/app/Controller/Web/Session.php +++ b/app/Controller/Api/Session.php @@ -1,9 +1,10 @@ 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(); } } } diff --git a/app/Controller/Web/Login.php b/app/Controller/Web/Login.php new file mode 100755 index 00000000..babfe4ac --- /dev/null +++ b/app/Controller/Web/Login.php @@ -0,0 +1,20 @@ +request = $request; + $this->view = new View(); + } + public function get() + { + $this->view->render("Login.php"); + } +} diff --git a/app/Controller/Web/User.php b/app/Controller/Web/User.php index cfe52dcd..de0b54a6 100644 --- a/app/Controller/Web/User.php +++ b/app/Controller/Web/User.php @@ -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; @@ -23,6 +24,7 @@ public function get() public function post() { $input = $this->request->getUrlParams(); + $userService = new UserService(); try { $response = $userService->create($input); diff --git a/app/Domain/Session.php b/app/Domain/Session.php index 32d8a871..4a7208f5 100755 --- a/app/Domain/Session.php +++ b/app/Domain/Session.php @@ -11,6 +11,8 @@ use Notes\Validator\InputValidator as InputValidator; +use Notes\Factory\User as UserFactory; + class Session { public function __construct() @@ -18,61 +20,59 @@ 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(); @@ -80,6 +80,5 @@ public function delete($sessionModel) $sessionModel = $sessionMapper->update($sessionModel); return $sessionModel; - } } } diff --git a/app/Domain/User.php b/app/Domain/User.php index c803876c..01e62c21 100755 --- a/app/Domain/User.php +++ b/app/Domain/User.php @@ -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; @@ -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) diff --git a/app/Model/Session.php b/app/Model/Session.php index ce121a46..84c9d831 100755 --- a/app/Model/Session.php +++ b/app/Model/Session.php @@ -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) { diff --git a/app/Response/Response.php b/app/Response/Response.php index 396b8819..a88179fa 100644 --- a/app/Response/Response.php +++ b/app/Response/Response.php @@ -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) diff --git a/app/Routes/Routes.php b/app/Routes/Routes.php old mode 100644 new mode 100755 index e70c14c0..58df5d70 --- a/app/Routes/Routes.php +++ b/app/Routes/Routes.php @@ -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) { @@ -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(); diff --git a/app/Service/Session.php b/app/Service/Session.php index e5fcf804..bef98054 100644 --- a/app/Service/Session.php +++ b/app/Service/Session.php @@ -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; } diff --git a/app/Service/User.php b/app/Service/User.php index 359cb28a..2b43b72f 100755 --- a/app/Service/User.php +++ b/app/Service/User.php @@ -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; diff --git a/app/View/Create.php b/app/View/Create.php index 8a0c1aad..3b76dc1b 100644 --- a/app/View/Create.php +++ b/app/View/Create.php @@ -3,197 +3,39 @@ - - + + + + + + + + + - - -
+

+
Create Note :
-
- +
-
    - +
@@ -203,11 +45,14 @@ function getUserId() {
- +
-
+ + + + diff --git a/app/View/Login.php b/app/View/Login.php index 3ee3cbfc..c4268c20 100644 --- a/app/View/Login.php +++ b/app/View/Login.php @@ -10,32 +10,53 @@ .error { } + + + + + + + + + + + + -
+
+ - + - + + + - + ?> - + - - + +
Email:
Password:
+ + +
Error :-

- +
diff --git a/app/View/Notes.php b/app/View/Notes.php index bfa6f972..5497b06f 100644 --- a/app/View/Notes.php +++ b/app/View/Notes.php @@ -6,12 +6,19 @@ + + + + - - Create - Logout + + + + diff --git a/public/CSS/style.css b/public/css/style.css similarity index 87% rename from public/CSS/style.css rename to public/css/style.css index be67a470..55131cb4 100644 --- a/public/CSS/style.css +++ b/public/css/style.css @@ -61,14 +61,9 @@ } .inputTags { - list-style:none; - display: block; - width: 100px; - margin-bottom: 3px; - margin-left: 10px; - border-left: 10px solid #1958b7; - border-right: 10px solid #508fc4; - background-color: #2175bc; - color: #fff; - text-decoration: none; -} + width: 120px; + height: 20px; + overflow: hidden; + background: no-repeat right #ddd; + border: 1px solid #ccc; +} \ No newline at end of file diff --git a/public/js/Controller/LoginController.js b/public/js/Controller/LoginController.js new file mode 100755 index 00000000..476e37e6 --- /dev/null +++ b/public/js/Controller/LoginController.js @@ -0,0 +1,55 @@ +var utils = { + post: function(url, request, isAsync, onSuccess, onFailure) { + var data = JSON.stringify(request); + + var xhr = new XMLHttpRequest(); + xhr.open('POST', url, isAsync); + xhr.setRequestHeader('Content-Type', 'application/json'); + xhr.send(data); + + xhr.onreadystatechange = function() { + + if (xhr.readyState == 4 && xhr.status == 200) { + var response = JSON.parse(xhr.responseText); + console.log("OnXhr Success Response: ", response); + if (response.status == 0) { + onFailure(response); + } else { + onSuccess(response); + } + } + } + }, + get: function(url, request, isSync, onSuccess, onFailure) { + + } +} + +var loginController = { + loginView: null, + init: function() { + this.loginView = new LoginView(function(e) { + loginController.loginView.resetData(); + }, function(e) { + //read data from View + var userModel = loginController.loginView.readUserData(); + + //call api + utils.post('/api/session', userModel, true, function(response) { + loginController.loginView.hide(response); + console.log("OnSuccess Response:", response); + //transfer control to notes controller + notesController.init(); + }, function(response) { + console.log("OnFailure Response:", response); + loginController.loginView.showError(response); + }); + + }); + this.loginView.show(); + } +} + +$(function() { + loginController.init(); +}) \ No newline at end of file diff --git a/public/js/Controller/NotesController.js b/public/js/Controller/NotesController.js new file mode 100755 index 00000000..b2a5b1c4 --- /dev/null +++ b/public/js/Controller/NotesController.js @@ -0,0 +1,8 @@ +var notesController = { + notesView: null, + init: function() { + + this.notesView = new NotesView(); + this.notesView.show() + } +} \ No newline at end of file diff --git a/public/js/View/LoginView.js b/public/js/View/LoginView.js new file mode 100755 index 00000000..5c2e984a --- /dev/null +++ b/public/js/View/LoginView.js @@ -0,0 +1,62 @@ +function LoginView(resetClickedHandler, loginClickedHandler) { + + this.resetData = function() { + document.getElementById('email').value = ""; + document.getElementById('password').value = ""; + }; + + this.readUserData = function() { + var id = document.getElementById('id').value; + var firstName = document.getElementById('firstName').value; + var lastName = document.getElementById('lastName').value; + var createdOn = document.getElementById('createdOn').value; + var email = document.getElementById('email').value; + console.log(email); + + var password = document.getElementById('password').value; + console.log(password); + + return { + id: id, + firstName: firstName, + lastName: lastName, + password: password, + email: email, + createdOn: createdOn + }; + }; + + this.setLoginClickedHandler = function(handler) { + console.log("In onLoginClickedHandler..."); + var loginButton = document.getElementById('login'); + + loginButton.addEventListener("click", function(e) { + handler(e); + }, false); + }; + + this.setResetClickedHandler = function(handler) { + console.log("In onLogin Reset Clicked Handler.."); + var resetButton = document.getElementById('reset'); + resetButton.addEventListener("click", function(e) { + handler(e); + }, false); + }; + + this.hide = function() { + document.getElementById('container').style.display = "none"; + }; + + this.showError = function(response) { + var errorMessage = response.data; + alert(errorMessage); + }; + + this.show=function(){ + + }; + + this.setLoginClickedHandler(loginClickedHandler); + this.setResetClickedHandler(resetClickedHandler); + +} \ No newline at end of file diff --git a/public/js/View/NotesView.js b/public/js/View/NotesView.js new file mode 100755 index 00000000..f5a9dd08 --- /dev/null +++ b/public/js/View/NotesView.js @@ -0,0 +1,6 @@ +function NotesView() { + this.show = function() { + console.log("In notes View"); + window.location.href = './notes'; + } +} diff --git a/tests/Domain/SessionTest.php b/tests/Domain/SessionTest.php index fe307c98..106efa18 100755 --- a/tests/Domain/SessionTest.php +++ b/tests/Domain/SessionTest.php @@ -47,10 +47,14 @@ public function it_should_create_session_with_valid_email_password() 'password' => 'Pushpa@123' ); + $userModel = new UserModel(); + + $userModel->setEmail($userInput['email']); + $userModel->setPassword($userInput['password']); $sessionModel = new sessionModel(); $sessionDomain = new Session(); - $sessionModel = $sessionDomain->create($userInput); + $sessionModel = $sessionDomain->create($userModel); $this->assertEquals(4, $sessionModel->getId()); $this->assertEquals(3, $sessionModel->getUserId()); @@ -196,7 +200,10 @@ public function it_should_throw_exception_with_invalid_email_password() 'email' => 'abcd@gmail.com', 'password' => 'Joy%hj5487' ); - + $userModel = new UserModel(); + + $userModel->setEmail($userInput['email']); + $userModel->setPassword($userInput['password']); $input = array( 'authToken' => 'pqr', @@ -210,6 +217,6 @@ public function it_should_throw_exception_with_invalid_email_password() $sessionDomain = new Session(); - $sessionModel = $sessionDomain->create($userInput, $sessionModel); + $sessionModel = $sessionDomain->create($userModel, $sessionModel); } } diff --git a/tests/Domain/UserTest.php b/tests/Domain/UserTest.php index 62fa8e1b..106dbad4 100755 --- a/tests/Domain/UserTest.php +++ b/tests/Domain/UserTest.php @@ -48,8 +48,6 @@ public function testCanInsertRecord() 'email' => 'kirti.6@gmail.com', 'password' => 'abc@$#A123' ); - $userFactory = new UserFactory(); - $userModel = $userFactory->create($input); $userDomain = new User(); $userModel = $userDomain->create($input); @@ -114,7 +112,7 @@ public function testCanReadRecordByUsernameAndPassword() $userFactory = new UserFactory(); $userModel = $userFactory->create($input); $userDomain = new User(); - $userModel = $userDomain->readByUsernameandPassword($input); + $userModel = $userDomain->readByUsernameandPassword($userModel); $expectedDataSet = $this->createXmlDataSet(dirname(__FILE__) . '/_files/user_seed.xml'); $actualDataSet = $this->getConnection()->createDataSet(array( 'Users' @@ -147,7 +145,7 @@ public function testUserCanThrowModelNotFoundExceptionWhenUserNamePasswordDoesNo $userModel = $userFactory->create($input); $userDomain = new User(); - $userModel = $userDomain->readByUsernameandPassword($input); + $userModel = $userDomain->readByUsernameandPassword($userModel); } diff --git a/tests/Routes/RoutesTest.php b/tests/Routes/RoutesTest.php old mode 100644 new mode 100755 index d5fcad4e..f71892bd --- a/tests/Routes/RoutesTest.php +++ b/tests/Routes/RoutesTest.php @@ -76,7 +76,6 @@ public function user_registration_failed_and_should_stay_on_same_page() 'password' => "AFd@756htrfj"); $client->post('/register' ,$parameters); - $this->assertEquals(200, $client->response->status()); } @@ -100,12 +99,23 @@ public function loginpage_should_be_loaded() public function loginpage_should_be_loaded_after_user_logged_in() { $client=new ClientRequest(); - $parameters = array('email' => "William@Edwards.com", - 'password' => "AFd@756htrfj"); - $client->post('/login', $parameters); + $parameters = array( + 'id' => null, + 'firstName' =>null, + 'lastName' => null, + 'email' => "William@Edwards.com", + 'password' => "AFd@756htrfj", + 'createdOn' => null + ); + $data = json_encode($parameters); + $client->post('/api/session', $data); + + $response = json_decode($client->response->body(), true); + + $this->assertEquals(1, $response['status']); + $this->assertEquals("SUCCESS", $response['message']); - $this->assertEquals(302, $client->response->status()); - $this->assertEquals('/notes', $client->response->getIterator()["Location"]); + $this->assertEquals(200, $client->response->status()); } /** @@ -115,10 +125,23 @@ public function loginpage_should_be_loaded_after_user_logged_in() public function user_login_failed_and_should_stay_on_same_page() { $client=new ClientRequest(); - $parameters = array('email' => "Joy@Edwards.com", - 'password' => "AFd756htrfj"); + $parameters = array( + 'id' => null, + 'firstName' =>null, + 'lastName' => null, + 'email' => "Joy@Edwards.com", + 'password' => "AFd756htrfj", + 'createdOn' => null + ); + + $data = json_encode($parameters); + $client->post('/api/session', $data); + + $response = json_decode($client->response->body(), true); + + $this->assertEquals(0, $response['status']); + $this->assertEquals("FAILURE", $response['message']); - $client->post('/login', $parameters); $this->assertEquals(200, $client->response->status()); } } diff --git a/tests/Service/SessionTest.php b/tests/Service/SessionTest.php index ee37308d..f68750c8 100644 --- a/tests/Service/SessionTest.php +++ b/tests/Service/SessionTest.php @@ -56,7 +56,7 @@ public function it_should_login_with_valid_email_password() $sessionModel = new sessionModel(); $sessionService = new Session(); - $sessionModel = $sessionService->login($userInput); + $sessionModel = $sessionService->login($userModel); $this->assertEquals(4, $sessionModel->getId()); $this->assertEquals(3, $sessionModel->getUserId()); @@ -218,6 +218,6 @@ public function it_should_throw_exception_with_invalid_email_password() $userModel->setPassword($userInput['password']); $sessionService = new Session(); - $sessionModel = $sessionService->login($userInput); + $sessionModel = $sessionService->login($userModel); } } diff --git a/tests/View/LoginTest.php b/tests/View/LoginTest.php index 752db79c..be7e3913 100644 --- a/tests/View/LoginTest.php +++ b/tests/View/LoginTest.php @@ -30,7 +30,8 @@ public function response_data_should_access_in_login_View() $dom->loadHTML($output); - $element = $dom->getElementsByTagName('div'); + $element = $dom->getElementsByTagName('p'); + $this->assertEquals("Invalid Email", $element->item(0)->textContent); } public function tearDown()