Fixed session bug, updated database, created show todos.

This commit is contained in:
maciejrusek
2026-05-06 22:22:15 +02:00
parent 89a76744a2
commit 45b08b310f
10 changed files with 129 additions and 26 deletions

View File

@@ -23,4 +23,6 @@ mvn clean flyway:migrate -Dflyway.user= -Dflyway.password= -Dflyway.url=
- [x] Fabryka połączenia do bazy danych - [x] Fabryka połączenia do bazy danych
- [x] Testy bazy - [x] Testy bazy
- [x] Zmienić to na h2 w testach unit... - [x] Zmienić to na h2 w testach unit...
- [x] Dodać jakąs migracje danych, proste 2 tabelki jako 2 migracje. - [x] Dodać jakąs migracje danych, proste 2 tabelki jako 2 migracje.
- [x] Jest Bug podczas tworzenia task ze usera nie ma.... jakos
- [x] Zrobić aby deadline był wyłącznie datą.

View File

@@ -176,10 +176,12 @@ public class TaskRepositoryJdbc implements TaskRepository {
public Optional<Task> save(TaskCreateDto taskCreateDto) { public Optional<Task> save(TaskCreateDto taskCreateDto) {
try { try {
User user = Session.getInstance().getUser(); User user = Session.getInstance().getUser();
if (user == null) return Optional.empty(); if (user == null) {
return Optional.empty();
}
PreparedStatement preparedStatement = connection.prepareStatement( 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()); preparedStatement.setString(1, taskCreateDto.title());
@@ -192,10 +194,9 @@ public class TaskRepositoryJdbc implements TaskRepository {
} else { } else {
preparedStatement.setNull(4, java.sql.Types.DATE); preparedStatement.setNull(4, java.sql.Types.DATE);
} }
preparedStatement.setInt(5, user.id());
System.out.println(preparedStatement);
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
System.out.println(resultSet);
if (resultSet.next()) { if (resultSet.next()) {
System.out.println(resultSet.getString("id")); System.out.println(resultSet.getString("id"));
@@ -281,10 +282,10 @@ public class TaskRepositoryJdbc implements TaskRepository {
int affected = preparedStatement.executeUpdate(); int affected = preparedStatement.executeUpdate();
if (affected > 0) { if (affected > 0) {
return false; return true;
} }
return true; return false;
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
return false; return false;

View File

@@ -17,6 +17,16 @@ public class TaskService {
this.taskRepository = taskRepository; 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) { public ArrayList<Task> getUserTasksFilterByDate(Date startDate, Date endDate) {
try { try {
return taskRepository.getUserTasks(startDate, endDate); return taskRepository.getUserTasks(startDate, endDate);

View File

@@ -51,6 +51,13 @@ public class CreateTodoPanel extends BasePanel {
JButton createButton = new JButton("Create"); JButton createButton = new JButton("Create");
createButton.addActionListener(e -> { createButton.addActionListener(e -> {
try { 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 titleText = todoTitle.getText();
String descText = todoDescription.getText(); String descText = todoDescription.getText();
String selectedStatus = (String) statusCombo.getSelectedItem(); String selectedStatus = (String) statusCombo.getSelectedItem();
@@ -82,7 +89,7 @@ public class CreateTodoPanel extends BasePanel {
todoTitle.setText(""); todoTitle.setText("");
todoDescription.setText(""); todoDescription.setText("");
statusCombo.setSelectedItem("NEW"); statusCombo.setSelectedItem("NEW");
todoDate.setValue(""); todoDate.setValue(null);
JOptionPane.showMessageDialog(this, "Task created"); JOptionPane.showMessageDialog(this, "Task created");
} else { } else {

View File

@@ -12,12 +12,6 @@ public class HomePanel extends BasePanel {
public HomePanel(Consumer<String> changePanel) { public HomePanel(Consumer<String> changePanel) {
super(changePanel); super(changePanel);
User user = Session.getInstance().getUser();
if (user == null) {
// Log, user is not logged.
changePanel.accept("auth");
}
JButton logoutButton = new JButton("Logout"); JButton logoutButton = new JButton("Logout");
logoutButton.addActionListener(e -> { logoutButton.addActionListener(e -> {
Session.getInstance().logout(); Session.getInstance().logout();
@@ -26,24 +20,25 @@ public class HomePanel extends BasePanel {
JButton addTodoButton = new JButton("Create Todo"); JButton addTodoButton = new JButton("Create Todo");
addTodoButton.addActionListener(e -> { 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"); changePanel.accept("create_todo");
}); });
JButton showTodoButton = new JButton("Show all Todo");
showTodoButton.addActionListener(e -> {
changePanel.accept("show_all_todo");
// Wyświetlenie wszystkich tasków tylko dzisiejszych tasków });
// Umożliwienie utworzenia nowych tasków.
add(logoutButton); add(logoutButton);
add(addTodoButton, BorderLayout.NORTH); add(addTodoButton, BorderLayout.NORTH);
add(showTodoButton, BorderLayout.NORTH);
} }
} }

View File

@@ -38,7 +38,9 @@ public class LoginPanel extends BasePanel {
Optional<User> user = service.login(usernameText, passwordText); Optional<User> user = service.login(usernameText, passwordText);
if (user.isPresent()) { if (user.isPresent()) {
Session.getInstance().setUser(user.get()); Session session = Session.getInstance();
session.setUser(user.get());
changePanel.accept("home"); changePanel.accept("home");
} else { } else {
JOptionPane.showMessageDialog(this, "User not found"); JOptionPane.showMessageDialog(this, "User not found");

View File

@@ -17,6 +17,7 @@ public class MyFrame extends JFrame {
mainPanel.add(new RegistryPanel(this::changePanel), "registry"); mainPanel.add(new RegistryPanel(this::changePanel), "registry");
mainPanel.add(new HomePanel(this::changePanel), "home"); mainPanel.add(new HomePanel(this::changePanel), "home");
mainPanel.add(new CreateTodoPanel(this::changePanel), "create_todo"); mainPanel.add(new CreateTodoPanel(this::changePanel), "create_todo");
mainPanel.add(new ShowTodosPanel(this::changePanel), "show_all_todo");
} }
public MyFrame() { public MyFrame() {

View 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);
}
}

View File

@@ -21,8 +21,10 @@ public class Session {
public static Session getInstance() { public static Session getInstance() {
if (instance == null) { if (instance == null) {
System.out.println("Tworzymy sesje");
instance = new Session(); instance = new Session();
} }
System.out.println("Zwracamy sesje");
return instance; return instance;
} }

View File

@@ -0,0 +1 @@
ALTER TABLE tasks ALTER COLUMN deadline TYPE DATE;