Added view for all Todos, added possibility of updating todo

This commit is contained in:
maciejrusek
2026-05-07 22:18:07 +02:00
parent 45b08b310f
commit fc476d63d5
5 changed files with 213 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
package service;
import dto.TaskCreateDto;
import dto.TaskUpdateDto;
import models.Task;
import repository.TaskRepository;
@@ -46,4 +47,23 @@ public class TaskService {
}
}
public Optional<Task> updateTask(TaskUpdateDto taskUpdateDto) {
try {
return taskRepository.update(taskUpdateDto);
} catch (Exception e) {
e.printStackTrace();
return Optional.empty();
}
}
public Boolean deleteTask(int taskId) {
try {
return taskRepository.delete(taskId);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}

View File

@@ -0,0 +1,131 @@
package ui;
import dto.TaskCreateDto;
import dto.TaskUpdateDto;
import models.Task;
import models.User;
import repository.TaskRepository;
import service.TaskService;
import util.Session;
import javax.swing.*;
import java.awt.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.function.Consumer;
public class EditTodoPanel extends BasePanel {
private Task currentTask;
public void loadEditForm() {
removeAll();
if (currentTask != null) {
JPanel formPanel = new JPanel(new GridLayout(5, 2, 5, 5));
JLabel todoLabel = new JLabel("title:");
JTextField todoTitle = new JTextField(currentTask.getTitle());
JLabel descLabel = new JLabel("Description:");
JTextField todoDescription = new JTextField(currentTask.getDescription());
String statuses[] = { "NEW", "IN_PROGRESS", "CANCELED", "DONE" };
JLabel statusLabel = new JLabel("Status:");
JComboBox<String> statusCombo = new JComboBox<>(statuses);
statusCombo.setSelectedItem(currentTask.getStatus().name());
JLabel dateLabel = new JLabel("Date:");
DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
JFormattedTextField todoDate = new JFormattedTextField(format);
todoDate.setValue(currentTask.getDeadline());
formPanel.add(todoLabel);
formPanel.add(todoTitle);
formPanel.add(descLabel);
formPanel.add(todoDescription);
formPanel.add(dateLabel);
formPanel.add(todoDate);
formPanel.add(statusLabel);
formPanel.add(statusCombo);
JButton updateButton = new JButton("Update");
updateButton.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();
String dateText = todoDate.getText();
Date parsedDate;
try {
parsedDate = format.parse(dateText);
} catch (Exception exc) {
JOptionPane.showMessageDialog(this, "Invalid date");
return;
}
TaskRepository taskRepository = Session.getTaskRepository();
TaskService service = new TaskService(taskRepository);
Task.Status status = Task.Status.valueOf(selectedStatus);
TaskUpdateDto taskUpdateDto = new TaskUpdateDto(
currentTask.getId(),
titleText,
descText,
status,
parsedDate
);
Optional<Task> task = service.updateTask(taskUpdateDto);
if (task.isPresent()) {
JOptionPane.showMessageDialog(this, "Task updated successfully");
} else {
JOptionPane.showMessageDialog(this, "Failed to update task");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
});
JButton returnButton = new JButton("Return");
returnButton.addActionListener(e -> {
changePanel.accept("show_all_todo");
});
formPanel.add(returnButton);
formPanel.add(updateButton);
add(formPanel, BorderLayout.CENTER);
}
}
public EditTodoPanel(Consumer<String> changePanel) {
super(changePanel);
setLayout(new BorderLayout());
loadEditForm();
}
public void loadTask(Task task) {
this.currentTask = task;
this.revalidate();
this.repaint();
loadEditForm();
}
}

View File

@@ -1,23 +1,38 @@
package ui;
import models.Task;
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
JPanel mainPanel = new JPanel(new CardLayout());
private EditTodoPanel editTodoPanel;
public void changePanel(String panel) {
CardLayout cardLayout = (CardLayout) mainPanel.getLayout();
cardLayout.show(mainPanel, panel);
}
public void openEditTodo(Task task) {
editTodoPanel.loadTask(task);
changePanel("edit_todo");
}
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");
mainPanel.add(new CreateTodoPanel(this::changePanel), "create_todo");
mainPanel.add(new ShowTodosPanel(this::changePanel), "show_all_todo");
mainPanel.add(new ShowTodosPanel(this::changePanel, this::openEditTodo), "show_all_todo");
editTodoPanel = new EditTodoPanel(this::changePanel);
mainPanel.add(editTodoPanel, "edit_todo");
}
public MyFrame() {

View File

@@ -15,7 +15,7 @@ import java.util.function.Consumer;
public class ShowTodosPanel extends BasePanel {
private JPanel tasksContainer;
public ShowTodosPanel(Consumer<String> changePanel) {
public ShowTodosPanel(Consumer<String> changePanel, Consumer<Task> changeTask) {
super(changePanel);
setLayout(new BorderLayout());
@@ -38,16 +38,57 @@ public class ShowTodosPanel extends BasePanel {
tasksContainer.removeAll();
for (Task task : tasks) {
JPanel taskPanel = new JPanel(new BorderLayout());
taskPanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(220, 220, 220)),
BorderFactory.createEmptyBorder(10, 15, 10, 15)
));
taskPanel.setBackground(Color.WHITE);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
infoPanel.setOpaque(false);
JLabel titleLabel = new JLabel(task.getTitle());
JLabel statusLabel = new JLabel(task.getStatus().name());
titleLabel.setForeground(new Color(0, 0, 0));
titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
taskPanel.add(titleLabel, BorderLayout.WEST);
taskPanel.add(statusLabel, BorderLayout.EAST);
JLabel statusLabel = new JLabel("Status: " + task.getStatus().name());
statusLabel.setForeground(new Color(120, 120, 120));
statusLabel.setFont(new Font("Arial", Font.PLAIN, 12));
taskPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));
infoPanel.add(titleLabel);
infoPanel.add(Box.createVerticalStrut(5));
infoPanel.add(statusLabel);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.setOpaque(false);
JButton editTodoButton = new JButton("Edit");
editTodoButton.addActionListener(ed -> {
changeTask.accept(task);
changePanel.accept("edit_button");
});
JButton deleteTodoButton = new JButton("Delete");
deleteTodoButton.addActionListener(ed -> {
if (service.deleteTask(task.getId())) {
showTodosButton.doClick();
JOptionPane.showMessageDialog(this, "Todo was deleted successful!", "Deleted", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Failed while deleted Todo", "Failed", JOptionPane.WARNING_MESSAGE);
}
});
buttonPanel.add(editTodoButton, BorderLayout.CENTER);
buttonPanel.add(deleteTodoButton, BorderLayout.EAST);
taskPanel.add(infoPanel, BorderLayout.CENTER);
taskPanel.add(buttonPanel, BorderLayout.EAST);
taskPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 90));
tasksContainer.add(taskPanel);
tasksContainer.add(Box.createVerticalStrut(10));
}
tasksContainer.revalidate();

View File

@@ -21,10 +21,8 @@ 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;
}