backend 1.1

This commit is contained in:
Soka_jplr 2024-05-06 08:01:27 -06:00
parent 5249c2fdd3
commit f5d5b65909
7 changed files with 234 additions and 20 deletions

View File

@ -1,3 +0,0 @@
public class Consultas {
}

View File

@ -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<String, Alumno> 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;
});
}
}
}

View File

@ -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());
}
}
}

View File

@ -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<Alumno> dameUsuarios() {
Statement stm = null;
ResultSet rs = null;
Connection conn = null;
List<Alumno> 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;
}
}

View File

@ -1,3 +1,5 @@
package mx.uv.Model;
public class Administrador {
private int id;
private String matricula;

View File

@ -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;
}

View File

@ -1,3 +1,4 @@
package mx.uv.Model;
public class Tutor {
private int id;
private String nombre;