From f5d5b659097cfc4adb4a0f8fb0d7e4a8fa8f32e9 Mon Sep 17 00:00:00 2001 From: Soka_jplr Date: Mon, 6 May 2024 08:01:27 -0600 Subject: [PATCH] backend 1.1 --- backend/Controller/Consultas.java | 3 - backend/src/main/java/mx/uv/App.java | 56 ++++++-- .../main/java/mx/uv/Controller/Conexion.java | 56 ++++++++ .../src/main/java/mx/uv/Controller/DAO.java | 124 ++++++++++++++++++ .../main/java/mx/uv/Model}/Administrador.java | 2 + .../main/java/mx/uv/Model}/Alumno.java | 12 +- .../main/java/mx/uv/Model}/Tutor.java | 1 + 7 files changed, 234 insertions(+), 20 deletions(-) delete mode 100644 backend/Controller/Consultas.java create mode 100644 backend/src/main/java/mx/uv/Controller/Conexion.java create mode 100644 backend/src/main/java/mx/uv/Controller/DAO.java rename backend/{model => src/main/java/mx/uv/Model}/Administrador.java (98%) rename backend/{model => src/main/java/mx/uv/Model}/Alumno.java (92%) rename backend/{model => src/main/java/mx/uv/Model}/Tutor.java (98%) diff --git a/backend/Controller/Consultas.java b/backend/Controller/Consultas.java deleted file mode 100644 index 5ecf421..0000000 --- a/backend/Controller/Consultas.java +++ /dev/null @@ -1,3 +0,0 @@ -public class Consultas { - -} diff --git a/backend/src/main/java/mx/uv/App.java b/backend/src/main/java/mx/uv/App.java index 16b2af1..13e0693 100644 --- a/backend/src/main/java/mx/uv/App.java +++ b/backend/src/main/java/mx/uv/App.java @@ -2,27 +2,63 @@ package mx.uv; import static spark.Spark.*; import java.util.HashMap; -import java.util.UUID; import com.google.gson.*; + +import mx.uv.Controller.DAO; +import mx.uv.Model.*; + public class App { + static Gson gson = new Gson(); + static HashMap usuarios = new HashMap<>(); public static void main( String[] args ) { - System.out.println( "Hello World!" ); - //port(80); + //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); + } + return "OK"; + }); - get("/usuarios", (request, response) ->{ + before((request,response)->response.header("Access-Control-Allow-Origin","*")); + + get("/TodosLosAlumnos", (request, response) ->{ response.type("application/json"); - JsonObject respuesta = new JsonObject(); - respuesta.addProperty("msj", "hola"); - return respuesta; + return gson.toJson(DAO.dameUsuarios()); }); - post("/usuarios", (request, response) ->{ + post("/agregarAlumno", (request, response) ->{ String payload = request.body(); + Alumno alumno = gson.fromJson(payload, Alumno.class); + + boolean msj = DAO.agregarAlumno(new Alumno("Pedro","Lazaro","Mexicano","SAKHBD197", "PezGato")); JsonObject respuesta = new JsonObject(); - respuesta.addProperty("msj", ""); + respuesta.addProperty("msj", msj); return respuesta; }); + put("/EditarAlumno", (request, response) ->{ + String payload = request.body(); + Alumno alumno = gson.fromJson(payload, Alumno.class); + boolean verificado = DAO.editarAlumno(alumno); + JsonObject respuesta = new JsonObject(); + respuesta.addProperty("existe", verificado); + return respuesta; + }); + delete("/EliminarAlumno", (request, response) ->{ + String payload = request.body(); + Alumno alumno = gson.fromJson(payload, Alumno.class); + boolean verificado = DAO.eliminarAlumno(alumno); + JsonObject respuesta = new JsonObject(); + respuesta.addProperty("existe", verificado); + return respuesta; + }); + + } -} +} \ No newline at end of file diff --git a/backend/src/main/java/mx/uv/Controller/Conexion.java b/backend/src/main/java/mx/uv/Controller/Conexion.java new file mode 100644 index 0000000..8537309 --- /dev/null +++ b/backend/src/main/java/mx/uv/Controller/Conexion.java @@ -0,0 +1,56 @@ +package mx.uv.Controller; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.logging.Logger; + +public class Conexion { + + private static Connection conexion; + private static Conexion instancia; + private static final String url = "jdbc:mysql://localhost:3306/universidad"; + private static final String user = "root"; + private static final String password = "Perro16tonto"; + private static final Logger logger = Logger.getLogger(Conexion.class.getName()); + + private Conexion() {} + + public static Conexion getInstance() { + if (instancia == null) { + synchronized (Conexion.class) { + if (instancia == null) { + instancia = new Conexion(); + } + } + } + return instancia; + } + + public Connection conectar() { + try { + Class.forName("com.mysql.cj.jdbc.Driver"); + conexion = DriverManager.getConnection(url, user, password); + if (conexion != null) { + logger.info("Conexión exitosa"); + return conexion; + } else { + logger.severe("ERROR: No se pudo conectar"); + } + } catch (Exception e) { + logger.severe("ERROR: " + e.getMessage()); + } + return null; + } + + public void cerrarConexion() { + try { + if (conexion != null && !conexion.isClosed()) { + conexion.close(); + logger.info("Se desconectó de la base de datos"); + } + } catch (SQLException e) { + logger.severe("ERROR: " + e.getMessage()); + } + } +} diff --git a/backend/src/main/java/mx/uv/Controller/DAO.java b/backend/src/main/java/mx/uv/Controller/DAO.java new file mode 100644 index 0000000..f3cf49d --- /dev/null +++ b/backend/src/main/java/mx/uv/Controller/DAO.java @@ -0,0 +1,124 @@ +package mx.uv.Controller; + +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + + +import mx.uv.Model.Alumno; + +public class DAO { + private static Conexion cn = Conexion.getInstance(); + + public static List dameUsuarios() { + Statement stm = null; + ResultSet rs = null; + Connection conn = null; + List resultado = new ArrayList<>(); + + conn = cn.conectar(); + + try { + String sql = "SELECT * from alumno"; + stm = conn.createStatement(); + rs = stm.executeQuery(sql); + while (rs.next()) { + Alumno u = new Alumno(rs.getInt(1), rs.getString(2), rs.getString(3),rs.getString(4), rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8)); + resultado.add(u); + } + } catch (Exception e) { + System.out.println(e); + } finally { + cerrarConexiones(null, conn); + } + return resultado; + } + public static boolean validarAlumno(Alumno alumno) { + Statement stm = null; + Connection conn = null; + boolean verificacion =false; + ResultSet rs = null; + conn = cn.conectar(); + try { + String sql ="select * from usuarios " + + "where matricula= '"+alumno.getMatricula()+"' and contrasena='"+alumno.getContrasena()+"'"; + stm = (Statement) conn.createStatement(); + rs = stm.executeQuery(sql); + if(rs.next()){ + verificacion = true; + }else{ + verificacion = false; + } + conn.close(); + } catch (SQLException ex) { + System.err.println(ex); + } + return verificacion; + } + + public static boolean agregarAlumno(Alumno alumno) { + PreparedStatement stm = null; + Connection conn = null; + boolean msj= false; + + conn = cn.conectar(); + try { + String sql = "INSERT INTO `alumno`(`nombre`,`apellido`,`nacionalidad`,`matricula`,`contrasena`)VALUES(?,?,?,?,?);"; + stm = (PreparedStatement) conn.prepareStatement(sql); + stm.setString(1, alumno.getNombre()); + stm.setString(2, alumno.getApellido()); + stm.setString(3, alumno.getNacionalidad()); + stm.setString(4, alumno.getMatricula()); + stm.setString(5, alumno.getContrasena()); + if (stm.executeUpdate() > 0) + msj = true; + else + msj = false; + + } catch (Exception e) { + System.out.println(e); + } finally { + cerrarConexiones(stm,conn); + } + return msj; + } + + private static void cerrarConexiones(PreparedStatement stm,Connection conn) { + if (stm != null) { + try { + stm.close(); + } catch (Exception e) { + System.out.println(e); + } + stm = null; + } + try { + conn.close(); + cn.cerrarConexion(); + } catch (Exception e) { + System.out.println(e); + } + } + public static boolean eliminarAlumno(Alumno alumno) { + + return false; + } + public static boolean editarAlumno(Alumno alumno) { + PreparedStatement stm = null; + Connection conn = null; + boolean verificacion =false; + conn = cn.conectar(); + try { + String sql ="UPDATE `producto` SET `nombreProducto` = '"+p.getNombreProducto()+"',`precio` = '"+p.getPrecio()+"',`cantidad` = '"+p.getCantidad()+"',`imagen` = '"+p.getImagen()+"'WHERE `id` = '"+p.getId()+"';"; + stm = conn.prepareStatement(sql); + stm.executeUpdate(); + verificacion = true; + conn.close(); + } catch (SQLException ex) { + System.err.println(ex); + }finally{ + actualizarVenta(p); + } + return verificacion; + } +} diff --git a/backend/model/Administrador.java b/backend/src/main/java/mx/uv/Model/Administrador.java similarity index 98% rename from backend/model/Administrador.java rename to backend/src/main/java/mx/uv/Model/Administrador.java index a400694..4d2ba05 100644 --- a/backend/model/Administrador.java +++ b/backend/src/main/java/mx/uv/Model/Administrador.java @@ -1,3 +1,5 @@ +package mx.uv.Model; + public class Administrador { private int id; private String matricula; diff --git a/backend/model/Alumno.java b/backend/src/main/java/mx/uv/Model/Alumno.java similarity index 92% rename from backend/model/Alumno.java rename to backend/src/main/java/mx/uv/Model/Alumno.java index d7a0f0e..7ab862b 100644 --- a/backend/model/Alumno.java +++ b/backend/src/main/java/mx/uv/Model/Alumno.java @@ -1,4 +1,4 @@ -package model; +package mx.uv.Model; // * Clase Alumno que servira para comunicarse con el controlador. public class Alumno { private int id; @@ -25,23 +25,21 @@ public class Alumno { public Alumno() { } - public Alumno(int id, String nombre, String apellido, String nacionalidad) { - this.id = id; + public Alumno(String nombre, String apellido, String nacionalidad, String matricula, String contrasena) { this.nombre = nombre; this.apellido = apellido; this.nacionalidad = nacionalidad; - } - - public Alumno(int id, String matricula, String contrasena) { - this.id = id; this.matricula = matricula; this.contrasena = contrasena; } + public Alumno(String matricula, String contrasena) { this.matricula = matricula; this.contrasena = contrasena; } + + public void setId(int id) { this.id = id; } diff --git a/backend/model/Tutor.java b/backend/src/main/java/mx/uv/Model/Tutor.java similarity index 98% rename from backend/model/Tutor.java rename to backend/src/main/java/mx/uv/Model/Tutor.java index 117ff18..b410300 100644 --- a/backend/model/Tutor.java +++ b/backend/src/main/java/mx/uv/Model/Tutor.java @@ -1,3 +1,4 @@ +package mx.uv.Model; public class Tutor { private int id; private String nombre;