update pom and reformat code
This commit is contained in:
parent
deeb2b20f3
commit
457baf6015
@ -17,7 +17,7 @@
|
||||
<dependency>
|
||||
<groupId>br.com.caelum</groupId>
|
||||
<artifactId>vraptor</artifactId>
|
||||
<version>4.2.0-RC3</version>
|
||||
<version>4.2.0.Final</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<Post> all() {
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
List<Post> posts = (List<Post>) session.createQuery("FROM Post p").list();
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
return posts;
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
List<Post> posts = (List<Post>) session.createQuery("FROM Post p").list();
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
return posts;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ import java.util.logging.Logger;
|
||||
public class UserDao {
|
||||
|
||||
SessionFactory sessionFactory;
|
||||
Logger logger = Logger.getLogger(getClass().getName());
|
||||
|
||||
public UserDao() {
|
||||
this(null);
|
||||
|
Loading…
x
Reference in New Issue
Block a user