New architecture test

This commit is contained in:
Данил
2025-05-25 14:10:59 +07:00
parent e25298da38
commit 4956813af9
4 changed files with 105 additions and 128 deletions
@@ -1,40 +1,13 @@
package com.Samsung.TaskTraker_Server.Repository; package com.Samsung.TaskTraker_Server.Repository;
import java.io.*;
import java.sql.*; import java.sql.*;
import java.util.*; import java.util.*;
public class DataBase { public class DataBase {
private final Object synhronizedDataBase = new Object();
private class TaskDB {
private PreparedStatement statement;
public TaskDB(PreparedStatement statement) {
this.statement = statement;
}
public void execute() {
try {
synchronized (synhronizedDataBase) {
statement.executeUpdate();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
private static DataBase instance; private static DataBase instance;
private final Connection connection; private final Connection connection;
private final List<TaskDB> TaskDBList = new ArrayList<>();
private final Timer UpdateDBTimer = new Timer();
public void close() { public void close() {
UpdateDBTimer.cancel();
for (TaskDB i : TaskDBList)
i.execute();
TaskDBList.clear();
try { try {
connection.close(); connection.close();
} catch (SQLException e) { } catch (SQLException e) {
@@ -50,15 +23,6 @@ public class DataBase {
System.out.println("[!] DB: " + e.getMessage()); System.out.println("[!] DB: " + e.getMessage());
throw new RuntimeException(e); throw new RuntimeException(e);
} }
UpdateDBTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for (TaskDB i : TaskDBList)
i.execute();
TaskDBList.clear();
}
}, 1_800_000,1_800_000);
} }
public static DataBase getInstance() { public static DataBase getInstance() {
@@ -68,47 +32,74 @@ public class DataBase {
return instance; return instance;
} }
private static String Serialize(List<Task> task) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ObjectOutputStream serialize = new ObjectOutputStream(baos);
serialize.writeObject(task);
serialize.flush();
return Base64.getEncoder().encodeToString(baos.toByteArray());
} catch (IOException e) {
System.out.println("[!] DB: " + e.getMessage());
throw new RuntimeException(e);
}
}
private static List<Task> Deserialize(String bytes) {
byte[] data = Base64.getDecoder().decode(bytes);
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) {
return (ArrayList<Task>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("[!] DB: " + e.getMessage());
throw new RuntimeException(e);
}
}
public void SaveUser(User user) throws SQLException { public void SaveUser(User user) throws SQLException {
PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO user (login, password, task_list) VALUE (?, ?, ?)"); PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO users(login, password) VALUE (?, ?)");
saveUser_stmt.setString(1, user.getLogin()); saveUser_stmt.setString(1, user.getLogin());
saveUser_stmt.setString(2, user.getPassword()); saveUser_stmt.setString(2, user.getPassword());
saveUser_stmt.setString(3, Serialize(user.getTaskList()));
saveUser_stmt.executeUpdate(); saveUser_stmt.executeUpdate();
} }
public void UpdateTaskList(User user) throws SQLException { public void AddTask(Task task, User user) throws SQLException {
PreparedStatement updateTask_stmt = connection.prepareStatement("UPDATE user SET task_list = ? WHERE token = ?"); PreparedStatement addTask = connection.prepareStatement("INSERT INTO tasks(ID," +
updateTask_stmt.setString(1, Serialize(user.getTaskList())); " TaskName," +
updateTask_stmt.setLong(2, user.getID()); " TaskDescription," +
TaskDBList.add(new TaskDB(updateTask_stmt)); " TaskDate," +
" TaskTime," +
" UserOwner)" +
" VALUE (?, ?, ?, ?, ?, ?)");
addTask.setLong(1, task.getID());
addTask.setString(2, task.getTaskName());
addTask.setString(3, task.getTaskDescription());
addTask.setString(4, task.getTaskDate());
addTask.setString(5, task.getTaskTime());
addTask.setLong(6, user.getID());
addTask.executeUpdate();
}
public void UpdateTask(Task task, User user) throws SQLException {
PreparedStatement addTask = connection.prepareStatement("UPDATE tasks SET TaskName = ?," +
" TaskDescription = ?," +
" TaskDate = ?," +
" TaskTime = ?" +
" WHERE UserOwner = ? and ID = ?");
addTask.setString(1, task.getTaskName());
addTask.setString(2, task.getTaskDescription());
addTask.setString(3, task.getTaskDate());
addTask.setString(4, task.getTaskTime());
addTask.setLong(5, user.getID());
addTask.setLong(6, task.getID());
addTask.executeUpdate(); // <---------
}
public void RemoveTask(long taskID, User user) throws SQLException {
PreparedStatement remove = connection.prepareStatement("DELETE FROM tasks WHERE ID = ? AND UserOwner = ?");
remove.setLong(1, taskID);
remove.setLong(2, user.getID());
remove.executeUpdate();
}
public List<Task> getTaskList(User user) throws SQLException {
PreparedStatement findTasks = connection.prepareStatement("SELECT * FROM tasks WHERE UserOwner = ?");
findTasks.setInt(1, user.getID());
ResultSet result = findTasks.executeQuery();
List<Task> taskList = new ArrayList<>();
while(result.next()) {
Task task = new Task(result.getLong(1), // dbID
result.getLong(2), // ID
result.getString(3), // TaskName
result.getString(4), // TaskDescription
result.getString(5), // TaskDate
result.getString(6)); // TaskTime
taskList.add(task);
}
return taskList;
} }
public User FindUser(String login) throws SQLException { public User FindUser(String login) throws SQLException {
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM users WHERE login = ?");
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM user WHERE login = ?");
getUser_stmt.setString(1, login); getUser_stmt.setString(1, login);
ResultSet resultSet = getUser_stmt.executeQuery(); ResultSet resultSet = getUser_stmt.executeQuery();
if(!resultSet.next()) if(!resultSet.next())
@@ -116,8 +107,7 @@ public class DataBase {
int id = resultSet.getInt(1); int id = resultSet.getInt(1);
String pass = resultSet.getNString(3); String pass = resultSet.getNString(3);
List<Task> taskList = Deserialize(resultSet.getNString(4));
return new User(id, login, pass, taskList); return new User(id, login, pass);
} }
} }
@@ -1,21 +1,26 @@
package com.Samsung.TaskTraker_Server.Repository; package com.Samsung.TaskTraker_Server.Repository;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
public class Task implements Serializable { public class Task implements Serializable {
@JsonIgnore
private final long dbID;
private final long ID; private final long ID;
private final String TaskName; private final String TaskName;
private final String TaskDescription; private final String TaskDescription;
private final String TaskDate; private final String TaskDate;
private final String TaskTime; private final String TaskTime;
public Task(long id, String taskName, String taskDescription, String taskDate, String taskTime) { public Task(long dbID, long id, String taskName, String taskDescription, String taskDate, String taskTime) {
this.TaskName = taskName; this.TaskName = taskName;
this.TaskDescription = taskDescription; this.TaskDescription = taskDescription;
this.TaskDate = taskDate; this.TaskDate = taskDate;
this.TaskTime = taskTime; this.TaskTime = taskTime;
this.ID = id; this.ID = id;
this.dbID = dbID;
} }
public long getID() { public long getID() {
@@ -42,11 +47,11 @@ public class Task implements Serializable {
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Task task = (Task) o; Task task = (Task) o;
return ID == task.ID; return dbID == task.dbID && ID == task.ID;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(ID, TaskName, TaskDescription, TaskDate, TaskTime); return Objects.hash(dbID, ID, TaskName, TaskDescription, TaskDate, TaskTime);
} }
} }
@@ -1,30 +1,18 @@
package com.Samsung.TaskTraker_Server.Repository; package com.Samsung.TaskTraker_Server.Repository;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
public class User { public class User {
private final long ID; private final int ID;
private final String login; private final String login;
private final String password; private final String password;
private List<Task> TaskList = new ArrayList<>();
public User(long ID, String login, String password, List<Task> taskList) { public User(int ID, String login, String password) {
this.ID = ID;
this.login = login;
this.password = password;
TaskList = taskList;
}
public User(long ID, String login, String password) {
this.ID = ID; this.ID = ID;
this.login = login; this.login = login;
this.password = password; this.password = password;
} }
public long getID() { public int getID() {
return ID; return ID;
} }
@@ -36,43 +24,15 @@ public class User {
return password; return password;
} }
public List<Task> getTaskList() {
return TaskList;
}
public void addTask(Task task) {
if(TaskList.contains(task)) {
TaskList.remove(task); // Просто удаляем потаму, что cравнение идёт только по ID
TaskList.add(task);
}
else {
TaskList.add(task);
}
try {
DataBase.getInstance().UpdateTaskList(this);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void removeTask(long taskID) {
TaskList.remove(new Task(taskID, null, null, null, null));
try {
DataBase.getInstance().UpdateTaskList(this);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
User user = (User) o; User user = (User) o;
return ID == user.ID && Objects.equals(login, user.login) && Objects.equals(password, user.password) && Objects.equals(TaskList, user.TaskList); return ID == user.ID && Objects.equals(login, user.login) && Objects.equals(password, user.password);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(ID, login, password, TaskList); return Objects.hash(ID, login, password);
} }
} }
@@ -57,6 +57,7 @@ public class TaskTrakerServerApplication {
else else
return "{\n\t\"status\":\"ok\"\n}"; return "{\n\t\"status\":\"ok\"\n}";
} catch (SQLException e) { } catch (SQLException e) {
System.out.println(e.getMessage());
return "{\n\t\"status\":\"error\"\n}"; return "{\n\t\"status\":\"error\"\n}";
} }
} }
@@ -78,6 +79,7 @@ public class TaskTrakerServerApplication {
return "{\n\t\"status\":\"ok\"\n}"; return "{\n\t\"status\":\"ok\"\n}";
} catch (SQLException e) { } catch (SQLException e) {
System.out.println(e.getMessage());
return "{\n\t\"status\":\"error\"\n}"; return "{\n\t\"status\":\"error\"\n}";
} }
} }
@@ -91,19 +93,20 @@ public class TaskTrakerServerApplication {
User user = UserRepository.getRepository().getUserByLogin(login); User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass)) if (user == null || !user.getPassword().equals(pass))
return new Task(0, null, null, null, null); return new Task(0, 0, null, null, null, null);
List<Task> taskList = user.getTaskList(); List<Task> taskList = DataBase.getInstance().getTaskList(user);
for (Task i : taskList) { for (Task i : taskList) {
if (i.getID() == taskID) if (i.getID() == taskID)
return i; return i;
} }
} catch (SQLException e) { } catch (SQLException e) {
return new Task(0, null, null, null, null); System.out.println(e.getMessage());
return new Task(0, 0, null, null, null, null);
} }
} }
return new Task(0, null, null, null, null); return new Task(0, 0, null, null, null, null);
} }
@GetMapping("/user/task/list") @GetMapping("/user/task/list")
@@ -117,14 +120,15 @@ public class TaskTrakerServerApplication {
if (user == null || !user.getPassword().equals(pass)) if (user == null || !user.getPassword().equals(pass))
return new ArrayList<>(); return new ArrayList<>();
else else
return user.getTaskList(); return DataBase.getInstance().getTaskList(user);
} catch (SQLException e) { } catch (SQLException e) {
System.out.println(e.getMessage());
return new ArrayList<>(); return new ArrayList<>();
} }
} }
} }
@PostMapping("/user/task/edit") @PostMapping("/user/task/add")
public void addUserTask(@RequestParam(value = "login", defaultValue = "null") String login, public void addUserTask(@RequestParam(value = "login", defaultValue = "null") String login,
@RequestParam(value = "password", defaultValue = "null") String pass, @RequestParam(value = "password", defaultValue = "null") String pass,
@RequestBody Task task) { @RequestBody Task task) {
@@ -133,8 +137,25 @@ public class TaskTrakerServerApplication {
User user = UserRepository.getRepository().getUserByLogin(login); User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass)) if (user == null || !user.getPassword().equals(pass))
return; return;
user.addTask(task); DataBase.getInstance().AddTask(task, user);
} catch (SQLException ignored) { } catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
@PostMapping("/user/task/edit")
public void editUserTask(@RequestParam(value = "login", defaultValue = "null") String login,
@RequestParam(value = "password", defaultValue = "null") String pass,
@RequestBody Task task) {
if (!login.equals("null") && !pass.equals("null") && task != null) {
try {
User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass))
return;
DataBase.getInstance().UpdateTask(task, user);
} catch (SQLException e) {
System.out.println(e.getMessage());
} }
} }
} }
@@ -148,8 +169,9 @@ public class TaskTrakerServerApplication {
User user = UserRepository.getRepository().getUserByLogin(login); User user = UserRepository.getRepository().getUserByLogin(login);
if (user == null || !user.getPassword().equals(pass)) if (user == null || !user.getPassword().equals(pass))
return; return;
user.removeTask(id); DataBase.getInstance().RemoveTask(id, user);
} catch (SQLException ignore) { } catch (SQLException e) {
System.out.println(e.getMessage());
} }
} }
} }