-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gusgonza
committed
May 29, 2024
1 parent
5c0532b
commit e4d361e
Showing
27 changed files
with
797 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Usa la imagen base desde Docker Hub | ||
FROM jgusgonza/my-mongo | ||
|
||
# Exponemos el puerto 3306 para que se pueda acceder a MySQL desde fuera del contenedor | ||
EXPOSE 27017 | ||
|
||
# Establecemos las variables de entorno para MySQL | ||
ENV MYSQL_ROOT_PASSWORD=Sabores1234 | ||
|
||
# Copia el archivo SQL al directorio de inicialización de MySQL | ||
#COPY ../../M02-BBDD/script/script_inicial.sql /docker-entrypoint-initdb.d/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: '3.8' | ||
|
||
services: | ||
mongo: | ||
image: gusgonza/mymongo | ||
container_name: mymongo | ||
ports: | ||
- "27017:27017" | ||
environment: | ||
ME_CONFIG_MONGODB_ADMINUSERNAME: root | ||
ME_CONFIG_MONGODB_ADMINPASSWORD: root | ||
ME_CONFIG_MONGODB_URL: mongodb://root:root@mongo:27017/ | ||
ME_CONFIG_BASICAUTH: "false" | ||
volumes: | ||
- mongo_data:/data/db | ||
restart: always | ||
|
||
volumes: | ||
mongo_data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...cteadesabores_back/src/main/java/abp/project/mesapp/database/DataBaseConnectionMongo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,29 @@ | ||
package abp.project.mesapp.database; | ||
|
||
import abp.project.mesapp.util.CheckError; | ||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class DataBaseConnectionMongo { | ||
public MongoClient connectDB(String uri) throws CheckError{ | ||
// Reduce logging level from MongoDB driver | ||
Logger.getLogger("org.mongodb.driver").setLevel(Level.SEVERE); | ||
MongoClient mongoClient = null; | ||
try { | ||
mongoClient = MongoClients.create(uri); | ||
System.out.println("Connected"); | ||
} catch (Exception e) { | ||
System.out.println("Error connecting"); | ||
throw new CheckError(CheckError.ERROR_CONNECT_BBDD); | ||
} | ||
return mongoClient; | ||
} | ||
|
||
public void disconnectDB(MongoClient mongoClient) { | ||
System.out.println("Disconnected"); | ||
mongoClient.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
M03-PROG/vialacteadesabores_back/src/main/java/abp/project/mesapp/model/Proveedores.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package abp.project.mesapp.model; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import org.bson.Document; | ||
|
||
import java.util.List; | ||
|
||
public class Proveedores { | ||
private int id; | ||
private String nombre; | ||
private String direccion; | ||
private String telefono; | ||
private String email; | ||
private List<Producto> productos; | ||
|
||
|
||
public Proveedores(int id, String nombre, String direccion, String telefono, String email) { | ||
this.id = id; | ||
this.nombre = nombre; | ||
this.direccion = direccion; | ||
this.telefono = telefono; | ||
this.email = email; | ||
this.productos = productos; | ||
} | ||
|
||
public int getId() { | ||
return id; | ||
} | ||
|
||
public String getNombre() { | ||
return nombre; | ||
} | ||
|
||
public String getDireccion() { | ||
return direccion; | ||
} | ||
|
||
public String getTelefono() { | ||
return telefono; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
|
||
public void setNombre(String nombre) { | ||
this.nombre = nombre; | ||
} | ||
|
||
public void setDireccion(String direccion) { | ||
this.direccion = direccion; | ||
} | ||
|
||
public void setTelefono(String telefono) { | ||
this.telefono = telefono; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public List<Producto> getProductos() { | ||
return productos; | ||
} | ||
|
||
public void setProductos(List<Producto> productos) { | ||
this.productos = productos; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Proveedores{" + | ||
"id=" + id + | ||
", nombre='" + nombre + '\'' + | ||
", direccion='" + direccion + '\'' + | ||
", telefono='" + telefono + '\'' + | ||
", email='" + email + '\'' + | ||
'}'; | ||
} | ||
public static void saveProveedorToMongoDB(MongoClient mongoClient, String databaseName, String collectionName, Proveedores proveedor) { | ||
MongoDatabase database = mongoClient.getDatabase(databaseName); | ||
MongoCollection<Document> collection = database.getCollection(collectionName); | ||
|
||
Document proveedorDocument = new Document(); | ||
proveedorDocument.append("id", proveedor.getId()) | ||
.append("nombre", proveedor.getNombre()) | ||
.append("direccion", proveedor.getDireccion()) | ||
.append("telefono", proveedor.getTelefono()) | ||
.append("email", proveedor.getEmail()); | ||
|
||
// Convertir lista de productos a documentos BSON | ||
List<Producto> productos = proveedor.getProductos(); | ||
for (Producto producto : productos) { | ||
Document productoDocument = new Document(); | ||
productoDocument.append("id_producto", producto.getIdproducto()) | ||
.append("nombre", producto.getNombre()) | ||
.append("stock", producto.getStock()) | ||
.append("proveedor", proveedor.getNombre()); // El nombre del proveedor se asocia con cada producto | ||
|
||
proveedorDocument.append("productos", productoDocument); | ||
} | ||
|
||
// Insertar documento del proveedor en la colección | ||
collection.insertOne(proveedorDocument); | ||
} | ||
|
||
|
||
} |
62 changes: 62 additions & 0 deletions
62
M03-PROG/vialacteadesabores_back/src/main/java/abp/project/mesapp/model/Tickets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package abp.project.mesapp.model; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoCollection; | ||
import lombok.Getter; | ||
import org.bson.Document; | ||
|
||
import java.util.Date; | ||
|
||
|
||
public class Tickets { | ||
int order; | ||
int table; | ||
int waiter; | ||
Date date; | ||
int num_diners; | ||
|
||
public Tickets(int order, int table, int waiter, Date date, int num_diners) { | ||
setOrder(order); | ||
setTable(table); | ||
setWaiter(waiter); | ||
setDate(date); | ||
setNumDinners(num_diners); | ||
} | ||
|
||
public void setOrder(int order) { | ||
this.order = order; | ||
} | ||
|
||
public void setTable(int table) { | ||
this.table = table; | ||
} | ||
|
||
public void setWaiter(int waiter) { | ||
this.waiter = waiter; | ||
} | ||
|
||
public void setDate(Date date) { | ||
this.date = date; | ||
} | ||
|
||
public void setNumDinners(int num_diners) { | ||
this.num_diners = num_diners; | ||
} | ||
|
||
public String toString() { | ||
return "Order: " + order + ", Table: " + table + ", Waiter: " + waiter + ", Date: " + date + ", Number of diners: " + num_diners; | ||
} | ||
|
||
public void saveTicket(MongoClient mongoClient, String databaseName, String collectionName) { | ||
MongoCollection<Document> collection = mongoClient.getDatabase(databaseName).getCollection(collectionName); | ||
|
||
Document ticketDocument = new Document(); | ||
ticketDocument.append("order", this.order) | ||
.append("table", this.table) | ||
.append("waiter", this.waiter) | ||
.append("date", this.date) | ||
.append("num_diners", this.num_diners); | ||
|
||
collection.insertOne(ticketDocument); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 27 additions & 7 deletions
34
M03-PROG/vialacteadesabores_back/src/main/java/abp/project/mesapp/util/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,38 @@ | ||
package abp.project.mesapp.util; | ||
|
||
import abp.project.mesapp.database.DataBaseConnection; | ||
import abp.project.mesapp.database.DataBaseConnectionMongo; | ||
import abp.project.mesapp.model.MenuFunciones; | ||
import abp.project.mesapp.model.Producto; | ||
import abp.project.mesapp.model.Proveedores; | ||
import abp.project.mesapp.model.Tickets; | ||
import com.mongodb.client.MongoClient; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Date; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws SQLException { | ||
DataBaseConnection db = new DataBaseConnection(); | ||
db.init(); | ||
MenuFunciones menu = new MenuFunciones(); | ||
menu.menuFunciones(db); | ||
// DESCONECTAR | ||
db.desconectar(); | ||
public static void main(String[] args) throws CheckError { | ||
try{ | ||
DataBaseConnection db = new DataBaseConnection(); | ||
db.init(); | ||
DataBaseConnectionMongo dbMongo = new DataBaseConnectionMongo(); | ||
System.out.println("Conexión establecida"); | ||
MongoClient mongoClient = dbMongo.connectDB(Constantes.uri); | ||
System.out.println("Disconnected"); | ||
dbMongo.disconnectDB(mongoClient); | ||
Tickets ticket = new Tickets(1, 1, 1, new Date(), 1); | ||
ticket.saveTicket(mongoClient, "vialacteadesabores", "tickets"); | ||
Proveedores proveedor = new Proveedores(1, "Proveedor A", "Calle 123", "123456789", "proveedor@example.com"); | ||
Producto producto1 = new Producto(1, "Producto 1", 10, "Proveedor A"); | ||
Producto producto2 = new Producto(2, "Producto 2", 20, "Proveedor A"); | ||
proveedor.getProductos().add(producto1); | ||
proveedor.getProductos().add(producto2); | ||
Proveedores.saveProveedorToMongoDB(mongoClient, "vialacteadesabores", "proveedores", proveedor); | ||
db.desconectar(); | ||
}catch (SQLException e){ | ||
throw new CheckError(CheckError.ERROR_CONNECT_BBDD); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.