up
This commit is contained in:
@@ -7,6 +7,9 @@ import io.vertx.sqlclient.Pool;
|
||||
import io.vertx.sqlclient.Row;
|
||||
import io.vertx.sqlclient.templates.SqlTemplate;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -18,6 +21,23 @@ public class RestaurantService {
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
// Хеширование пароля SHA-1
|
||||
private String hashPassword(String password) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
byte[] hash = md.digest(password.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("SHA-1 algorithm not found", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Future<Void> initDatabase() {
|
||||
String createTable = """
|
||||
CREATE TABLE IF NOT EXISTS restaurants (
|
||||
@@ -31,7 +51,6 @@ public class RestaurantService {
|
||||
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
)
|
||||
""";
|
||||
|
||||
return pool.query(createTable).execute().mapEmpty();
|
||||
}
|
||||
|
||||
@@ -42,10 +61,11 @@ public class RestaurantService {
|
||||
}
|
||||
|
||||
public Future<Void> createRestaurant(String name, String login, String password, String host, boolean https) {
|
||||
String hashedPassword = hashPassword(password);
|
||||
Map<String, Object> params = Map.of(
|
||||
"name", name,
|
||||
"login", login,
|
||||
"password", password,
|
||||
"password", hashedPassword,
|
||||
"host", host,
|
||||
"https", https
|
||||
);
|
||||
@@ -55,23 +75,6 @@ public class RestaurantService {
|
||||
.mapEmpty();
|
||||
}
|
||||
|
||||
public Future<JsonObject> findByName(String name) {
|
||||
return SqlTemplate.forQuery(pool,
|
||||
"SELECT id, name, login, password, created, updated, host FROM restaurants WHERE name = #{name}")
|
||||
.mapTo(row -> new JsonObject()
|
||||
.put("id", row.getInteger("id"))
|
||||
.put("name", row.getString("name"))
|
||||
.put("login", row.getString("login"))
|
||||
.put("password", row.getString("password"))
|
||||
.put("created", row.getLocalDateTime("created") != null ?
|
||||
row.getLocalDateTime("created").toString() : null)
|
||||
.put("updated", row.getLocalDateTime("updated") != null ?
|
||||
row.getLocalDateTime("updated").toString() : null)
|
||||
.put("host", row.getString("host")))
|
||||
.execute(Collections.singletonMap("name", name))
|
||||
.map(rows -> rows.iterator().hasNext() ? rows.iterator().next() : null);
|
||||
}
|
||||
|
||||
public Future<JsonArray> getAllRestaurants() {
|
||||
return pool.query("SELECT id, name, login, created, updated, https, host FROM restaurants ORDER BY id")
|
||||
.execute()
|
||||
@@ -118,7 +121,8 @@ public class RestaurantService {
|
||||
params.put("https", https);
|
||||
String sql;
|
||||
if (password != null && !password.isEmpty()) {
|
||||
params.put("password", password);
|
||||
String hashedPassword = hashPassword(password);
|
||||
params.put("password", hashedPassword);
|
||||
sql = "UPDATE restaurants SET name = #{name}, login = #{login}, password = #{password}, host = #{host}, https = #{https} WHERE id = #{id}";
|
||||
} else {
|
||||
sql = "UPDATE restaurants SET name = #{name}, login = #{login}, host = #{host}, https = #{https} WHERE id = #{id}";
|
||||
|
||||
Reference in New Issue
Block a user