73 lines
2.8 KiB
Java
73 lines
2.8 KiB
Java
package com.example.starter;
|
|
|
|
import io.vertx.core.json.JsonObject;
|
|
import io.vertx.ext.web.RoutingContext;
|
|
import io.vertx.sqlclient.Tuple;
|
|
|
|
public class SetWeatherData {
|
|
private DatabaseService databaseService;
|
|
private SetUser setUser;
|
|
|
|
|
|
public SetWeatherData(DatabaseService ddbs) {
|
|
this.databaseService = ddbs;
|
|
}
|
|
public void setUserHandler(SetUser setUser) {
|
|
this.setUser = setUser;
|
|
}
|
|
public void setRangeData(RoutingContext context) {
|
|
JsonObject body = context.body().asJsonObject();
|
|
if (body == null) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Corps de la requête manquant").encode());
|
|
return;
|
|
}
|
|
String id = body.getString("id");
|
|
Double min = body.getDouble("min");
|
|
Double max = body.getDouble("max");
|
|
String type = body.getString("type");
|
|
|
|
if (id == null || min == null || max == null || type == null) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Paramètres manquants ou invalides").encode());
|
|
return;
|
|
}
|
|
if (!type.matches("^[a-zA-Z_]+$")) {
|
|
context.response()
|
|
.setStatusCode(400)
|
|
.end(new JsonObject().put("error", "Type invalide").encode());
|
|
return;
|
|
}
|
|
String query = String.format("UPDATE range_data SET %s_min=?, %s_max=? WHERE station_id=?", type, type);
|
|
Integer idUser = body.getInteger("idUser");
|
|
databaseService.pool
|
|
.preparedQuery(
|
|
query)
|
|
.execute(Tuple.of(min, max, Integer.parseInt(id)))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD :" + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("Erreur", "Erreur de récupération de la BDD").encode());
|
|
})
|
|
.onSuccess(rows -> {
|
|
if (rows.rowCount() == 0) {
|
|
context.response()
|
|
.setStatusCode(404)
|
|
.end(new JsonObject().put("error", "Objet non trouvé").encode());
|
|
return;
|
|
}
|
|
if (idUser != null) {
|
|
setUser.updateUserPoints(idUser, 1);
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end(new JsonObject().put("success", "Les limites ont bien été mis à jour").encode());
|
|
return;
|
|
});
|
|
}
|
|
|
|
}
|