Fixed structure of UI, Added some logic to Auth
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.Optional;
|
||||
|
||||
public interface UserRepository {
|
||||
Optional<User> getUser(String username, String password);
|
||||
Boolean userExists(String username);
|
||||
Optional<User> saveUser(String username, String email, String password);
|
||||
boolean deleteUser(int userId);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ public class UserRepositoryJdbc implements UserRepository {
|
||||
|
||||
preparedStatement.setString(1, username);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
if (!resultSet.next()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
String passwordFromDB = resultSet.getString("password");
|
||||
String emailFromDB = resultSet.getString("email");
|
||||
@@ -43,6 +46,28 @@ public class UserRepositoryJdbc implements UserRepository {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean userExists(String username) {
|
||||
try {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||
"SELECT * FROM users WHERE name=(?);"
|
||||
);
|
||||
|
||||
preparedStatement.setString(1, username);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Optional<User> saveUser(String username, String email, String password) {
|
||||
try {
|
||||
|
||||
@@ -15,10 +15,7 @@ public class UserService {
|
||||
|
||||
public Optional<User> login(String username, String password) {
|
||||
try {
|
||||
|
||||
|
||||
|
||||
return Optional.empty();
|
||||
return userRepository.getUser(username, password);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
return Optional.empty();
|
||||
@@ -27,6 +24,10 @@ public class UserService {
|
||||
|
||||
public Optional<User> register(String username, String email, String password) {
|
||||
try {
|
||||
if (userRepository.userExists(username)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return userRepository.saveUser(username, email, password);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
28
src/main/java/ui/AuthPanel.java
Normal file
28
src/main/java/ui/AuthPanel.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package ui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class AuthPanel extends BasePanel {
|
||||
|
||||
public AuthPanel(Consumer<String> changePanel) {
|
||||
super(changePanel);
|
||||
|
||||
// Login
|
||||
JButton loginButton = new JButton("Login");
|
||||
loginButton.addActionListener(e -> {
|
||||
changePanel.accept("login");
|
||||
});
|
||||
|
||||
// Registry
|
||||
JButton registryButton = new JButton("Registry");
|
||||
registryButton.addActionListener(e -> {
|
||||
changePanel.accept("registry");
|
||||
});
|
||||
|
||||
|
||||
add(loginButton);
|
||||
add(registryButton);
|
||||
}
|
||||
}
|
||||
13
src/main/java/ui/BasePanel.java
Normal file
13
src/main/java/ui/BasePanel.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class BasePanel extends JPanel {
|
||||
protected Consumer<String> changePanel;
|
||||
|
||||
public BasePanel(Consumer<String> changePanel) {
|
||||
this.changePanel = changePanel;
|
||||
}
|
||||
}
|
||||
|
||||
30
src/main/java/ui/HomePanel.java
Normal file
30
src/main/java/ui/HomePanel.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package ui;
|
||||
|
||||
import models.User;
|
||||
import util.Session;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class HomePanel extends BasePanel {
|
||||
|
||||
public HomePanel(Consumer<String> changePanel) {
|
||||
super(changePanel);
|
||||
|
||||
User user = Session.getInstance().getUser();
|
||||
if (user == null) {
|
||||
// Log, user is not logged.
|
||||
changePanel.accept("auth");
|
||||
}
|
||||
|
||||
|
||||
JButton logoutButton = new JButton("Logout");
|
||||
logoutButton.addActionListener(e -> {
|
||||
Session.getInstance().logout();
|
||||
changePanel.accept("auth");
|
||||
});
|
||||
|
||||
add(logoutButton);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,17 +8,18 @@ import util.Session;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class LoginPanel {
|
||||
public class LoginPanel extends BasePanel {
|
||||
|
||||
public static JPanel getLoginPanel(){
|
||||
JPanel panel = new JPanel();
|
||||
public LoginPanel(Consumer<String> changePanel) {
|
||||
super(changePanel);
|
||||
|
||||
JTextField username = new JTextField("Login");
|
||||
JPasswordField password = new JPasswordField("Password");
|
||||
|
||||
panel.add(username, BorderLayout.NORTH);
|
||||
panel.add(password, BorderLayout.CENTER);
|
||||
add(username, BorderLayout.NORTH);
|
||||
add(password, BorderLayout.CENTER);
|
||||
|
||||
JButton loginButton = new JButton("Login");
|
||||
loginButton.addActionListener(e -> {
|
||||
@@ -32,18 +33,22 @@ public class LoginPanel {
|
||||
Optional<User> user = service.login(usernameText, passwordText);
|
||||
if (user.isPresent()) {
|
||||
Session.getInstance().setUser(user.get());
|
||||
changePanel.accept("home");
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(panel, "User not found");
|
||||
JOptionPane.showMessageDialog(this, "User not found");
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(panel, "Error");
|
||||
ex.printStackTrace();
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage());
|
||||
}
|
||||
|
||||
});
|
||||
add(loginButton, BorderLayout.SOUTH);
|
||||
|
||||
panel.add(loginButton, BorderLayout.SOUTH);
|
||||
|
||||
return panel;
|
||||
JButton returnButton = new JButton("Return");
|
||||
returnButton.addActionListener(e -> {
|
||||
changePanel.accept("auth");
|
||||
});
|
||||
add(returnButton, BorderLayout.EAST);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,18 @@ public class MyFrame extends JFrame {
|
||||
JPanel mainPanel = new JPanel(new CardLayout());
|
||||
|
||||
public void changePanel(String panel) {
|
||||
|
||||
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
|
||||
cardLayout.show(mainPanel, panel);
|
||||
}
|
||||
|
||||
public void init(JPanel mainPanel) {
|
||||
mainPanel.add(new AuthPanel(this::changePanel), "auth");
|
||||
mainPanel.add(new LoginPanel(this::changePanel), "login");
|
||||
mainPanel.add(new RegistryPanel(this::changePanel), "registry");
|
||||
mainPanel.add(new HomePanel(this::changePanel), "home");
|
||||
}
|
||||
|
||||
|
||||
public MyFrame() {
|
||||
super("Todo App");
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
@@ -17,26 +26,7 @@ public class MyFrame extends JFrame {
|
||||
setLocation(50, 50);
|
||||
setLayout(new CardLayout());
|
||||
|
||||
JPanel startPanel = new JPanel();
|
||||
JButton loginButton = new JButton("Login");
|
||||
JButton registryButton = new JButton("Registry");
|
||||
|
||||
startPanel.add(loginButton);
|
||||
startPanel.add(registryButton);
|
||||
mainPanel.add(startPanel, "start");
|
||||
mainPanel.add(LoginPanel.getLoginPanel(), "login");
|
||||
mainPanel.add(RegistryPanel.getRegistryPanel(), "registry");
|
||||
|
||||
loginButton.addActionListener(e -> {
|
||||
CardLayout cl = (CardLayout)mainPanel.getLayout();
|
||||
cl.show(mainPanel, "login");
|
||||
});
|
||||
|
||||
registryButton.addActionListener(e -> {
|
||||
CardLayout cl = (CardLayout)mainPanel.getLayout();
|
||||
cl.show(mainPanel, "registry");
|
||||
});
|
||||
|
||||
init(mainPanel);
|
||||
add(mainPanel);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@@ -8,22 +8,23 @@ import util.Session;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class RegistryPanel {
|
||||
public class RegistryPanel extends BasePanel {
|
||||
|
||||
public static JPanel getRegistryPanel(){
|
||||
JPanel panel = new JPanel();
|
||||
public RegistryPanel(Consumer<String> changePanel){
|
||||
super(changePanel);
|
||||
|
||||
JTextField username = new JTextField("Login");
|
||||
JTextField email = new JTextField("email@email.com");
|
||||
JPasswordField password = new JPasswordField("Password");
|
||||
|
||||
panel.add(username, BorderLayout.NORTH);
|
||||
panel.add(email, BorderLayout.NORTH);
|
||||
panel.add(password, BorderLayout.CENTER);
|
||||
add(username, BorderLayout.NORTH);
|
||||
add(email, BorderLayout.NORTH);
|
||||
add(password, BorderLayout.CENTER);
|
||||
|
||||
JButton loginButton = new JButton("Registry");
|
||||
loginButton.addActionListener(e -> {
|
||||
JButton registryButton = new JButton("Registry");
|
||||
registryButton.addActionListener(e -> {
|
||||
try {
|
||||
String usernameText = username.getText();
|
||||
String emailText = email.getText();
|
||||
@@ -35,21 +36,24 @@ public class RegistryPanel {
|
||||
Optional<User> user = service.register(usernameText, emailText, passwordText);
|
||||
if (user.isPresent()) {
|
||||
Session.getInstance().setUser(user.get());
|
||||
changePanel.accept("home");
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(panel, "User not found");
|
||||
JOptionPane.showMessageDialog(this, "User not found");
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
JOptionPane.showMessageDialog(panel, "Error");
|
||||
ex.printStackTrace();
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage());
|
||||
}
|
||||
|
||||
});
|
||||
add(registryButton, BorderLayout.SOUTH);
|
||||
|
||||
panel.add(loginButton, BorderLayout.SOUTH);
|
||||
|
||||
return panel;
|
||||
JButton returnButton = new JButton("Return");
|
||||
returnButton.addActionListener(e -> {
|
||||
changePanel.accept("auth");
|
||||
});
|
||||
add(returnButton, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user