From 457baf6015db554179da905cd63d9ff951552fdd Mon Sep 17 00:00:00 2001 From: Seun Matt Date: Mon, 18 Dec 2017 20:22:31 +0100 Subject: [PATCH] update pom and reformat code --- vraptor/pom.xml | 2 +- .../baeldung/controllers/AuthController.java | 67 ++++++++++--------- .../baeldung/controllers/IndexController.java | 6 +- .../baeldung/controllers/PostController.java | 32 ++++----- .../main/java/com/baeldung/daos/PostDao.java | 38 +++++------ .../main/java/com/baeldung/daos/UserDao.java | 1 - 6 files changed, 75 insertions(+), 71 deletions(-) diff --git a/vraptor/pom.xml b/vraptor/pom.xml index 1f094a3465..49b0922ddd 100644 --- a/vraptor/pom.xml +++ b/vraptor/pom.xml @@ -17,7 +17,7 @@ br.com.caelum vraptor - 4.2.0-RC3 + 4.2.0.Final diff --git a/vraptor/src/main/java/com/baeldung/controllers/AuthController.java b/vraptor/src/main/java/com/baeldung/controllers/AuthController.java index 746f3b0edc..ed2d3fdba7 100644 --- a/vraptor/src/main/java/com/baeldung/controllers/AuthController.java +++ b/vraptor/src/main/java/com/baeldung/controllers/AuthController.java @@ -42,30 +42,32 @@ public class AuthController { @Post("/register") public void register(User user, HttpServletRequest request) { - validator.validate(user); + validator.validate(user); - if(validator.hasErrors()) { - result.include("errors", validator.getErrors()); - } + if(validator.hasErrors()) { + result.include("errors", validator.getErrors()); + } - validator.onErrorRedirectTo(this).registrationForm(); + validator.onErrorRedirectTo(this).registrationForm(); - if(!user.getPassword().equals(request.getParameter("password_confirmation"))) { - result.include("error", "Passwords Do Not Match"); - result.redirectTo(this).registrationForm(); - } + if(!user.getPassword() + .equals(request.getParameter("password_confirmation"))) { + result.include("error", "Passwords Do Not Match"); + result.redirectTo(this).registrationForm(); + } - user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt())); + user.setPassword( + BCrypt.hashpw(user.getPassword(), BCrypt.gensalt())); - Object resp = userDao.add(user); + Object resp = userDao.add(user); - if(resp != null) { - result.include("status", "Registration Successful! Now Login"); - result.redirectTo(this).loginForm(); - } else { - result.include("error", "There was an error during registration"); - result.redirectTo(this).registrationForm(); - } + if(resp != null) { + result.include("status", "Registration Successful! Now Login"); + result.redirectTo(this).loginForm(); + } else { + result.include("error", "There was an error during registration"); + result.redirectTo(this).registrationForm(); + } } @Get("/login") @@ -76,19 +78,22 @@ public class AuthController { @Post("/login") public void login(HttpServletRequest request) { - if (request.getParameter("user.email").isEmpty() || request.getParameter("user.password").isEmpty()) { - result.include("error", "Email/Password is Required!"); - result.redirectTo(AuthController.class).loginForm(); - } + String password = request.getParameter("user.password"); + String email = request.getParameter("user.email"); - User user = userDao.findByEmail(request.getParameter("user.email")); - if (Objects.nonNull(user) && BCrypt.checkpw(request.getParameter("user.password"), user.getPassword())) { - userInfo.setUser(user); - result.include("status", "Login Successful!"); - result.redirectTo(IndexController.class).index(); - } else { - result.include("error", "Email/Password Does Not Match!"); - result.redirectTo(AuthController.class).loginForm(); - } + if(email.isEmpty() || password.isEmpty()) { + result.include("error", "Email/Password is Required!"); + result.redirectTo(AuthController.class).loginForm(); + } + + User user = userDao.findByEmail(email); + if(user != null && BCrypt.checkpw(password, user.getPassword())) { + userInfo.setUser(user); + result.include("status", "Login Successful!"); + result.redirectTo(IndexController.class).index(); + } else { + result.include("error", "Email/Password Does Not Match!"); + result.redirectTo(AuthController.class).loginForm(); + } } } diff --git a/vraptor/src/main/java/com/baeldung/controllers/IndexController.java b/vraptor/src/main/java/com/baeldung/controllers/IndexController.java index 2e3f1f1126..cca34f5447 100644 --- a/vraptor/src/main/java/com/baeldung/controllers/IndexController.java +++ b/vraptor/src/main/java/com/baeldung/controllers/IndexController.java @@ -23,14 +23,14 @@ public class IndexController { @Inject public IndexController(Result result, PostDao postDao) { - this.result = result; - this.postDao = postDao; + this.result = result; + this.postDao = postDao; } @Path("/") public void index() { result.include("posts", postDao.all()); - result.use(FreemarkerView.class).withTemplate("index"); + result.use(FreemarkerView.class).withTemplate("index"); } diff --git a/vraptor/src/main/java/com/baeldung/controllers/PostController.java b/vraptor/src/main/java/com/baeldung/controllers/PostController.java index 655b248350..979d0c28e2 100644 --- a/vraptor/src/main/java/com/baeldung/controllers/PostController.java +++ b/vraptor/src/main/java/com/baeldung/controllers/PostController.java @@ -48,28 +48,28 @@ public class PostController { @br.com.caelum.vraptor.Post("/post/add") public void add(Post post) { + post.setAuthor(userInfo.getUser()); + validator.validate(post); + if(validator.hasErrors()) + result.include("errors", validator.getErrors()); + validator.onErrorRedirectTo(this).addForm(); - post.setAuthor(userInfo.getUser()); - validator.validate(post); - if(validator.hasErrors()) - result.include("errors", validator.getErrors()); - validator.onErrorRedirectTo(this).addForm(); + Object id = postDao.add(post); - Object id = postDao.add(post); - - if(Objects.nonNull(id)) { - result.include("status", "Post Added Successfully"); - result.redirectTo(IndexController.class).index(); - } else { - result.include("error", "There was an error creating the post. Try Again"); - result.redirectTo(this).addForm(); - } + if(Objects.nonNull(id)) { + result.include("status", "Post Added Successfully"); + result.redirectTo(IndexController.class).index(); + } else { + result.include( + "error", "There was an error creating the post. Try Again"); + result.redirectTo(this).addForm(); + } } @Get("/posts/{id}") public void view(int id) { - result.include("post", postDao.findById(id)); - result.use(FreemarkerView.class).withTemplate("view"); + result.include("post", postDao.findById(id)); + result.use(FreemarkerView.class).withTemplate("view"); } diff --git a/vraptor/src/main/java/com/baeldung/daos/PostDao.java b/vraptor/src/main/java/com/baeldung/daos/PostDao.java index d4a8af15b8..e9a62b5b5e 100644 --- a/vraptor/src/main/java/com/baeldung/daos/PostDao.java +++ b/vraptor/src/main/java/com/baeldung/daos/PostDao.java @@ -20,35 +20,35 @@ public class PostDao { @Inject public PostDao(SessionFactory sessionFactory) { - this.sessionFactory = sessionFactory; + this.sessionFactory = sessionFactory; } public Object add(Post post) { - Session session = sessionFactory.openSession(); - session.beginTransaction(); - Object id = session.save(post); - session.getTransaction().commit(); - session.close(); - return id; + Session session = sessionFactory.openSession(); + session.beginTransaction(); + Object id = session.save(post); + session.getTransaction().commit(); + session.close(); + return id; } public Post findById(int id) { - Session session = sessionFactory.openSession(); - session.beginTransaction(); - Post post = (Post) session.get(Post.class, id); - session.getTransaction().commit(); - session.close(); - return post; + Session session = sessionFactory.openSession(); + session.beginTransaction(); + Post post = (Post) session.get(Post.class, id); + session.getTransaction().commit(); + session.close(); + return post; } public List all() { - Session session = sessionFactory.openSession(); - session.beginTransaction(); - List posts = (List) session.createQuery("FROM Post p").list(); - session.getTransaction().commit(); - session.close(); - return posts; + Session session = sessionFactory.openSession(); + session.beginTransaction(); + List posts = (List) session.createQuery("FROM Post p").list(); + session.getTransaction().commit(); + session.close(); + return posts; } } diff --git a/vraptor/src/main/java/com/baeldung/daos/UserDao.java b/vraptor/src/main/java/com/baeldung/daos/UserDao.java index 5e63ff9b26..6af1a49d25 100644 --- a/vraptor/src/main/java/com/baeldung/daos/UserDao.java +++ b/vraptor/src/main/java/com/baeldung/daos/UserDao.java @@ -16,7 +16,6 @@ import java.util.logging.Logger; public class UserDao { SessionFactory sessionFactory; - Logger logger = Logger.getLogger(getClass().getName()); public UserDao() { this(null);