Funcionalidad de BackEnd Personalizar Pastel y Pedidos

This commit is contained in:
xFractu 2024-05-12 18:03:42 -06:00
parent 9971d7f9ba
commit 70c96f0341
10 changed files with 211 additions and 89 deletions

View File

@ -353,6 +353,102 @@ public class App
//Hacer Reservaciones:
post("/frontend/hacerPedidoPastel1", (request, response) -> {
response.type("application/json");
String payload = request.body();
try {
JsonElement jsonElement = JsonParser.parseString(payload);
JsonObject jsonObject = jsonElement.getAsJsonObject();
// Obtener los datos del pastel del JSON recibido
String textoEncima = jsonObject.get("textoEncima").getAsString();
String textoCantidad = jsonObject.get("textoCantidad").getAsString();
String textoRelleno = jsonObject.get("textoRelleno").getAsString();
String textoTipo = jsonObject.get("textoTipo").getAsString();
System.out.println("tamaño: " + textoCantidad);
// Crear un nuevo objeto Pasteles y asignar los valores recibidos
Pasteles reservacion = new Pasteles();
reservacion.setIdPedido(UUID.randomUUID().toString());
reservacion.setIdUsuario(idG); // Asignar el ID del usuario (¿De dónde obtienes idG?)
reservacion.setIdPastel("1"); // Asignar el ID del pastel (¿De dónde obtienes este valor?)
reservacion.setIdNombre("Pastel de Fresas"); // Asignar el nombre recibido desde el frontend
reservacion.setIdPrecio("1,000"); // Asignar el precio del pastel (¿De dónde obtienes este valor?)
reservacion.setIdTamaño(textoCantidad); // Asignar el tamaño recibido desde el frontend
reservacion.setStatus("en proceso"); // Asignar el estado "en proceso"
reservacion.setInscripcion(textoEncima); // Asignar la inscripción recibida desde el frontend
reservacion.setTipoRelleno(textoRelleno); // Asignar el tipo de relleno recibido desde el frontend
// Puedes realizar acciones adicionales con la información de la reservación
System.out.println("Reservación: " + reservacion);
// Lógica para hacer la reservación en la base de datos
String mensaje = DAO.hacerPedido(reservacion);
// Crear la respuesta
JsonObject respuesta = new JsonObject();
respuesta.addProperty("msj", mensaje);
return gson.toJson(respuesta);
} catch (JsonSyntaxException e) {
// Manejar errores de formato JSON
System.out.println("Error en el formato JSON: " + e.getMessage());
response.status(400); // Bad Request
return gson.toJson("Error en el formato JSON");
} catch (Exception e) {
// Manejar otros errores
System.out.println("Error en la reservación: " + e.getMessage());
response.status(500); // Internal Server Error
return gson.toJson("Error en la reservación");
}
});
post("/frontend/obtenerPedidosDePasteles", (request, response) -> {
response.type("application/json");
// Obtener el ID del usuario desde la variable global
String idUsuario = idG;
// Obtener los pedidos de pasteles para el usuario
List<Pasteles> pedidosDePasteles = DAO.damePedidosDePastelesPorUsuario(idUsuario);
int numeroDePedidos = pedidosDePasteles.size();
System.out.println("Número de pedidos de pasteles: " + numeroDePedidos);
// Construir un objeto JSON con los pedidos de pasteles
JsonArray pedidosArray = new JsonArray();
for (Pasteles pedido : pedidosDePasteles) {
JsonObject pedidoJson = new JsonObject();
pedidoJson.addProperty("id_pedido", pedido.getIdPedido());
pedidoJson.addProperty("id_usuario", pedido.getIdUsuario());
pedidoJson.addProperty("id_pastel", pedido.getIdPastel());
pedidoJson.addProperty("nombre_pastel", pedido.getIdNombre());
pedidoJson.addProperty("precio", pedido.getIdPrecio());
pedidoJson.addProperty("tamaño", pedido.getIdTamaño());
pedidoJson.addProperty("estatus", pedido.getStatus());
pedidoJson.addProperty("inscripcion", pedido.getInscripcion());
pedidoJson.addProperty("relleno", pedido.getTipoRelleno());
pedidosArray.add(pedidoJson);
}
// Crear el objeto final que contiene todos los pedidos de pasteles
JsonObject responseJson = new JsonObject();
responseJson.add("pedidos_de_pasteles", pedidosArray);
System.out.println(responseJson);
return responseJson.toString();
});
}

View File

@ -5,10 +5,10 @@ import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexion {
private static String url = "jdbc:mysql://127.0.0.1:3306/dbpastel";
private static String url = "jdbc:mysql://127.0.0.1:3306/dbpastel00";
private static String driverName = "com.mysql.cj.jdbc.Driver";
private static String username = "root";
private static String password = "cesarin_11";
private static String username = "pastel00";
private static String password = "pastel00";
// variable de conexion
private static Connection connection = null;

View File

@ -271,6 +271,8 @@ public class DAO {
}
}
public static Usuario obtenerDatosUsuario(String id) {
PreparedStatement stm = null;
Connection conn = null;
@ -316,5 +318,98 @@ public class DAO {
}
}
}
public static String hacerPedido(Pasteles pasteles) {
PreparedStatement stm = null;
Connection conn = null;
String mensaje = "";
conn = Conexion.getConnection();
try {
String sql = "INSERT INTO pedidos (idUsuario, idPastel, precio, tamaño, estatus, inscripcion, relleno) VALUES (?,?,?,?,?,?,?)";
stm = conn.prepareStatement(sql);
stm.setString(1, pasteles.getIdUsuario());
stm.setString(2, pasteles.getIdPastel());
stm.setString(3, pasteles.getIdPrecio());
stm.setString(4, pasteles.getIdTamaño());
stm.setString(5, pasteles.getStatus());
stm.setString(6, pasteles.getInscripcion());
stm.setString(7, pasteles.getTipoRelleno());
if (stm.executeUpdate() > 0)
mensaje = "Pedido realizado con éxito";
else
mensaje = "No se pudo realizar el pedido";
} catch (SQLException e) {
System.out.println("Error al ejecutar la consulta: " + e.getMessage());
} finally {
if (stm != null) {
try {
stm.close();
} catch (SQLException e) {
System.out.println("Error al cerrar el statement: " + e.getMessage());
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("Error al cerrar la conexión: " + e.getMessage());
}
}
}
return mensaje;
}
public static List<Pasteles> damePedidosDePastelesPorUsuario(String idUsuario) {
System.out.println("ENTRO AL METODO: damePedidosDePastelesPorUsuario");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Pasteles> resultado = new ArrayList<>();
try {
conn = Conexion.getConnection();
String sql = "SELECT p.idPedido, p.idUsuario, p.idPastel, pastel.nombreP as nombre_pastel, pastel.precio, "
+ "p.tamaño, p.estatus, p.inscripcion, p.relleno "
+ "FROM pedidos p "
+ "JOIN pasteles pastel ON p.idPastel = pastel.id "
+ "WHERE p.idUsuario = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, idUsuario);
rs = pstmt.executeQuery();
while (rs.next()) {
Pasteles pastel = new Pasteles(
rs.getString("idPedido"),
rs.getString("idUsuario"),
rs.getString("idPastel"),
rs.getString("nombre_pastel"),
rs.getString("precio"),
rs.getString("tamaño"),
rs.getString("estatus"),
rs.getString("inscripcion"),
rs.getString("relleno")
);
resultado.add(pastel);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return resultado;
}
}

View File

@ -4,6 +4,7 @@ public class Pasteles {
String idPedido;
String idUsuario;
String idPastel;
String idNombre;
String idPrecio;
String idTamaño;
@ -27,10 +28,18 @@ public class Pasteles {
this.idUsuario = idUsuario;
}
public String getIdPastel() {
return idPastel;
}
public void setIdPastel(String idPastel) {
this.idPastel = idPastel;
}
public String getIdNombre() {
return idNombre;
}
public void setIdNombre(String idNombre) {
this.idNombre = idNombre;
}
@ -76,7 +85,7 @@ public class Pasteles {
}
public String toString() {
return "Pasteles [idPedido=" + idPedido + ", idUsuario=" + idUsuario + ", idNombre=" + idNombre + ", idPrecio=" + idPrecio
return "Pasteles [idPedido=" + idPedido + ", idUsuario=" + idUsuario + ", idPastel=" + idPastel + ", idNombre=" + idNombre + ", idPrecio=" + idPrecio
+ ", idTamaño=" + idTamaño + ", status=" + status + ", inscripcion=" + inscripcion + ", tipoRelleno=" + tipoRelleno + "]";
}
@ -84,10 +93,11 @@ public class Pasteles {
}
public Pasteles(String idPedido, String idUsuario, String idNombre, String idPrecio, String idTamaño,
public Pasteles(String idPedido, String idUsuario, String idPastel, String idNombre, String idPrecio, String idTamaño,
String status, String inscripcion, String tipoRelleno) {
this.idPedido = idPedido;
this.idUsuario = idUsuario;
this.idPastel = idPastel;
this.idNombre = idNombre;
this.idPrecio = idPrecio;
this.idTamaño = idTamaño;
@ -95,4 +105,8 @@ public class Pasteles {
this.inscripcion = inscripcion;
this.tipoRelleno = tipoRelleno;
}
}

View File

@ -1,83 +0,0 @@
package mx.uv;
public class Reservaciones {
String idR;
String idU;
String idH;
String nombre;
String precio;
String checkIn;
String checkOut;
String personas;
public String getIdR() {
return idR;
}
public void setIdR(String idR) {
this.idR = idR;
}
public String getIdU() {
return idU;
}
public void setIdU(String idU) {
this.idU = idU;
}
public String getIdH() {
return idH;
}
public void setIdH(String idH) {
this.idH = idH;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getPrecio() {
return precio;
}
public void setPrecio(String precio) {
this.precio = precio;
}
public String getCheckIn() {
return checkIn;
}
public void setCheckIn(String checkIn) {
this.checkIn = checkIn;
}
public String getCheckOut() {
return checkOut;
}
public void setCheckOut(String checkOut) {
this.checkOut = checkOut;
}
public String getPersonas() {
return personas;
}
public void setPersonas(String personas) {
this.personas = personas;
}
public String toString() {
return "Reservaciones [id=" + idR + ", nombre=" + nombre + ", precio=" + precio + ", checkIn=" + checkIn + ", checkOut=" + checkOut + ", personas=" + personas +"]";
}
public Reservaciones(){
}
public Reservaciones(String idR, String idU, String idH, String nombre, String precio, String checkIn,
String checkOut, String personas) {
this.idR = idR;
this.idU = idU;
this.idH = idH;
this.nombre = nombre;
this.precio = precio;
this.checkIn = checkIn;
this.checkOut = checkOut;
this.personas = personas;
}
}