cambios en App.java

This commit is contained in:
Soka_jplr 2024-05-19 14:31:56 -06:00
parent 7b37079daa
commit 122b4f43dc
1 changed files with 29 additions and 24 deletions

View File

@ -1,39 +1,39 @@
package mx.uv; package mx.uv;
import static spark.Spark.*; import static spark.Spark.*;
import java.util.HashMap; import java.util.HashMap;
import com.google.gson.*; import com.google.gson.*;
import mx.uv.Controller.DAO; import mx.uv.Controller.DAO;
import mx.uv.Model.*; import mx.uv.Model.*;
public class App public class App {
{
static Gson gson = new Gson(); static Gson gson = new Gson();
static HashMap<String, Alumno> usuarios = new HashMap<>(); static HashMap<String, Alumno> usuarios = new HashMap<>();
public static void main( String[] args )
{ public static void main(String[] args) {
//fuente:https://gist.github.com/saeidzebardast/e375b7d17be3e0f4dddf // fuente:https://gist.github.com/saeidzebardast/e375b7d17be3e0f4dddf
options("/*",(request,response)->{ options("/*", (request, response) -> {
String accessControlRequestHeaders=request.headers("Access-Control-Request-Headers"); String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
if(accessControlRequestHeaders!=null){ if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers",accessControlRequestHeaders); response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
}
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
} }
String accessControlRequestMethod=request.headers("Access-Control-Request-Method");
if(accessControlRequestMethod!=null){
response.header("Access-Control-Allow-Methods",accessControlRequestMethod);
}
return "OK"; return "OK";
}); });
before((request,response)->response.header("Access-Control-Allow-Origin","*")); before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));
get("/todosLosAlumnos", (request, response) ->{ get("/todosLosAlumnos", (request, response) -> {
response.type("application/json"); response.type("application/json");
return gson.toJson(DAO.dameAlumnos()); return gson.toJson(DAO.dameAlumnos());
}); });
post("/agregarAlumno", (request, response) ->{ post("/agregarAlumno", (request, response) -> {
String payload = request.body(); String payload = request.body();
Alumno alumno = gson.fromJson(payload, Alumno.class); Alumno alumno = gson.fromJson(payload, Alumno.class);
@ -44,7 +44,7 @@ public class App
respuesta.addProperty("verificacion", msj.isVerificacion()); respuesta.addProperty("verificacion", msj.isVerificacion());
return respuesta; return respuesta;
}); });
put("/editarAlumno", (request, response) ->{ put("/editarAlumno", (request, response) -> {
String payload = request.body(); String payload = request.body();
Alumno alumno = gson.fromJson(payload, Alumno.class); Alumno alumno = gson.fromJson(payload, Alumno.class);
boolean verificado = DAO.editarAlumno(alumno); boolean verificado = DAO.editarAlumno(alumno);
@ -52,7 +52,7 @@ public class App
respuesta.addProperty("Editado", verificado); respuesta.addProperty("Editado", verificado);
return respuesta; return respuesta;
}); });
delete("/eliminarAlumno", (request, response) ->{ delete("/eliminarAlumno", (request, response) -> {
String payload = request.body(); String payload = request.body();
Alumno alumno = gson.fromJson(payload, Alumno.class); Alumno alumno = gson.fromJson(payload, Alumno.class);
boolean verificado = DAO.eliminarAlumno(alumno.getId()); boolean verificado = DAO.eliminarAlumno(alumno.getId());
@ -61,14 +61,19 @@ public class App
return respuesta; return respuesta;
}); });
post("/alumnoIniciado", (request, response) ->{ get("/alumnoIniciado", (request, response) -> {
response.type("application/json"); response.type("application/json");
String payload = request.body(); String contrasena = request.queryParams("contrasena");
Alumno alumno = gson.fromJson(payload, Alumno.class); String matricula = request.queryParams("matricula");
return gson.toJson(DAO.alumnoIniciado(alumno.getMatricula(),alumno.getContrasena())); Alumno alumno = new Alumno(matricula, contrasena);
if (contrasena == null || matricula == null) {
response.status(400); // Bad request
return gson.toJson("Missing matricula or contrasena");
}
return gson.toJson(DAO.alumnoIniciado(alumno.getMatricula(), alumno.getContrasena()));
}); });
post("/usuarioValido", (request, response) ->{ post("/usuarioValido", (request, response) -> {
String payload = request.body(); String payload = request.body();
Alumno alumno = gson.fromJson(payload, Alumno.class); Alumno alumno = gson.fromJson(payload, Alumno.class);
boolean verificado = DAO.validarAlumno(alumno); boolean verificado = DAO.validarAlumno(alumno);