Fix bug with rewrite password for user and refactor
This commit is contained in:
@@ -119,7 +119,8 @@ public class DataBase {
|
||||
PreparedStatement getUser_stmt = connection.prepareStatement("SELECT * FROM user WHERE login = ?");
|
||||
getUser_stmt.setString(1, login);
|
||||
ResultSet resultSet = getUser_stmt.executeQuery();
|
||||
resultSet.next();
|
||||
if(!resultSet.next())
|
||||
return null;
|
||||
|
||||
int id = resultSet.getInt(1);
|
||||
String pass = resultSet.getNString(3);
|
||||
|
||||
@@ -9,6 +9,8 @@ public class UserRepository {
|
||||
|
||||
private final List<User> UserCache = new ArrayList<>();
|
||||
|
||||
private UserRepository() {}
|
||||
|
||||
public static synchronized UserRepository getRepository() {
|
||||
if (instance == null) {
|
||||
instance = new UserRepository();
|
||||
@@ -17,6 +19,16 @@ public class UserRepository {
|
||||
}
|
||||
|
||||
public User AddUser(String login, String password) throws SQLException {
|
||||
for (User i : UserCache) {
|
||||
if(i.getLogin().equals(login)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
User find = DataBase.getInstance().FindUser(login);
|
||||
if(find != null)
|
||||
return null;
|
||||
|
||||
User user = new User(0, login, password);
|
||||
|
||||
DataBase.getInstance().SaveUser(user);
|
||||
|
||||
@@ -67,10 +67,15 @@ public class TaskTrakerServerApplication {
|
||||
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"))
|
||||
else if(login.isEmpty() || pass.isEmpty())
|
||||
return "{\n\t\"status\":\"small\",\n\t\"token\":\"null\"\n}";
|
||||
else 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);
|
||||
User user = UserRepository.getRepository().AddUser(login, pass);
|
||||
if(user == null)
|
||||
return "{\n\t\"status\":\"error\"\n}";
|
||||
|
||||
return "{\n\t\"status\":\"ok\"\n}";
|
||||
} catch (SQLException e) {
|
||||
return "{\n\t\"status\":\"error\"\n}";
|
||||
|
||||
Reference in New Issue
Block a user