Modify task model, create some UI...
This commit is contained in:
10
src/main/java/dto/TaskCreateDto.java
Normal file
10
src/main/java/dto/TaskCreateDto.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package dto;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public record TaskCreateDto (
|
||||
String title,
|
||||
String description,
|
||||
Date deadline
|
||||
) {
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package repository;
|
||||
|
||||
import dto.TaskCreateDto;
|
||||
import models.Task;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -12,7 +13,7 @@ public interface TaskRepository {
|
||||
ArrayList<Task> getUserTasks(Date startDate, Date endDate);
|
||||
ArrayList<Task> getUserTasks(String title);
|
||||
Optional<Task> getUserTask(int taskId);
|
||||
Optional<Task> save(String title, String description);
|
||||
Optional<Task> save(TaskCreateDto taskCreateDto);
|
||||
Optional<Task> update(int taskId, String title, String description);
|
||||
boolean delete(int taskId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package repository;
|
||||
|
||||
import dto.TaskCreateDto;
|
||||
import models.Task;
|
||||
import models.User;
|
||||
import util.Session;
|
||||
@@ -162,24 +163,25 @@ public class TaskRepositoryJdbc implements TaskRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Task> save(String title, String description) {
|
||||
public Optional<Task> save(TaskCreateDto taskCreateDto) {
|
||||
try {
|
||||
User user = Session.getInstance().getUser();
|
||||
if (user == null) Optional.empty();
|
||||
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||
"INSERT INTO tasks (title, description) VALUES (?, ?) RETURNING id, created_at"
|
||||
"INSERT INTO tasks (title, description, deadline) VALUES (?, ?, ?) RETURNING id, created_at"
|
||||
);
|
||||
|
||||
preparedStatement.setString(1, title);
|
||||
preparedStatement.setString(2, description);
|
||||
preparedStatement.setString(1, taskCreateDto.title());
|
||||
preparedStatement.setString(2, taskCreateDto.description());
|
||||
preparedStatement.setDate(3, (java.sql.Date) taskCreateDto.deadline());
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
|
||||
if (resultSet.next()) {
|
||||
Task task = new Task(
|
||||
resultSet.getInt("id"),
|
||||
title,
|
||||
description,
|
||||
taskCreateDto.title(),
|
||||
taskCreateDto.description(),
|
||||
resultSet.getTimestamp("created_at"),
|
||||
user
|
||||
);
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package service;
|
||||
|
||||
import dto.TaskCreateDto;
|
||||
import models.Task;
|
||||
import repository.TaskRepository;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
public class TaskService {
|
||||
|
||||
@@ -18,10 +20,20 @@ public class TaskService {
|
||||
public ArrayList<Task> getUserTasksFilterByDate(Date startDate, Date endDate) {
|
||||
try {
|
||||
return taskRepository.getUserTasks(startDate, endDate);
|
||||
} catch (SQLException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Optional<Task> createTask(TaskCreateDto taskCreateDto) {
|
||||
try {
|
||||
return taskRepository.save(taskCreateDto);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
70
src/main/java/ui/CreateTodoPanel.java
Normal file
70
src/main/java/ui/CreateTodoPanel.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package ui;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.text.spi.DateFormatProvider;
|
||||
import java.util.Date;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class CreateTodoPanel extends BasePanel {
|
||||
|
||||
public CreateTodoPanel(Consumer<String> changePanel) {
|
||||
super(changePanel);
|
||||
|
||||
JPanel formPanel = new JPanel(new GridLayout(4, 2, 5, 5));
|
||||
JLabel todoLabel = new JLabel("title:");
|
||||
JTextField todoTitle = new JTextField();
|
||||
|
||||
JLabel descLabel = new JLabel("Description:");
|
||||
JTextField todoDescription = new JTextField();
|
||||
|
||||
JLabel dateLabel = new JLabel("Date:");
|
||||
DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
|
||||
JFormattedTextField todoDate = new JFormattedTextField(format);
|
||||
|
||||
|
||||
formPanel.add(todoLabel);
|
||||
formPanel.add(todoTitle);
|
||||
formPanel.add(descLabel);
|
||||
formPanel.add(todoDescription);
|
||||
formPanel.add(dateLabel);
|
||||
formPanel.add(todoDate);
|
||||
|
||||
JButton createButton = new JButton("Create");
|
||||
createButton.addActionListener(e -> {
|
||||
try {
|
||||
String titleText = todoTitle.getText();
|
||||
String descText = todoDescription.getText();
|
||||
String dateText = todoDate.getText();
|
||||
|
||||
Date parsedDate;
|
||||
try {
|
||||
parsedDate = format.parse(dateText);
|
||||
} catch (Exception exc) {
|
||||
JOptionPane.showMessageDialog(this, "Invalid date");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
JButton returnButton = new JButton("Return");
|
||||
returnButton.addActionListener(e -> {
|
||||
changePanel.accept("home");
|
||||
});
|
||||
|
||||
|
||||
formPanel.add(returnButton);
|
||||
formPanel.add(createButton);
|
||||
add(formPanel, BorderLayout.CENTER);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import models.User;
|
||||
import util.Session;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class HomePanel extends BasePanel {
|
||||
@@ -23,6 +24,11 @@ public class HomePanel extends BasePanel {
|
||||
changePanel.accept("auth");
|
||||
});
|
||||
|
||||
JButton addTodoButton = new JButton("Create Todo");
|
||||
addTodoButton.addActionListener(e -> {
|
||||
Session.getInstance().logout();
|
||||
changePanel.accept("create_todo");
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -37,6 +43,7 @@ public class HomePanel extends BasePanel {
|
||||
|
||||
|
||||
add(logoutButton);
|
||||
add(addTodoButton, BorderLayout.NORTH);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,11 +15,17 @@ public class LoginPanel extends BasePanel {
|
||||
public LoginPanel(Consumer<String> changePanel) {
|
||||
super(changePanel);
|
||||
|
||||
JTextField username = new JTextField("Login");
|
||||
JPasswordField password = new JPasswordField("Password");
|
||||
JPanel formPanel = new JPanel(new GridLayout(3, 2, 5, 5));
|
||||
JLabel usernameLabel = new JLabel("Username:");
|
||||
JTextField username = new JTextField();
|
||||
|
||||
add(username, BorderLayout.NORTH);
|
||||
add(password, BorderLayout.CENTER);
|
||||
JLabel passwordLabel = new JLabel("Password:");
|
||||
JPasswordField password = new JPasswordField();
|
||||
|
||||
formPanel.add(usernameLabel);
|
||||
formPanel.add(username);
|
||||
formPanel.add(passwordLabel);
|
||||
formPanel.add(password);
|
||||
|
||||
JButton loginButton = new JButton("Login");
|
||||
loginButton.addActionListener(e -> {
|
||||
@@ -43,12 +49,14 @@ public class LoginPanel extends BasePanel {
|
||||
JOptionPane.showMessageDialog(this, ex.getMessage());
|
||||
}
|
||||
});
|
||||
add(loginButton, BorderLayout.SOUTH);
|
||||
|
||||
JButton returnButton = new JButton("Return");
|
||||
returnButton.addActionListener(e -> {
|
||||
changePanel.accept("auth");
|
||||
});
|
||||
add(returnButton, BorderLayout.EAST);
|
||||
|
||||
formPanel.add(returnButton);
|
||||
formPanel.add(loginButton);
|
||||
add(formPanel, BorderLayout.CENTER);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public class MyFrame extends JFrame {
|
||||
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");
|
||||
}
|
||||
|
||||
public MyFrame() {
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
CREATE TYPE statusLabels AS ENUM ('NEW', 'IN_PROGRESS', 'CANCELED', 'DONE');
|
||||
|
||||
ALTER TABLE tasks
|
||||
ADD COLUMN deadline TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
ADD COLUMN status statusLabels NOT NULL DEFAULT 'NEW';
|
||||
Reference in New Issue
Block a user