Create repository
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package com.Samsung.TaskTracker_Server.Repository;
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
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 final Connection connection;
|
||||
private final List<TaskDB> TaskDBList = new ArrayList<>();
|
||||
private final Timer UpdateDBTimer = new Timer();
|
||||
|
||||
public void close() {
|
||||
UpdateDBTimer.cancel();
|
||||
for (TaskDB i : TaskDBList)
|
||||
i.execute();
|
||||
TaskDBList.clear();
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
instance = null;
|
||||
}
|
||||
|
||||
private DataBase(String login, String pass) {
|
||||
try {
|
||||
connection = DriverManager.getConnection(API_KEYS.DB_URL, login, pass);
|
||||
} catch (SQLException e) {
|
||||
System.out.println("[!] DB: " + e.getMessage());
|
||||
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() {
|
||||
if(instance == null) {
|
||||
instance = new DataBase(API_KEYS.DB_LOGIN, API_KEYS.DB_PASSWORD);
|
||||
}
|
||||
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 {
|
||||
PreparedStatement saveUser_stmt = connection.prepareStatement("INSERT INTO user (login, password, task_list) VALUE (?, ?, ?)");
|
||||
saveUser_stmt.setString(1, user.getLogin());
|
||||
saveUser_stmt.setString(2, user.getPassword());
|
||||
saveUser_stmt.setString(3, Serialize(user.getTaskList()));
|
||||
//saveUser_stmt.executeUpdate();
|
||||
TaskDBList.add(new TaskDB(saveUser_stmt));
|
||||
}
|
||||
|
||||
public void UpdateTaskList(User user) throws SQLException {
|
||||
PreparedStatement updateTask_stmt = connection.prepareStatement("UPDATE user SET task_list = ? WHERE token = ?");
|
||||
updateTask_stmt.setString(1, Serialize(user.getTaskList()));
|
||||
updateTask_stmt.setLong(2, user.getID());
|
||||
TaskDBList.add(new TaskDB(updateTask_stmt));
|
||||
}
|
||||
|
||||
public User FindUser(String login) throws SQLException {
|
||||
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM user WHERE login = ?");
|
||||
getUser_stmt.setString(1, login);
|
||||
ResultSet resultSet = getUser_stmt.executeQuery();
|
||||
resultSet.next();
|
||||
|
||||
int id = resultSet.getInt(1);
|
||||
String pass = resultSet.getNString(3);
|
||||
List<Task> taskList = Deserialize(resultSet.getNString(4));
|
||||
|
||||
return new User(id, login, pass, taskList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.Samsung.TaskTracker_Server.Repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Task implements Serializable {
|
||||
private final long ID;
|
||||
private final String TaskName;
|
||||
private final String TaskDescription;
|
||||
private final String TaskDate;
|
||||
private final String TaskTime;
|
||||
|
||||
public Task(long id, String taskName, String taskDescription, String taskDate, String taskTime) {
|
||||
this.TaskName = taskName;
|
||||
this.TaskDescription = taskDescription;
|
||||
this.TaskDate = taskDate;
|
||||
this.TaskTime = taskTime;
|
||||
this.ID = id;
|
||||
}
|
||||
|
||||
public long getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public String getTaskName() {
|
||||
return TaskName;
|
||||
}
|
||||
|
||||
public String getTaskDescription() {
|
||||
return TaskDescription;
|
||||
}
|
||||
|
||||
public String getTaskDate() {
|
||||
return TaskDate;
|
||||
}
|
||||
|
||||
public String getTaskTime() {
|
||||
return TaskTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Task task = (Task) o;
|
||||
return ID == task.ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(ID, TaskName, TaskDescription, TaskDate, TaskTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.Samsung.TaskTracker_Server.Repository;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class User {
|
||||
private final long ID;
|
||||
private final String login;
|
||||
private final String password;
|
||||
private List<Task> TaskList = new ArrayList<>();
|
||||
|
||||
public User(long ID, String login, String password, List<Task> taskList) {
|
||||
this.ID = ID;
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
TaskList = taskList;
|
||||
}
|
||||
|
||||
public User(long ID, String login, String password) {
|
||||
this.ID = ID;
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public long getID() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public List<Task> getTaskList() {
|
||||
return TaskList;
|
||||
}
|
||||
|
||||
public void addTask(Task task) {
|
||||
if(TaskList.contains(task)) {
|
||||
TaskList.remove(task); // Просто удаляем потаму, что равнение идёт только по 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
|
||||
public boolean equals(Object o) {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
User user = (User) o;
|
||||
return ID == user.ID && Objects.equals(login, user.login) && Objects.equals(password, user.password) && Objects.equals(TaskList, user.TaskList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(ID, login, password, TaskList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.Samsung.TaskTracker_Server.Repository;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
public class UserRepository {
|
||||
private static final int MAX_COUNT_USERS_IN_CACHE = 1000;
|
||||
private static UserRepository instance = null;
|
||||
|
||||
private final List<User> UserCache = new ArrayList<>();
|
||||
|
||||
public static synchronized UserRepository getRepository() {
|
||||
if (instance == null) {
|
||||
instance = new UserRepository();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public User AddUser(String login, String password) throws SQLException {
|
||||
User user = new User(0, login, password);
|
||||
|
||||
DataBase.getInstance().SaveUser(user);
|
||||
if(UserCache.size() >= MAX_COUNT_USERS_IN_CACHE)
|
||||
UserCache.remove(0);
|
||||
UserCache.add(user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
public User getUserByLogin(String login) throws SQLException {
|
||||
for (User i : UserCache) {
|
||||
if(i.getLogin().equals(login)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
User loadUser = DataBase.getInstance().FindUser(login);
|
||||
if(UserCache.size() >= MAX_COUNT_USERS_IN_CACHE)
|
||||
UserCache.remove(0);
|
||||
UserCache.add(loadUser);
|
||||
return loadUser;
|
||||
}
|
||||
|
||||
private String GenerateNewToken(User user) {
|
||||
long key1 = user.getID();
|
||||
long key2 = user.getLogin().hashCode();
|
||||
long key3 = user.getPassword().hashCode();
|
||||
long key4 = new Random().nextInt();
|
||||
return Base64.getMimeEncoder().encodeToString(Long.toString(key1 ^ key2 ^ key3 ^ key4).getBytes());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.Samsung.TaskTracker_Server;
|
||||
|
||||
import com.Samsung.TaskTracker_Server.Repository.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ExitCodeGenerator;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/")
|
||||
@SpringBootApplication
|
||||
public class TaskTrackerServerApplication {
|
||||
private static volatile boolean isRunning = true;
|
||||
public static void main(String[] args) {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(TaskTrackerServerApplication.class, args);
|
||||
|
||||
while (isRunning) {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
int code = SpringApplication.exit(context, new ExitCodeGenerator() {
|
||||
@Override
|
||||
public int getExitCode() {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
System.exit(code);
|
||||
}
|
||||
|
||||
@GetMapping("/admin/shutdown")
|
||||
void shutdown(@RequestParam(name = "password", defaultValue = "null") String password) {
|
||||
if(!password.equals(API_KEYS.ADMIN_PASSWORD))
|
||||
return;
|
||||
DataBase.getInstance().close();
|
||||
isRunning = false;
|
||||
}
|
||||
|
||||
@GetMapping("/singin")
|
||||
String SingIn(@RequestParam(name = "login", defaultValue = "null") String login, @RequestParam(name = "password", defaultValue = "null") String pass) {
|
||||
if (login.length() > 20 || pass.length() > 20)
|
||||
return "{\n\t\"status\":\"big\"\n}";
|
||||
if (login.equals("null") || pass.equals("null"))
|
||||
return "{\n\t\"status\":\"error\"\n}";
|
||||
else {
|
||||
try {
|
||||
User user = UserRepository.getRepository().getUserByLogin(login);
|
||||
if (user == null || !user.getPassword().equals(pass))
|
||||
return "{\n\t\"status\":\"error\"\n}";
|
||||
else
|
||||
return "{\n\t\"status\":\"ok\"\n}";
|
||||
} catch (SQLException e) {
|
||||
return "{\n\t\"status\":\"error\"\n}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/singup")
|
||||
String SingUp(@RequestParam(name = "login", defaultValue = "null") String login, @RequestParam(name = "password", defaultValue = "null") String pass) {
|
||||
if (login.length() > 20 || pass.length() > 20)
|
||||
return "{\n\t\"status\":\"big\",\n\t\"token\":\"null\"\n}";
|
||||
if (login.equals("null") || pass.equals("null"))
|
||||
return "{\n\t\"status\":\"error\",\n\t\"token\":\"null\"\n}";
|
||||
try {
|
||||
User token = UserRepository.getRepository().AddUser(login, pass);
|
||||
return "{\n\t\"status\":\"ok\"\n}";
|
||||
} catch (SQLException e) {
|
||||
return "{\n\t\"status\":\"error\"\n}";
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/user/task")
|
||||
public Task getTask(@RequestParam(value = "login", defaultValue = "null") String login,
|
||||
@RequestParam(value = "password", defaultValue = "null") String pass,
|
||||
@RequestParam(name = "id") long taskID) {
|
||||
if (!login.equals("null") || !pass.equals("null")) {
|
||||
try {
|
||||
User user = UserRepository.getRepository().getUserByLogin(login);
|
||||
|
||||
if (user == null || !user.getPassword().equals(pass))
|
||||
return new Task(0, null, null, null, null);
|
||||
|
||||
List<Task> taskList = user.getTaskList();
|
||||
|
||||
for (Task i : taskList) {
|
||||
if (i.getID() == taskID)
|
||||
return i;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
return new Task(0, null, null, null, null);
|
||||
}
|
||||
}
|
||||
return new Task(0, null, null, null, null);
|
||||
}
|
||||
|
||||
@GetMapping("/user/task/list")
|
||||
public List<Task> getUserTasks(@RequestParam(value = "login", defaultValue = "null") String login,
|
||||
@RequestParam(value = "password", defaultValue = "null") String pass) {
|
||||
if (login.equals("null") || pass.equals("null"))
|
||||
return new ArrayList<>();
|
||||
else {
|
||||
try {
|
||||
User user = UserRepository.getRepository().getUserByLogin(login);
|
||||
if (user == null || !user.getPassword().equals(pass))
|
||||
return new ArrayList<>();
|
||||
else
|
||||
return user.getTaskList();
|
||||
} catch (SQLException e) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/user/task/edit")
|
||||
public void addUserTask(@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;
|
||||
user.addTask(task);
|
||||
} catch (SQLException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/user/task/remove")
|
||||
public void removeUserTask(@RequestParam(value = "login", defaultValue = "null") String login,
|
||||
@RequestParam(value = "password", defaultValue = "null") String pass,
|
||||
@RequestParam(name = "id", defaultValue = "-1") long id) {
|
||||
if (!login.equals("null") && !pass.equals("null") && id != -1) {
|
||||
try {
|
||||
User user = UserRepository.getRepository().getUserByLogin(login);
|
||||
if (user == null || !user.getPassword().equals(pass))
|
||||
return;
|
||||
user.removeTask(id);
|
||||
} catch (SQLException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.application.name=TaskTracker_Server
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.Samsung.TaskTracker_Server;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class TaskTrackerServerApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user