up...
This commit is contained in:
@@ -8,6 +8,7 @@ import io.vertx.core.Future;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
@@ -26,11 +27,13 @@ import su.xserver.iikocon.config.AppConfig;
|
||||
import su.xserver.iikocon.handler.*;
|
||||
import su.xserver.iikocon.iiko.IikoHandler;
|
||||
import su.xserver.iikocon.iiko.IikoOlapClient;
|
||||
import su.xserver.iikocon.iiko.OlapQueryService;
|
||||
import su.xserver.iikocon.service.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MainVerticle extends AbstractVerticle {
|
||||
|
||||
@@ -46,6 +49,7 @@ public class MainVerticle extends AbstractVerticle {
|
||||
private RestaurantService restaurantService;
|
||||
private ExternalDataBaseService externalDataBaseService;
|
||||
private SettingsService settingsService;
|
||||
private OlapQueryService olapQueryService;
|
||||
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) throws ClassNotFoundException {
|
||||
@@ -75,6 +79,7 @@ public class MainVerticle extends AbstractVerticle {
|
||||
restaurantService = new RestaurantService(db.getPool());
|
||||
settingsService = new SettingsService(db.getPool());
|
||||
externalDataBaseService = new ExternalDataBaseService(db.getPool(), vertx);
|
||||
olapQueryService = new OlapQueryService(db.getPool(), externalDataBaseService);
|
||||
|
||||
userService.initDatabase().onFailure(err -> {
|
||||
log.error("Failed to initialize database", err);
|
||||
@@ -92,6 +97,10 @@ public class MainVerticle extends AbstractVerticle {
|
||||
log.error("Failed to initialize database", err);
|
||||
startPromise.fail(err);
|
||||
});
|
||||
olapQueryService.initDatabase().onFailure(err -> {
|
||||
log.error("Failed to initialize database", err);
|
||||
startPromise.fail(err);
|
||||
});
|
||||
|
||||
createRouterAndStartHttp(startPromise);
|
||||
|
||||
@@ -517,6 +526,98 @@ public class MainVerticle extends AbstractVerticle {
|
||||
|
||||
new IikoHandler(vertx, router, db, restaurantService, authHandler);
|
||||
|
||||
|
||||
// Роуты для OLAP запросов
|
||||
router.get("/api/olap/queries").handler(authHandler::requireAuth).handler(rc -> {
|
||||
olapQueryService.getAllQueries()
|
||||
.onSuccess(queries -> rc.response().putHeader("Content-Type", "application/json").end(queries.encode()))
|
||||
.onFailure(err -> rc.response().setStatusCode(500).end(err.getMessage()));
|
||||
});
|
||||
|
||||
router.get("/api/olap/queries/:id").handler(authHandler::requireAuth).handler(rc -> {
|
||||
int id = Integer.parseInt(rc.pathParam("id"));
|
||||
olapQueryService.getQueryById(id)
|
||||
.onSuccess(query -> {
|
||||
if (query == null) rc.response().setStatusCode(404).end();
|
||||
else rc.response().putHeader("Content-Type", "application/json").end(query.encode());
|
||||
})
|
||||
.onFailure(err -> rc.response().setStatusCode(500).end(err.getMessage()));
|
||||
});
|
||||
|
||||
router.post("/api/olap/queries").handler(authHandler::requireAuth).handler(rc -> {
|
||||
JsonObject body = rc.body().asJsonObject();
|
||||
String name = body.getString("name");
|
||||
Integer dbConnectionId = body.getInteger("dbConnectionId");
|
||||
JsonObject config = body.getJsonObject("config");
|
||||
JsonArray restaurantIdsArray = body.getJsonArray("restaurantIds", new JsonArray());
|
||||
List<Integer> restaurantIds = restaurantIdsArray.stream().map(id -> (Integer) id).collect(Collectors.toList());
|
||||
|
||||
if (name == null || dbConnectionId == null || config == null) {
|
||||
rc.response().setStatusCode(400).end("Missing required fields");
|
||||
return;
|
||||
}
|
||||
|
||||
// Сначала генерируем SQL
|
||||
olapQueryService.generateSql(config, dbConnectionId)
|
||||
.compose(sql -> olapQueryService.createQuery(name, dbConnectionId, config, restaurantIds, sql))
|
||||
.onSuccess(id -> rc.response().setStatusCode(201).putHeader("Content-Type", "application/json").end(new JsonObject().put("id", id).encode()))
|
||||
.onFailure(err -> rc.response().setStatusCode(500).end(err.getMessage()));
|
||||
});
|
||||
|
||||
router.put("/api/olap/queries/:id").handler(authHandler::requireAuth).handler(rc -> {
|
||||
int id = Integer.parseInt(rc.pathParam("id"));
|
||||
JsonObject body = rc.body().asJsonObject();
|
||||
String name = body.getString("name");
|
||||
Integer dbConnectionId = body.getInteger("dbConnectionId");
|
||||
JsonObject config = body.getJsonObject("config");
|
||||
JsonArray restaurantIdsArray = body.getJsonArray("restaurantIds", new JsonArray());
|
||||
List<Integer> restaurantIds = restaurantIdsArray.stream().map(v -> (Integer) v).collect(Collectors.toList());
|
||||
|
||||
if (name == null || dbConnectionId == null || config == null) {
|
||||
rc.response().setStatusCode(400).end("Missing required fields");
|
||||
return;
|
||||
}
|
||||
|
||||
olapQueryService.generateSql(config, dbConnectionId)
|
||||
.compose(sql -> olapQueryService.updateQuery(id, name, dbConnectionId, config, restaurantIds, sql))
|
||||
.onSuccess(v -> rc.response().end())
|
||||
.onFailure(err -> rc.response().setStatusCode(500).end(err.getMessage()));
|
||||
});
|
||||
|
||||
router.delete("/api/olap/queries/:id").handler(authHandler::requireAuth).handler(rc -> {
|
||||
int id = Integer.parseInt(rc.pathParam("id"));
|
||||
olapQueryService.deleteQuery(id)
|
||||
.onSuccess(v -> rc.response().end())
|
||||
.onFailure(err -> rc.response().setStatusCode(500).end(err.getMessage()));
|
||||
});
|
||||
|
||||
router.post("/api/olap/generate-sql").handler(authHandler::requireAuth).handler(rc -> {
|
||||
JsonObject body = rc.body().asJsonObject();
|
||||
JsonObject config = body.getJsonObject("config");
|
||||
Integer dbConnectionId = body.getInteger("dbConnectionId");
|
||||
if (config == null || dbConnectionId == null) {
|
||||
rc.response().setStatusCode(400).end("Missing config or dbConnectionId");
|
||||
return;
|
||||
}
|
||||
olapQueryService.generateSql(config, dbConnectionId)
|
||||
.onSuccess(sql -> rc.response().putHeader("Content-Type", "text/plain").end(sql))
|
||||
.onFailure(err -> rc.response().setStatusCode(500).end(err.getMessage()));
|
||||
});
|
||||
|
||||
router.post("/api/olap/export-json").handler(authHandler::requireAuth).handler(rc -> {
|
||||
JsonObject body = rc.body().asJsonObject();
|
||||
JsonObject config = body.getJsonObject("config");
|
||||
if (config == null) {
|
||||
rc.response().setStatusCode(400).end("Missing config");
|
||||
return;
|
||||
}
|
||||
JsonObject fullJson = olapQueryService.generateFullIikoJson(config);
|
||||
rc.response()
|
||||
.putHeader("Content-Type", "application/json")
|
||||
.putHeader("Content-Disposition", "attachment; filename=olap_export.json")
|
||||
.end(fullJson.encodePrettily());
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user