Created some logic in TaskRepository. Added id to models, etc...
This commit is contained in:
@@ -5,16 +5,26 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class Task {
|
public class Task {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
private User user;
|
private User user;
|
||||||
private String title;
|
private String title;
|
||||||
private String description;
|
private String description;
|
||||||
private Date createdAt;
|
private Date createdAt;
|
||||||
|
|
||||||
public Task(String title, String description, User user) {
|
public Task(Integer id, String title, String description, Date createdAt, User user) {
|
||||||
this.user = user;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.createdAt = new Date();
|
this.createdAt = new Date();
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUser() {
|
public User getUser() {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models;
|
package models;
|
||||||
|
|
||||||
public record User(
|
public record User(
|
||||||
|
Integer id,
|
||||||
String username,
|
String username,
|
||||||
String email
|
String email
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -3,14 +3,16 @@ package repository;
|
|||||||
import models.Task;
|
import models.Task;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface TaskRepository {
|
public interface TaskRepository {
|
||||||
ArrayList<Task> getUserTasks(int userId);
|
ArrayList<Task> getUserTasks();
|
||||||
ArrayList<Task> findUserTasksByTitle(int userId, String title);
|
ArrayList<Task> getUserTasks(Date startDate, Date endDate);
|
||||||
Optional<Task> getUserTask(int userId, int taskId);
|
ArrayList<Task> getUserTasks(String title);
|
||||||
void save(Task task);
|
Optional<Task> getUserTask(int taskId);
|
||||||
void update(int taskId, Task task);
|
Optional<Task> save(String title, String description);
|
||||||
void delete(int taskId);
|
Optional<Task> update(int taskId, String title, String description);
|
||||||
|
boolean delete(int taskId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,259 @@
|
|||||||
package repository;
|
package repository;
|
||||||
|
|
||||||
import models.Task;
|
import models.Task;
|
||||||
|
import models.User;
|
||||||
|
import util.Session;
|
||||||
|
|
||||||
|
import javax.imageio.plugins.jpeg.JPEGImageReadParam;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.Date;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public class TaskRepositoryJdbc implements TaskRepository {
|
public class TaskRepositoryJdbc implements TaskRepository {
|
||||||
|
|
||||||
|
private final Connection connection;
|
||||||
|
|
||||||
@Override
|
public TaskRepositoryJdbc(Connection connection) {
|
||||||
public ArrayList<Task> getUserTasks(int userId) {
|
this.connection = connection;
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArrayList<Task> findUserTasksByTitle(int userId, String title) {
|
public ArrayList<Task> getUserTasks() {
|
||||||
|
try {
|
||||||
|
User user = Session.getInstance().getUser();
|
||||||
|
if (user == null) return new ArrayList<>();
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
|
"SELECT * FROM tasks WHERE user_id=(?)"
|
||||||
|
);
|
||||||
|
preparedStatement.setInt(1, user.id());
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
ArrayList<Task> tasks = new ArrayList<>();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
Task task = new Task(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
resultSet.getString("title"),
|
||||||
|
resultSet.getString("description"),
|
||||||
|
resultSet.getTimestamp("created_at"),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
tasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks;
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArrayList<Task> getUserTasks(Date startDate, Date endDate) {
|
||||||
|
try {
|
||||||
|
User user = Session.getInstance().getUser();
|
||||||
|
if (user == null) return new ArrayList<>();
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
|
"SELECT * FROM tasks WHERE user_id=(?) AND created_at BETWEEN (?) AND (?)"
|
||||||
|
);
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, user.id());
|
||||||
|
preparedStatement.setDate(2, new java.sql.Date(startDate.getTime()));
|
||||||
|
preparedStatement.setDate(3, new java.sql.Date(endDate.getTime()));
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
ArrayList<Task> tasks = new ArrayList<>();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
Task task = new Task(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
resultSet.getString("title"),
|
||||||
|
resultSet.getString("description"),
|
||||||
|
resultSet.getTimestamp("created_at"),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
tasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks;
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Task> getUserTask(int userId, int taskId) {
|
public ArrayList<Task> getUserTasks(String title) {
|
||||||
|
try {
|
||||||
|
User user = Session.getInstance().getUser();
|
||||||
|
if (user == null) return new ArrayList<>();
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
|
"SELECT * FROM tasks WHERE user_id=(?) AND title ILIKE (?)"
|
||||||
|
);
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, user.id());
|
||||||
|
preparedStatement.setString(2, "%" + title + "%");
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
ArrayList<Task> tasks = new ArrayList<>();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
|
||||||
|
Task task = new Task(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
resultSet.getString("title"),
|
||||||
|
resultSet.getString("description"),
|
||||||
|
resultSet.getTimestamp("created_at"),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
tasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tasks;
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Task> getUserTask(int taskId) {
|
||||||
|
try {
|
||||||
|
User user = Session.getInstance().getUser();
|
||||||
|
if (user == null) return Optional.empty();
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
|
"SELECT * FROM tasks WHERE id=(?) AND user_id=(?)"
|
||||||
|
);
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, taskId);
|
||||||
|
preparedStatement.setInt(2, user.id());
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
Task task = new Task(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
resultSet.getString("title"),
|
||||||
|
resultSet.getString("description"),
|
||||||
|
resultSet.getTimestamp("created_at"),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
return Optional.of(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.empty();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save(Task task) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(int taskId, Task task) {
|
public Optional<Task> save(String title, String description) {
|
||||||
|
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"
|
||||||
|
);
|
||||||
|
|
||||||
|
preparedStatement.setString(1, title);
|
||||||
|
preparedStatement.setString(2, description);
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
Task task = new Task(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
resultSet.getTimestamp("created_at"),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
return Optional.of(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.empty();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(int taskId) {
|
public Optional<Task> update(int taskId, String title, String description) {
|
||||||
|
try {
|
||||||
|
User user = Session.getInstance().getUser();
|
||||||
|
if (user == null) Optional.empty();
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
|
"UPDATE tasks SET title=?, description=? WHERE id=? AND user_id=(?) RETURNING id, created_at;"
|
||||||
|
);
|
||||||
|
|
||||||
|
preparedStatement.setString(1, title);
|
||||||
|
preparedStatement.setString(2, description );
|
||||||
|
preparedStatement.setInt(3, taskId);
|
||||||
|
preparedStatement.setInt(4, user.id());
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
if (resultSet.next()) {
|
||||||
|
Task task = new Task(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
title,
|
||||||
|
description,
|
||||||
|
resultSet.getTimestamp("created_at"),
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
|
return Optional.of(task);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.empty();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean delete(int taskId) {
|
||||||
|
try {
|
||||||
|
User user = Session.getInstance().getUser();
|
||||||
|
if (user == null) return false;
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
|
"DELETE FROM tasks WHERE id=(?) AND user_id=(?);"
|
||||||
|
);
|
||||||
|
|
||||||
|
preparedStatement.setInt(1, taskId);
|
||||||
|
preparedStatement.setInt(2, user.id());
|
||||||
|
|
||||||
|
int affected = preparedStatement.executeUpdate();
|
||||||
|
|
||||||
|
if (affected > 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,13 +31,14 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Integer userId = resultSet.getInt("id");
|
||||||
String passwordFromDB = resultSet.getString("password");
|
String passwordFromDB = resultSet.getString("password");
|
||||||
String emailFromDB = resultSet.getString("email");
|
String emailFromDB = resultSet.getString("email");
|
||||||
if (!PasswordService.checkPassword(password, passwordFromDB)) {
|
if (!PasswordService.checkPassword(password, passwordFromDB)) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = new User(username, emailFromDB);
|
User user = new User(userId, username, emailFromDB);
|
||||||
|
|
||||||
return Optional.of(user);
|
return Optional.of(user);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@@ -74,16 +75,24 @@ public class UserRepositoryJdbc implements UserRepository {
|
|||||||
String hashedPassword = PasswordService.hashPassword(password);
|
String hashedPassword = PasswordService.hashPassword(password);
|
||||||
|
|
||||||
PreparedStatement preparedStatement = connection.prepareStatement(
|
PreparedStatement preparedStatement = connection.prepareStatement(
|
||||||
"INSERT INTO users (name, email, password) VALUES (?, ?, ?);"
|
"INSERT INTO users (name, email, password) VALUES (?, ?, ?) RETURNING id, name, email;"
|
||||||
);
|
);
|
||||||
|
|
||||||
preparedStatement.setString(1, username);
|
preparedStatement.setString(1, username);
|
||||||
preparedStatement.setString(2, email);
|
preparedStatement.setString(2, email);
|
||||||
preparedStatement.setString(3, hashedPassword);
|
preparedStatement.setString(3, hashedPassword);
|
||||||
preparedStatement.executeUpdate();
|
|
||||||
|
|
||||||
User user = new User(username, email);
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
if (resultSet.next()) {
|
||||||
|
User user = new User(
|
||||||
|
resultSet.getInt("id"),
|
||||||
|
username,
|
||||||
|
email
|
||||||
|
);
|
||||||
return Optional.of(user);
|
return Optional.of(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Optional.empty();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|||||||
27
src/main/java/service/TaskService.java
Normal file
27
src/main/java/service/TaskService.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package service;
|
||||||
|
|
||||||
|
import models.Task;
|
||||||
|
import repository.TaskRepository;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class TaskService {
|
||||||
|
|
||||||
|
TaskRepository taskRepository;
|
||||||
|
|
||||||
|
public TaskService(TaskRepository taskRepository) {
|
||||||
|
this.taskRepository = taskRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Task> getUserTasksFilterByDate(Date startDate, Date endDate) {
|
||||||
|
try {
|
||||||
|
return taskRepository.getUserTasks(startDate, endDate);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -17,13 +17,25 @@ public class HomePanel extends BasePanel {
|
|||||||
changePanel.accept("auth");
|
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();
|
||||||
changePanel.accept("auth");
|
changePanel.accept("auth");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Wyświetlenie wszystkich tasków tylko dzisiejszych tasków
|
||||||
|
// Umożliwienie utworzenia nowych tasków.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
add(logoutButton);
|
add(logoutButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ public class MyFrame extends JFrame {
|
|||||||
mainPanel.add(new HomePanel(this::changePanel), "home");
|
mainPanel.add(new HomePanel(this::changePanel), "home");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public MyFrame() {
|
public MyFrame() {
|
||||||
super("Todo App");
|
super("Todo App");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|||||||
Reference in New Issue
Block a user