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

Joshua ramos #16

Merged
merged 2 commits into from
Feb 16, 2023
Merged
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
29 changes: 22 additions & 7 deletions src/main/java/com/codeup/adlister/controllers/RegisterServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;

@WebServlet(name = "controllers.RegisterServlet", urlPatterns = "/register")
Expand All @@ -23,19 +24,33 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
String passwordConfirmation = request.getParameter("confirm_password");

// validate input
boolean userNameExists = (!username.equals(request.getParameter(username)));

boolean inputHasErrors = username.isEmpty()
|| email.isEmpty()
|| password.isEmpty()
|| (! password.equals(passwordConfirmation));

if (inputHasErrors) {
if (userNameExists){
JOptionPane.showMessageDialog(null,"Please choose a new username that one exists already");
request.setAttribute("userNameExists", true);
response.sendRedirect("/register");
return;
}

// create and save a new user
User user = new User(username, email, password);
DaoFactory.getUsersDao().insert(user);
response.sendRedirect("/login");
}else if (inputHasErrors) {
JOptionPane.showMessageDialog(null,"Passwords do not match");
request.setAttribute("inputHasErrors", true);
String message = "Passwords did not match";
request.setAttribute("message", message);
response.sendRedirect("/register");
// return;

}else{
User user = new User(username, email, password);
DaoFactory.getUsersDao().insert(user);
response.sendRedirect("/login");
}
// User user = new User(username, email, password);
// DaoFactory.getUsersDao().insert(user);
// response.sendRedirect("/login");
}
}