v01
This commit is contained in:
@@ -30,48 +30,59 @@ public class UserService {
|
||||
ip VARCHAR(45)
|
||||
)
|
||||
""";
|
||||
|
||||
return pool.query(createTable).execute().mapEmpty();
|
||||
}
|
||||
|
||||
public Future<Long> countUsers() {
|
||||
return pool.query("SELECT COUNT(*) AS cnt FROM users").execute()
|
||||
return pool.query("SELECT COUNT(*) AS cnt FROM users")
|
||||
.execute()
|
||||
.map(rows -> rows.iterator().next().getLong("cnt"));
|
||||
}
|
||||
|
||||
public Future<Void> createUser(String login, String password, String ip) {
|
||||
String hash = BCrypt.hashpw(password, BCrypt.gensalt());
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("login", login);
|
||||
params.put("password", hash);
|
||||
params.put("ip", ip);
|
||||
return SqlTemplate.forUpdate(pool, "INSERT INTO users (login, password, ip) VALUES (#{login}, #{password}, #{ip})")
|
||||
|
||||
return SqlTemplate.forUpdate(pool,
|
||||
"INSERT INTO users (login, password, ip) VALUES (#{login}, #{password}, #{ip})")
|
||||
.execute(params)
|
||||
.mapEmpty();
|
||||
}
|
||||
|
||||
public Future<JsonObject> findByLogin(String login) {
|
||||
return SqlTemplate.forQuery(pool, "SELECT id, login, password, created, updated, ip FROM users WHERE login = #{login}")
|
||||
return SqlTemplate.forQuery(pool,
|
||||
"SELECT id, login, password, created, updated, ip FROM users WHERE login = #{login}")
|
||||
.mapTo(row -> new JsonObject()
|
||||
.put("id", row.getInteger("id"))
|
||||
.put("login", row.getString("login"))
|
||||
.put("password", row.getString("password"))
|
||||
.put("created", row.getLocalDateTime("created").toString())
|
||||
.put("updated", row.getLocalDateTime("updated").toString())
|
||||
.put("created", row.getLocalDateTime("created") != null ?
|
||||
row.getLocalDateTime("created").toString() : null)
|
||||
.put("updated", row.getLocalDateTime("updated") != null ?
|
||||
row.getLocalDateTime("updated").toString() : null)
|
||||
.put("ip", row.getString("ip")))
|
||||
.execute(Collections.singletonMap("login", login))
|
||||
.map(rows -> rows.iterator().hasNext() ? rows.iterator().next() : null);
|
||||
}
|
||||
|
||||
public Future<JsonArray> getAllUsers() {
|
||||
return pool.query("SELECT id, login, created, updated, ip FROM users ORDER BY id").execute()
|
||||
return pool.query("SELECT id, login, created, updated, ip FROM users ORDER BY id")
|
||||
.execute()
|
||||
.map(rows -> {
|
||||
JsonArray array = new JsonArray();
|
||||
for (Row row : rows) {
|
||||
array.add(new JsonObject()
|
||||
.put("id", row.getInteger("id"))
|
||||
.put("login", row.getString("login"))
|
||||
.put("created", row.getLocalDateTime("created").toString())
|
||||
.put("updated", row.getLocalDateTime("updated").toString())
|
||||
.put("created", row.getLocalDateTime("created") != null ?
|
||||
row.getLocalDateTime("created").toString() : null)
|
||||
.put("updated", row.getLocalDateTime("updated") != null ?
|
||||
row.getLocalDateTime("updated").toString() : null)
|
||||
.put("ip", row.getString("ip")));
|
||||
}
|
||||
return array;
|
||||
@@ -79,6 +90,10 @@ public class UserService {
|
||||
}
|
||||
|
||||
public boolean checkPassword(String plain, String hash) {
|
||||
return BCrypt.checkpw(plain, hash);
|
||||
try {
|
||||
return BCrypt.checkpw(plain, hash);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user