Fixed session bug, updated database, created show todos.
This commit is contained in:
@@ -176,10 +176,12 @@ public class TaskRepositoryJdbc implements TaskRepository {
|
||||
public Optional<Task> save(TaskCreateDto taskCreateDto) {
|
||||
try {
|
||||
User user = Session.getInstance().getUser();
|
||||
if (user == null) return Optional.empty();
|
||||
if (user == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||
"INSERT INTO tasks (title, description, status, deadline) VALUES (?, ?, ?, ?) RETURNING id, created_at"
|
||||
"INSERT INTO tasks (title, description, status, deadline, user_id) VALUES (?, ?, ?, ?, ?) RETURNING id, created_at"
|
||||
);
|
||||
|
||||
preparedStatement.setString(1, taskCreateDto.title());
|
||||
@@ -192,10 +194,9 @@ public class TaskRepositoryJdbc implements TaskRepository {
|
||||
} else {
|
||||
preparedStatement.setNull(4, java.sql.Types.DATE);
|
||||
}
|
||||
preparedStatement.setInt(5, user.id());
|
||||
|
||||
System.out.println(preparedStatement);
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
System.out.println(resultSet);
|
||||
|
||||
if (resultSet.next()) {
|
||||
System.out.println(resultSet.getString("id"));
|
||||
@@ -281,10 +282,10 @@ public class TaskRepositoryJdbc implements TaskRepository {
|
||||
int affected = preparedStatement.executeUpdate();
|
||||
|
||||
if (affected > 0) {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
return false;
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
|
||||
@@ -17,6 +17,16 @@ public class TaskService {
|
||||
this.taskRepository = taskRepository;
|
||||
}
|
||||
|
||||
public ArrayList<Task> getTasks() {
|
||||
try {
|
||||
return taskRepository.getUserTasks();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Task> getUserTasksFilterByDate(Date startDate, Date endDate) {
|
||||
try {
|
||||
return taskRepository.getUserTasks(startDate, endDate);
|
||||
|
||||
@@ -51,6 +51,13 @@ public class CreateTodoPanel extends BasePanel {
|
||||
JButton createButton = new JButton("Create");
|
||||
createButton.addActionListener(e -> {
|
||||
try {
|
||||
User user = Session.getInstance().getUser();
|
||||
if (user == null) {
|
||||
JOptionPane.showMessageDialog(this, "You must be logged in");
|
||||
changePanel.accept("auth");
|
||||
return;
|
||||
}
|
||||
|
||||
String titleText = todoTitle.getText();
|
||||
String descText = todoDescription.getText();
|
||||
String selectedStatus = (String) statusCombo.getSelectedItem();
|
||||
@@ -82,7 +89,7 @@ public class CreateTodoPanel extends BasePanel {
|
||||
todoTitle.setText("");
|
||||
todoDescription.setText("");
|
||||
statusCombo.setSelectedItem("NEW");
|
||||
todoDate.setValue("");
|
||||
todoDate.setValue(null);
|
||||
|
||||
JOptionPane.showMessageDialog(this, "Task created");
|
||||
} else {
|
||||
|
||||
@@ -12,12 +12,6 @@ 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();
|
||||
@@ -26,24 +20,25 @@ public class HomePanel extends BasePanel {
|
||||
|
||||
JButton addTodoButton = new JButton("Create Todo");
|
||||
addTodoButton.addActionListener(e -> {
|
||||
Session.getInstance().logout();
|
||||
User user = Session.getInstance().getUser();
|
||||
if (user == null) {
|
||||
JOptionPane.showMessageDialog(this, "You must be logged in");
|
||||
changePanel.accept("auth");
|
||||
return;
|
||||
}
|
||||
|
||||
changePanel.accept("create_todo");
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
// Wyświetlenie wszystkich tasków tylko dzisiejszych tasków
|
||||
// Umożliwienie utworzenia nowych tasków.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JButton showTodoButton = new JButton("Show all Todo");
|
||||
showTodoButton.addActionListener(e -> {
|
||||
changePanel.accept("show_all_todo");
|
||||
});
|
||||
|
||||
|
||||
add(logoutButton);
|
||||
add(addTodoButton, BorderLayout.NORTH);
|
||||
add(showTodoButton, BorderLayout.NORTH);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,9 @@ public class LoginPanel extends BasePanel {
|
||||
|
||||
Optional<User> user = service.login(usernameText, passwordText);
|
||||
if (user.isPresent()) {
|
||||
Session.getInstance().setUser(user.get());
|
||||
Session session = Session.getInstance();
|
||||
session.setUser(user.get());
|
||||
|
||||
changePanel.accept("home");
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "User not found");
|
||||
|
||||
@@ -17,6 +17,7 @@ public class MyFrame extends JFrame {
|
||||
mainPanel.add(new RegistryPanel(this::changePanel), "registry");
|
||||
mainPanel.add(new HomePanel(this::changePanel), "home");
|
||||
mainPanel.add(new CreateTodoPanel(this::changePanel), "create_todo");
|
||||
mainPanel.add(new ShowTodosPanel(this::changePanel), "show_all_todo");
|
||||
}
|
||||
|
||||
public MyFrame() {
|
||||
|
||||
82
src/main/java/ui/ShowTodosPanel.java
Normal file
82
src/main/java/ui/ShowTodosPanel.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package ui;
|
||||
|
||||
import models.Task;
|
||||
import models.User;
|
||||
import repository.TaskRepository;
|
||||
import service.TaskService;
|
||||
import util.Session;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ShowTodosPanel extends BasePanel {
|
||||
private JPanel tasksContainer;
|
||||
|
||||
public ShowTodosPanel(Consumer<String> changePanel) {
|
||||
super(changePanel);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
JButton showTodosButton = new JButton("Refresh");
|
||||
showTodosButton.addActionListener(e -> {
|
||||
try {
|
||||
User user = Session.getInstance().getUser();
|
||||
if (user == null) {
|
||||
JOptionPane.showMessageDialog(this, "You must be logged in");
|
||||
changePanel.accept("auth");
|
||||
return;
|
||||
}
|
||||
|
||||
TaskRepository taskRepository = Session.getTaskRepository();
|
||||
TaskService service = new TaskService(taskRepository);
|
||||
|
||||
ArrayList<Task> tasks = service.getTasks();
|
||||
|
||||
tasksContainer.removeAll();
|
||||
for (Task task : tasks) {
|
||||
JPanel taskPanel = new JPanel(new BorderLayout());
|
||||
|
||||
JLabel titleLabel = new JLabel(task.getTitle());
|
||||
JLabel statusLabel = new JLabel(task.getStatus().name());
|
||||
|
||||
taskPanel.add(titleLabel, BorderLayout.WEST);
|
||||
taskPanel.add(statusLabel, BorderLayout.EAST);
|
||||
|
||||
taskPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));
|
||||
|
||||
tasksContainer.add(taskPanel);
|
||||
}
|
||||
|
||||
tasksContainer.revalidate();
|
||||
tasksContainer.repaint();
|
||||
|
||||
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
JButton returnButton = new JButton("Return");
|
||||
returnButton.addActionListener(e -> {
|
||||
changePanel.accept("home");
|
||||
});
|
||||
|
||||
|
||||
tasksContainer = new JPanel();
|
||||
tasksContainer.setLayout(new BoxLayout(tasksContainer, BoxLayout.Y_AXIS));
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane(tasksContainer);
|
||||
|
||||
JPanel topPanel = new JPanel();
|
||||
|
||||
topPanel.add(showTodosButton);
|
||||
topPanel.add(returnButton);
|
||||
|
||||
add(topPanel, BorderLayout.NORTH);
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,10 @@ public class Session {
|
||||
|
||||
public static Session getInstance() {
|
||||
if (instance == null) {
|
||||
System.out.println("Tworzymy sesje");
|
||||
instance = new Session();
|
||||
}
|
||||
System.out.println("Zwracamy sesje");
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ALTER TABLE tasks ALTER COLUMN deadline TYPE DATE;
|
||||
Reference in New Issue
Block a user