From 122b4f43dc142dcdec86883e295b7d87e5d231fa Mon Sep 17 00:00:00 2001 From: Soka_jplr Date: Sun, 19 May 2024 14:31:56 -0600 Subject: [PATCH] cambios en App.java --- backend/src/main/java/mx/uv/App.java | 53 +++++++++++++++------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/backend/src/main/java/mx/uv/App.java b/backend/src/main/java/mx/uv/App.java index 9b7504c..959f0fd 100644 --- a/backend/src/main/java/mx/uv/App.java +++ b/backend/src/main/java/mx/uv/App.java @@ -1,39 +1,39 @@ package mx.uv; + import static spark.Spark.*; import java.util.HashMap; -import com.google.gson.*; +import com.google.gson.*; import mx.uv.Controller.DAO; import mx.uv.Model.*; -public class App -{ +public class App { static Gson gson = new Gson(); static HashMap usuarios = new HashMap<>(); - public static void main( String[] args ) - { - //fuente:https://gist.github.com/saeidzebardast/e375b7d17be3e0f4dddf - options("/*",(request,response)->{ - String accessControlRequestHeaders=request.headers("Access-Control-Request-Headers"); - if(accessControlRequestHeaders!=null){ - response.header("Access-Control-Allow-Headers",accessControlRequestHeaders); + + public static void main(String[] args) { + // fuente:https://gist.github.com/saeidzebardast/e375b7d17be3e0f4dddf + options("/*", (request, response) -> { + String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers"); + if (accessControlRequestHeaders != null) { + 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"; }); - 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"); return gson.toJson(DAO.dameAlumnos()); }); - post("/agregarAlumno", (request, response) ->{ + post("/agregarAlumno", (request, response) -> { String payload = request.body(); Alumno alumno = gson.fromJson(payload, Alumno.class); @@ -44,7 +44,7 @@ public class App respuesta.addProperty("verificacion", msj.isVerificacion()); return respuesta; }); - put("/editarAlumno", (request, response) ->{ + put("/editarAlumno", (request, response) -> { String payload = request.body(); Alumno alumno = gson.fromJson(payload, Alumno.class); boolean verificado = DAO.editarAlumno(alumno); @@ -52,7 +52,7 @@ public class App respuesta.addProperty("Editado", verificado); return respuesta; }); - delete("/eliminarAlumno", (request, response) ->{ + delete("/eliminarAlumno", (request, response) -> { String payload = request.body(); Alumno alumno = gson.fromJson(payload, Alumno.class); boolean verificado = DAO.eliminarAlumno(alumno.getId()); @@ -61,14 +61,19 @@ public class App return respuesta; }); - post("/alumnoIniciado", (request, response) ->{ + get("/alumnoIniciado", (request, response) -> { response.type("application/json"); - String payload = request.body(); - Alumno alumno = gson.fromJson(payload, Alumno.class); - return gson.toJson(DAO.alumnoIniciado(alumno.getMatricula(),alumno.getContrasena())); + String contrasena = request.queryParams("contrasena"); + String matricula = request.queryParams("matricula"); + 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(); Alumno alumno = gson.fromJson(payload, Alumno.class); boolean verificado = DAO.validarAlumno(alumno);