v0
This commit is contained in:
39
src/main/java/su/xserver/iikocon/SetupHandler.java
Normal file
39
src/main/java/su/xserver/iikocon/SetupHandler.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package su.xserver.iikocon;
|
||||
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
|
||||
public class SetupHandler {
|
||||
private final UserService userService;
|
||||
|
||||
public SetupHandler(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
public void handleSetup(RoutingContext ctx) {
|
||||
// Проверяем, есть ли уже пользователи
|
||||
userService.countUsers().onComplete(ar -> {
|
||||
if (ar.succeeded() && ar.result() == 0) {
|
||||
JsonObject body = ctx.body().asJsonObject();
|
||||
String login = body.getString("login");
|
||||
String password = body.getString("password");
|
||||
|
||||
if (login == null || password == null || login.length() < 3 || password.length() < 6) {
|
||||
ctx.response().setStatusCode(400).end("Invalid login or password (min 3/6 chars)");
|
||||
return;
|
||||
}
|
||||
|
||||
String ip = ctx.request().remoteAddress().host();
|
||||
userService.createUser(login, password, ip).onComplete(cr -> {
|
||||
if (cr.succeeded()) {
|
||||
ctx.response().setStatusCode(201).end(new JsonObject().put("success", true).encode());
|
||||
} else {
|
||||
ctx.response().setStatusCode(500).end("Failed to create admin: " + cr.cause().getMessage());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
ctx.response().setStatusCode(403).end("Setup already completed");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user