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;
|
package repository;
|
||||||
|
|
||||||
|
import dto.TaskCreateDto;
|
||||||
import models.Task;
|
import models.Task;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -12,7 +13,7 @@ public interface TaskRepository {
|
|||||||
ArrayList<Task> getUserTasks(Date startDate, Date endDate);
|
ArrayList<Task> getUserTasks(Date startDate, Date endDate);
|
||||||
ArrayList<Task> getUserTasks(String title);
|
ArrayList<Task> getUserTasks(String title);
|
||||||
Optional<Task> getUserTask(int taskId);
|
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);
|
Optional<Task> update(int taskId, String title, String description);
|
||||||
boolean delete(int taskId);
|
boolean delete(int taskId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package repository;
|
package repository;
|
||||||
|
|
||||||
|
import dto.TaskCreateDto;
|
||||||
import models.Task;
|
import models.Task;
|
||||||
import models.User;
|
import models.User;
|
||||||
import util.Session;
|
import util.Session;
|
||||||
@@ -162,24 +163,25 @@ public class TaskRepositoryJdbc implements TaskRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Task> save(String title, String description) {
|
public Optional<Task> save(TaskCreateDto taskCreateDto) {
|
||||||
try {
|
try {
|
||||||
User user = Session.getInstance().getUser();
|
User user = Session.getInstance().getUser();
|
||||||
if (user == null) Optional.empty();
|
if (user == null) Optional.empty();
|
||||||
|
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(
|
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(1, taskCreateDto.title());
|
||||||
preparedStatement.setString(2, description);
|
preparedStatement.setString(2, taskCreateDto.description());
|
||||||
|
preparedStatement.setDate(3, (java.sql.Date) taskCreateDto.deadline());
|
||||||
ResultSet resultSet = preparedStatement.executeQuery();
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
if (resultSet.next()) {
|
if (resultSet.next()) {
|
||||||
Task task = new Task(
|
Task task = new Task(
|
||||||
resultSet.getInt("id"),
|
resultSet.getInt("id"),
|
||||||
title,
|
taskCreateDto.title(),
|
||||||
description,
|
taskCreateDto.description(),
|
||||||
resultSet.getTimestamp("created_at"),
|
resultSet.getTimestamp("created_at"),
|
||||||
user
|
user
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
package service;
|
package service;
|
||||||
|
|
||||||
|
import dto.TaskCreateDto;
|
||||||
import models.Task;
|
import models.Task;
|
||||||
import repository.TaskRepository;
|
import repository.TaskRepository;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class TaskService {
|
public class TaskService {
|
||||||
|
|
||||||
@@ -18,10 +20,20 @@ public class TaskService {
|
|||||||
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);
|
||||||
} catch (SQLException e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
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 util.Session;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class HomePanel extends BasePanel {
|
public class HomePanel extends BasePanel {
|
||||||
@@ -23,6 +24,11 @@ public class HomePanel extends BasePanel {
|
|||||||
changePanel.accept("auth");
|
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(logoutButton);
|
||||||
|
add(addTodoButton, BorderLayout.NORTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,11 +15,17 @@ public class LoginPanel extends BasePanel {
|
|||||||
public LoginPanel(Consumer<String> changePanel) {
|
public LoginPanel(Consumer<String> changePanel) {
|
||||||
super(changePanel);
|
super(changePanel);
|
||||||
|
|
||||||
JTextField username = new JTextField("Login");
|
JPanel formPanel = new JPanel(new GridLayout(3, 2, 5, 5));
|
||||||
JPasswordField password = new JPasswordField("Password");
|
JLabel usernameLabel = new JLabel("Username:");
|
||||||
|
JTextField username = new JTextField();
|
||||||
|
|
||||||
add(username, BorderLayout.NORTH);
|
JLabel passwordLabel = new JLabel("Password:");
|
||||||
add(password, BorderLayout.CENTER);
|
JPasswordField password = new JPasswordField();
|
||||||
|
|
||||||
|
formPanel.add(usernameLabel);
|
||||||
|
formPanel.add(username);
|
||||||
|
formPanel.add(passwordLabel);
|
||||||
|
formPanel.add(password);
|
||||||
|
|
||||||
JButton loginButton = new JButton("Login");
|
JButton loginButton = new JButton("Login");
|
||||||
loginButton.addActionListener(e -> {
|
loginButton.addActionListener(e -> {
|
||||||
@@ -43,12 +49,14 @@ public class LoginPanel extends BasePanel {
|
|||||||
JOptionPane.showMessageDialog(this, ex.getMessage());
|
JOptionPane.showMessageDialog(this, ex.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
add(loginButton, BorderLayout.SOUTH);
|
|
||||||
|
|
||||||
JButton returnButton = new JButton("Return");
|
JButton returnButton = new JButton("Return");
|
||||||
returnButton.addActionListener(e -> {
|
returnButton.addActionListener(e -> {
|
||||||
changePanel.accept("auth");
|
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 LoginPanel(this::changePanel), "login");
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
public MyFrame() {
|
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