208 lines
9.2 KiB
Java
208 lines
9.2 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 SetObjects {
|
|
private DatabaseService databaseService;
|
|
private SetUser setUser;
|
|
|
|
public SetObjects(DatabaseService ddbs) {
|
|
this.databaseService = ddbs;
|
|
}
|
|
|
|
public void setUserHandler(SetUser setUser) {
|
|
this.setUser = setUser;
|
|
}
|
|
|
|
public void setInfoObjet(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;
|
|
}
|
|
Integer id = body.getInteger("id");
|
|
Integer idUser = body.getInteger("idUser");
|
|
String description = body.getString("description");
|
|
String type = body.getString("type");
|
|
String location = body.getString("location");
|
|
String status = body.getString("status");
|
|
|
|
databaseService.pool
|
|
.preparedQuery(
|
|
"UPDATE weather_objects SET description=?,type=?,location=?,status=?,last_update=CURRENT_TIMESTAMP WHERE id=?")
|
|
.execute(Tuple.of(description, type, location, status, 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;
|
|
}
|
|
Boolean shouldUpdatePoints = body.getBoolean("shouldUpdatePoints", false);
|
|
|
|
if (Boolean.TRUE.equals(shouldUpdatePoints) && idUser != null) {
|
|
setUser.updateUserPoints(idUser, 1);
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end(new JsonObject().put("success", "L'objet à bien été mis à jour").encode());
|
|
return;
|
|
});
|
|
}
|
|
|
|
public void deleteCategories(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 name = body.getString("name");
|
|
databaseService.pool
|
|
.preparedQuery("DELETE FROM categories WHERE name=?")
|
|
.execute(Tuple.of(name))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD :" + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("error", "Erreur de récupération de la BDD").encode());
|
|
})
|
|
.onSuccess(rows -> {
|
|
if (rows.rowCount() == 0) {
|
|
context.response()
|
|
.setStatusCode(404)
|
|
.end(new JsonObject().put("error", "Catégorie non trouvé").encode());
|
|
return;
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end(new JsonObject().put("success", "La catégorie à bien été supprimé").encode());
|
|
return;
|
|
});
|
|
}
|
|
|
|
public void deleteObject(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");
|
|
databaseService.pool
|
|
.preparedQuery("DELETE FROM weather_objects WHERE id=?")
|
|
.execute(Tuple.of(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("error", "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;
|
|
}
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end(new JsonObject().put("success", "L'objet à bien été supprimé").encode());
|
|
return;
|
|
});
|
|
|
|
}
|
|
|
|
public void newObject(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;
|
|
}
|
|
Integer idUser = body.getInteger("proprio_id");
|
|
String name = body.getString("nom");
|
|
String description = body.getString("description");
|
|
String type = body.getString("type");
|
|
String location = body.getString("location");
|
|
String status = body.getString("status");
|
|
String batterieType = body.getString("batterieType");
|
|
Integer proprio_id = body.getInteger("proprio_id");
|
|
databaseService.pool
|
|
.preparedQuery(
|
|
"INSERT INTO weather_objects (name,description,type,location,status,type_batterie,proprio_id) VALUES (?,?,?,?,?,?,?)")
|
|
.execute(Tuple.of(name, description, type, location, status, batterieType, proprio_id))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD :" + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("error", "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, 2);
|
|
}
|
|
;
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end(new JsonObject().put("success", "L'objet à bien été ajouté").encode());
|
|
return;
|
|
});
|
|
}
|
|
|
|
public void newCategorie(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 name = body.getString("name");
|
|
|
|
databaseService.pool
|
|
.preparedQuery(
|
|
"INSERT INTO categories (name) VALUES (?)")
|
|
.execute(Tuple.of(name))
|
|
.onFailure(e -> {
|
|
System.err.println("Erreur de récupération de la BDD :" + e.getMessage());
|
|
context.response()
|
|
.setStatusCode(500)
|
|
.end(new JsonObject().put("error", "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;
|
|
}
|
|
|
|
context.response()
|
|
.putHeader("content-type", "application/json: charset=UTF-8")
|
|
.end(new JsonObject().put("success", "La catégorie à bien été ajouté").encode());
|
|
return;
|
|
});
|
|
}
|
|
|
|
}
|