-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Top 3 bebidas que son más servidas en los bares, teniendo en cuenta solo aquellos bares frecuentados por bebedores y bebidas preferidas por esos bebedores
- Loading branch information
1 parent
a5a44ea
commit 5c592a4
Showing
9 changed files
with
258 additions
and
17 deletions.
There are no files selected for viewing
Binary file not shown.
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
81 changes: 81 additions & 0 deletions
81
demo/src/main/java/uniandes/edu/co/demo/controller/BebedoresController.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,81 @@ | ||
package uniandes.edu.co.demo.controller; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import uniandes.edu.co.demo.modelo.Bebedor; | ||
import uniandes.edu.co.demo.repository.BebedorRepository; | ||
|
||
@RestController | ||
public class BebedoresController { | ||
|
||
@Autowired | ||
private BebedorRepository bebedorRepository; | ||
|
||
// Obtener todos los bebedores | ||
@GetMapping("/bebedores") | ||
public ResponseEntity<Collection<Bebedor>> obtenerTodosLosBebedores() { | ||
try { | ||
// Llamar al método definido en el repositorio para obtener todos los registros | ||
List<Bebedor> bebedores = bebedorRepository.buscarTodosLosBebedores(); | ||
|
||
return ResponseEntity.ok(bebedores); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); | ||
} | ||
} | ||
|
||
// Obtener bebedor por ID | ||
@GetMapping("/bebedores/{id}") | ||
public ResponseEntity<Bebedor> obtenerBebedorPorId(@PathVariable("id") int id) { | ||
try { | ||
Bebedor bebedor = bebedorRepository.buscarBebedorPorId(id); | ||
if (bebedor != null) { | ||
return ResponseEntity.ok(bebedor); | ||
} else { | ||
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | ||
} | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null); | ||
} | ||
} | ||
|
||
// Crear un nuevo bebedor | ||
@PostMapping("/bebedores/new/save") | ||
public ResponseEntity<String> guardarBebedor(@RequestBody Bebedor bebedor) { | ||
try { | ||
bebedorRepository.save(bebedor); | ||
return new ResponseEntity<>("Bebedor creado exitosamente", HttpStatus.CREATED); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>("Error al crear el bebedor", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
// Actualizar un bebedor existente | ||
@PostMapping("/bebedores/{id}/edit/save") | ||
public ResponseEntity<String> editarGuardarBebedor(@PathVariable("id") int id, @RequestBody Bebedor bebedor) { | ||
try { | ||
bebedor.setId(id); | ||
bebedorRepository.save(bebedor); | ||
return new ResponseEntity<>("Bebedor actualizado exitosamente", HttpStatus.OK); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>("Error al actualizar el bebedor", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
// Eliminar un bebedor por ID | ||
@DeleteMapping("/bebedores/{id}/delete") | ||
public ResponseEntity<String> eliminarBebedor(@PathVariable("id") int id) { | ||
try { | ||
bebedorRepository.eliminarBebedorPorId(id); | ||
return new ResponseEntity<>("Bebedor eliminado exitosamente", HttpStatus.OK); | ||
} catch (Exception e) { | ||
return new ResponseEntity<>("Error al eliminar el bebedor", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
demo/src/main/java/uniandes/edu/co/demo/modelo/BebidaConsumo.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,38 @@ | ||
package uniandes.edu.co.demo.modelo; | ||
|
||
public class BebidaConsumo { | ||
private String _id; | ||
private int total; | ||
|
||
public BebidaConsumo() { | ||
} | ||
|
||
public BebidaConsumo(String _id, int total) { | ||
this._id = _id; | ||
this.total = total; | ||
} | ||
|
||
public String getId() { | ||
return _id; | ||
} | ||
|
||
public void setId(String _id) { | ||
this._id = _id; | ||
} | ||
|
||
public int getTotal() { | ||
return total; | ||
} | ||
|
||
public void setTotal(int total) { | ||
this.total = total; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BebidaConsumo{" + | ||
"_id='" + _id + '\'' + | ||
", total=" + total + | ||
'}'; | ||
} | ||
} |
32 changes: 16 additions & 16 deletions
32
demo/src/main/java/uniandes/edu/co/demo/repository/BarRepository.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,38 +1,38 @@ | ||
package uniandes.edu.co.demo.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.data.mongodb.repository.Query; | ||
import org.springframework.data.mongodb.repository.Update; | ||
import java.util.List; | ||
|
||
import uniandes.edu.co.demo.modelo.Bar; | ||
import uniandes.edu.co.demo.modelo.Bebida; | ||
|
||
public interface BarRepository extends MongoRepository<Bar, Integer> { | ||
|
||
// Consulta todos los bares, pero excluye la oferta de bebidas para mejorar el rendimiento | ||
@Query(value = "{}", fields = " { 'oferta_bebidas': 0 }") | ||
// Consultar todos los bares excluyendo la lista de bebidas para mejorar el rendimiento | ||
@Query(value = "{}", fields = "{ 'oferta_bebidas': 0 }") | ||
List<Bar> buscarTodosLosBares(); | ||
|
||
// Busca un bar por su ID | ||
// Consultar bar por su ID | ||
@Query("{_id: ?0}") | ||
List<Bar> buscarPorId(int id); | ||
|
||
// Inserta un nuevo bar usando el método save() heredado de MongoRepository | ||
default void insertarBar(Bar bar) { | ||
save(bar); // save() es proporcionado por MongoRepository | ||
} | ||
// Crear un nuevo bar | ||
@Query("{ $insert: { _id: ?0, nombre: ?1, ciudad: ?2, presupuesto: ?3, cant_sedes: ?4, oferta_bebidas: ?5 } }") | ||
void insertarBar(int id, String nombre, String ciudad, String presupuesto, int cant_sedes, List<Bebida> oferta_bebidas); | ||
|
||
// Actualiza un bar por su ID, modificando los campos nombre, ciudad, presupuesto, etc. | ||
@Query("{_id: ?0 }") | ||
@Update("{ $set: { nombre: ?1, ciudad: ?2, presupuesto: ?3, cant_sedes: ?4, oferta_bebidas: ?5 } }") | ||
// Actualizar un bar por su ID | ||
@Query("{ _id: ?0 }") | ||
@Update("{ $set: { nombre: ?1, ciudad: ?2, presupuesto: ?3, cant_sedes: ?4, oferta_bebidas: ?5 }}") | ||
void actualizarBar(int id, String nombre, String ciudad, String presupuesto, int cant_sedes, List<Bebida> oferta_bebidas); | ||
|
||
// Elimina un bar por su ID | ||
@Query(value = "_id: ?0", delete = true) | ||
// Eliminar un bar por su ID | ||
@Query(value = "{_id: ?0}", delete = true) | ||
void eliminarBarPorId(int id); | ||
|
||
// Obtiene solo la oferta de bebidas de un bar por su ID | ||
@Query(value = "{ _id: ?0 }", fields = "{ 'oferta_bebidas': 1 }") | ||
// Obtener todas las bebidas de un bar por su ID | ||
@Query(value = "{_id: ?0}", fields = "{ 'oferta_bebidas': 1 }") | ||
List<Bebida> obtenerBebidasPorBar(int id); | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
demo/src/main/java/uniandes/edu/co/demo/repository/BarRepositoryCustom.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,64 @@ | ||
package uniandes.edu.co.demo.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.bson.Document; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class BarRepositoryCustom { | ||
|
||
private final MongoTemplate mongoTemplate; | ||
|
||
public BarRepositoryCustom(MongoTemplate mongoTemplate) { | ||
this.mongoTemplate = mongoTemplate; | ||
} | ||
|
||
/** | ||
* Obtiene las bebidas más consumidas basado en bares frecuentados y preferencias. | ||
* | ||
* @return Lista de bebidas más consumidas con su cantidad. | ||
*/ | ||
public List<Document> obtenerBebidasMasConsumidas() { | ||
List<Document> pipeline = List.of( | ||
// Descomponer bares frecuentados en cada bebedor | ||
new Document("$unwind", "$bares_frecuentados"), | ||
// Hacer lookup en la colección de bares | ||
new Document("$lookup", new Document() | ||
.append("from", "bares") | ||
.append("localField", "bares_frecuentados") | ||
.append("foreignField", "_id") | ||
.append("as", "bar_detalle") | ||
), | ||
// Descomponer los detalles del bar | ||
new Document("$unwind", "$bar_detalle"), | ||
// Descomponer la oferta de bebidas del bar | ||
new Document("$unwind", "$bar_detalle.oferta_bebidas"), | ||
// Descomponer las preferencias de cada bebedor | ||
new Document("$unwind", "$preferencias"), | ||
// Filtrar solo las bebidas que están en las preferencias de los bebedores | ||
new Document("$match", new Document() | ||
.append("$expr", new Document() | ||
.append("$eq", List.of("$bar_detalle.oferta_bebidas.nombre", "$preferencias")) | ||
) | ||
), | ||
// Agrupar por nombre de bebida y contar cuántas veces ha sido consumida | ||
new Document("$group", new Document() | ||
.append("_id", "$preferencias") | ||
.append("total", new Document() | ||
.append("$sum", 1) | ||
) | ||
), | ||
// Ordenar las bebidas por total en orden descendente | ||
new Document("$sort", new Document() | ||
.append("total", -1) | ||
), | ||
// Limitar a las 3 más consumidas | ||
new Document("$limit", 3) | ||
); | ||
|
||
// Ejecutar la consulta en la colección "bebedores" y devolver el resultado | ||
return mongoTemplate.getCollection("bebedores").aggregate(pipeline).into(new java.util.ArrayList<>()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
demo/src/main/java/uniandes/edu/co/demo/repository/BebedorRepository.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,25 @@ | ||
package uniandes.edu.co.demo.repository; | ||
|
||
import java.util.List; | ||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.data.mongodb.repository.Query; | ||
import uniandes.edu.co.demo.modelo.Bebedor; | ||
|
||
public interface BebedorRepository extends MongoRepository<Bebedor, Integer> { | ||
|
||
// Consultar todos los bebedores utilizando una consulta nativa de MongoDB | ||
@Query("{}") | ||
List<Bebedor> buscarTodosLosBebedores(); | ||
|
||
// Consultar bebedor por su ID | ||
@Query("{_id: ?0}") | ||
Bebedor buscarBebedorPorId(int id); | ||
|
||
// Consultar bebedores por ciudad | ||
@Query("{'ciudad': ?0}") | ||
List<Bebedor> buscarBebedoresPorCiudad(String ciudad); | ||
|
||
// Eliminar un bebedor por su ID | ||
@Query(value = "{_id: ?0}", delete = true) | ||
void eliminarBebedorPorId(int id); | ||
} |
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,2 +1,2 @@ | ||
spring.data.mongodb.uri=mongodb://user:[email protected]:8087/user | ||
spring.data.mongodb.database= | ||
spring.data.mongodb.database= |