From 31eed8f958914b98633275b56646c4f485f51de7 Mon Sep 17 00:00:00 2001 From: vicsash Date: Tue, 21 May 2024 20:48:04 +0200 Subject: [PATCH] Cambiando las opciones de fechas en buscador principal --- .../controllers/BuscadorController.java | 3 +- .../controllers/UserController.java | 10 - .../modelControllers/OfertaController.java | 10 +- .../empresas/OfertaRepository.java | 19 +- .../servicios/empresa/OfertaService.java | 49 ++--- .../resources/templates/buscador_admin.html | 189 +++++++----------- .../resources/templates/buscador_alumno.html | 108 +--------- 7 files changed, 100 insertions(+), 288 deletions(-) diff --git a/src/main/java/com/example/proyectofinal/controllers/BuscadorController.java b/src/main/java/com/example/proyectofinal/controllers/BuscadorController.java index e860ba0..c93e102 100644 --- a/src/main/java/com/example/proyectofinal/controllers/BuscadorController.java +++ b/src/main/java/com/example/proyectofinal/controllers/BuscadorController.java @@ -200,6 +200,7 @@ public class BuscadorController { @RequestParam(defaultValue = "") String secondaryOption, Model model) throws ParseException { System.out.println("Query: TEST " + query); + System.out.println("SecondaryOption: TEST " + secondaryOption); String[] queryMultiWord = query.split(","); List queryList = new ArrayList<>(); for (String queryForList : queryMultiWord) { @@ -209,7 +210,7 @@ public class BuscadorController { } } List itemsPage = Arrays.asList(5, 10, 15, 20, 25, 50); - Page page = null; + Page page; if(queryMultiWord.length>1){ page = ofertasService.getPageMultiWord(pageNum, size, sortField, sortDir, queryList, secondaryOption); diff --git a/src/main/java/com/example/proyectofinal/controllers/UserController.java b/src/main/java/com/example/proyectofinal/controllers/UserController.java index f334a62..66584e8 100644 --- a/src/main/java/com/example/proyectofinal/controllers/UserController.java +++ b/src/main/java/com/example/proyectofinal/controllers/UserController.java @@ -28,18 +28,14 @@ import java.util.Set; @Controller public class UserController { - @Autowired private CicloService cicloService; @Autowired private SkillService skillService; - @Autowired private UsuarioService usuarioService; - @Autowired private PasswordEncoder passwordEncoder; - @Autowired private AlumnoService alumnoService; @@ -122,20 +118,14 @@ public class UserController { @GetMapping("/usuario/info") public String showUserInfo(Model model, Authentication authentication) { - // Get the current user String username = authentication.getName(); Usuario usuario = usuarioService.findByLogInName(username); - if(usuario.getRol().getId() ==1){ throw new ResponseStatusException(HttpStatus.FORBIDDEN, "No tienes permisos para acceder a esta página"); } - Alumno alumno = alumnoService.findByEmail(usuario.getEmail()); - model.addAttribute("usuario", usuario); model.addAttribute("alumno", alumno); - - // Return the name of the view return "user/info_user"; } } diff --git a/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java b/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java index 60955c9..ee35a4c 100644 --- a/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java +++ b/src/main/java/com/example/proyectofinal/controllers/modelControllers/OfertaController.java @@ -17,9 +17,8 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.server.ResponseStatusException; -import java.util.Collection; -import java.util.List; -import java.util.Set; +import java.text.SimpleDateFormat; +import java.util.*; @Controller @RequestMapping() @@ -132,4 +131,9 @@ public class OfertaController { } } + @GetMapping("api/distinct-years") + @ResponseBody + public List getDistinctYears() { + return ofertaService.findDistinctYears(); + } } diff --git a/src/main/java/com/example/proyectofinal/repositories/empresas/OfertaRepository.java b/src/main/java/com/example/proyectofinal/repositories/empresas/OfertaRepository.java index 279684e..4f67967 100644 --- a/src/main/java/com/example/proyectofinal/repositories/empresas/OfertaRepository.java +++ b/src/main/java/com/example/proyectofinal/repositories/empresas/OfertaRepository.java @@ -52,17 +52,11 @@ public interface OfertaRepository extends JpaRepository { @Query("SELECT o FROM Oferta o WHERE o.nombre LIKE %:query%") Page findOfertaByNombre(String query, PageRequest of); - @Query("SELECT o FROM Oferta o WHERE o.fecha < :query") - Page findOfertaByFechaAntes(Date query, PageRequest of); - - @Query("SELECT o FROM Oferta o WHERE o.fecha > :query") - Page findOfertaByFechaDespues(Date query, PageRequest of); - @Query("SELECT o FROM Oferta o WHERE o.sucursal.empresa.nombre LIKE %:query%") Page findOfertaByEmpresa(@Param("query") String query, Pageable pageable); - @Query(value = "SELECT * FROM ofertas o WHERE YEAR(o.fecha) = YEAR(:dateQuery)", nativeQuery = true) - Page findOfertaByFechaAnyo(@Param("dateQuery") Date dateQuery, Pageable pageable); + @Query(value = "SELECT * FROM ofertas o WHERE YEAR(o.fecha) = :yearQuery", nativeQuery = true) + Page findOfertaByFechaAnyo(@Param("yearQuery") Integer yearQuery, Pageable pageable); @Query("SELECT o FROM Oferta o WHERE lower(o.nombre) IN :queryList") Page findOfertasByNombres(@Param("queryList") String queryList, Pageable pageable); @@ -79,15 +73,12 @@ public interface OfertaRepository extends JpaRepository { @Query("SELECT o FROM Oferta o WHERE o.sucursal.empresa.nombre LIKE %:query%") Collection ofertasByEmpresaCust(@Param("query") String query); - @Query("SELECT o FROM Oferta o WHERE o.fecha < :query") - Collection ofertasByFechaAntesCust(@Param("query") Date query); - - @Query("SELECT o FROM Oferta o WHERE o.fecha > :query") - Collection ofertasByFechaDespuesCust(@Param("query") Date query); - @Query("SELECT o FROM Oferta o JOIN o.skills s WHERE s.nombre LIKE %:query%") Collection ofertasBySkillCust(@Param("query") String query); @Query(value = "SELECT * FROM ofertas o WHERE YEAR(o.fecha) = YEAR(:dateQuery)", nativeQuery = true) Collection ofertasByFechaAnyoCust(Date dateQuery); + + @Query(value = "SELECT DISTINCT YEAR(o.fecha) FROM ofertas o", nativeQuery = true) + List findDistinctYears(); } diff --git a/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java b/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java index d5689eb..efa817e 100644 --- a/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java +++ b/src/main/java/com/example/proyectofinal/servicios/empresa/OfertaService.java @@ -108,23 +108,19 @@ public class OfertaService implements IOferta { } @Override public Page getPage(int pageNum, int size, String sortField, String sortDir, String query, String secondaryOption) throws ParseException { - Date dateQuery = null; - if(isDate(query)){ - dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query); - System.out.println("Date query TEST: " + dateQuery); - } + Integer yearQuery = null; + if(isYear(query)){ + yearQuery = Integer.parseInt(query); + System.out.println("Year query TEST: " + yearQuery); + } if(secondaryOption.equalsIgnoreCase("Todo")){ return findAllPaginated(pageNum, size, sortField, sortDir); }else if (secondaryOption.equals("Skill")) { return ofertaRepository.findOfertaBySkill(query, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); } else if(secondaryOption.equals("Nombre")) { return ofertaRepository.findOfertaByNombre(query, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); - } else if(secondaryOption.equals("Fecha antes")) { - return ofertaRepository.findOfertaByFechaAntes(dateQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); - } else if(secondaryOption.equals("Fecha despues")) { - return ofertaRepository.findOfertaByFechaDespues(dateQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); } else if(secondaryOption.equals("Año")) { - return ofertaRepository.findOfertaByFechaAnyo(dateQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); + return ofertaRepository.findOfertaByFechaAnyo(yearQuery, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); } else if(secondaryOption.equals("Empresa")) { return ofertaRepository.findOfertaByEmpresa(query, PageRequest.of(pageNum - 1, size, Sort.by(sortField).ascending())); } else { @@ -154,13 +150,12 @@ public class OfertaService implements IOferta { return new PageImpl<>(allMatches.subList(start, end), PageRequest.of(pageNum - 1, size), allMatches.size()); } - public boolean isDate(String query) { - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // Adjust this to match the date format of your query + public boolean isYear(String query) { try { - Date date = formatter.parse(query); - return true; // The query is a date - } catch (ParseException e) { - return false; // The query is not a date + Integer.parseInt(query); + return true; + } catch (NumberFormatException e) { + return false; } } @@ -179,22 +174,7 @@ public class OfertaService implements IOferta { listEmpPrime.addAll(ofertaRepository.ofertasByEmpresaCust(query)); } else if (secondaryOption.equals("Skill")) { listEmpPrime.addAll(ofertaRepository.ofertasBySkillCust(query)); - }else if(secondaryOption.equals("Fecha antes")){ - try { - Date dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query); - listEmpPrime.addAll(ofertaRepository.ofertasByFechaAntesCust(dateQuery)); - } catch (ParseException e) { - e.printStackTrace(); - } - }else if(secondaryOption.equals("Fecha despues")){ - try { - Date dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query); - listEmpPrime.addAll(ofertaRepository.ofertasByFechaDespuesCust(dateQuery)); - } catch (ParseException e) { - e.printStackTrace(); - - } - }else if(secondaryOption.equals("Año")){ + }else if(secondaryOption.equals("Año")){ try { Date dateQuery = new SimpleDateFormat("yyyy-MM-dd").parse(query); listEmpPrime.addAll(ofertaRepository.ofertasByFechaAnyoCust(dateQuery)); @@ -228,6 +208,7 @@ public class OfertaService implements IOferta { } } } - - + public List findDistinctYears() { + return ofertaRepository.findDistinctYears(); + } } diff --git a/src/main/resources/templates/buscador_admin.html b/src/main/resources/templates/buscador_admin.html index 0c54167..81af655 100644 --- a/src/main/resources/templates/buscador_admin.html +++ b/src/main/resources/templates/buscador_admin.html @@ -47,9 +47,6 @@ justify-content: space-between; width: 100%; } - #date{ - margin-top: 15px; - } #user{ position: relative; margin-left: 50px; @@ -120,14 +117,15 @@ -
- + + +
\ No newline at end of file diff --git a/src/main/resources/templates/buscador_alumno.html b/src/main/resources/templates/buscador_alumno.html index 03b2b2d..3efc461 100644 --- a/src/main/resources/templates/buscador_alumno.html +++ b/src/main/resources/templates/buscador_alumno.html @@ -121,9 +121,6 @@ -
- -
@@ -210,34 +207,9 @@ userUpdated === 'false'; } }); - // Get reference to the date picker - var datePicker = document.getElementById('datePicker'); - - // Add an event listener to the date picker - datePicker.addEventListener('change', function() { - var searchBar = document.querySelector('input[name="query"]'); - var secondaryOption = document.getElementById('secondaryDropdown').value; - - // If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker - if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) { - searchBar.value = datePicker.value; - } - - // Log the value of the search bar to the console - //console.log("Query: " + searchBar.value); - }); document.getElementById('searchForm').addEventListener('submit', function() { var searchOption = document.getElementById('searchOption').value; var secondaryOption = document.getElementById('secondaryDropdown').value; - var datePicker = document.getElementById('datePicker'); - var searchBar = document.querySelector('input[name="query"]'); - - // If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker - if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) { - searchBar.value = datePicker.value; - } - - // Update the value of the hidden input field document.getElementById('hiddenSecondaryOption').value = secondaryOption; this.action = "/buscador/" + searchOption + "/page/1"; }); @@ -246,34 +218,17 @@ var searchBar = document.querySelector('input[name="query"]'); var secondaryDropdown = document.getElementById('secondaryDropdown'); - - primaryDropdown.addEventListener('change', function() { - if (primaryDropdown.value === 'YourCondition') { // replace 'YourCondition' with the actual condition - secondaryDropdown.style.display = 'none'; - } else { - secondaryDropdown.style.display = 'block'; - } - }); - secondaryDropdown.addEventListener('change', function() { - var datePicker = document.getElementById('datePicker'); - if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryDropdown.value)) { - searchBar.value = ''; - searchBar.disabled = true; - datePicker.style.display = 'block'; - } else if (secondaryDropdown.value === 'Todo') { + if (secondaryDropdown.value === 'Todo') { searchBar.value = 'Todo'; searchBar.disabled = true; - datePicker.style.display = 'none'; } else { searchBar.value = ''; searchBar.disabled = false; - datePicker.style.display = 'none'; } }); primaryDropdown.addEventListener('change', function() { - // Clear the secondary dropdown secondaryDropdown.innerHTML = ''; // Determine what to add to the secondary dropdown based on the selected option of the primary dropdown @@ -289,7 +244,7 @@ options = ['Todo', 'Nombre', 'Empresa']; break; case'ofertas': - options = ['Todo', 'Nombre', 'Empresa','Fecha']; + options = ['Todo', 'Nombre', 'Empresa','Año']; break; case'sucursales': options = ['Todo', 'Nombre', 'Empresa','Localidad']; @@ -320,77 +275,18 @@ secondaryDropdown.add(option); } });// Add an event listener to the primary dropdown - primaryDropdown.addEventListener('change', function() { - // Clear the secondary dropdown - secondaryDropdown.innerHTML = ''; - - // Determine what to add to the secondary dropdown based on the selected option of the primary dropdown - var options; - switch (primaryDropdown.value) { - case 'empresas': - options = ['Todo', 'Nombre', 'Sector','Keywords']; - break; - case 'sectores': - options = ['Todo']; - break; - case 'contactos': - options = ['Todo', 'Nombre', 'Empresa']; - break; - case'ofertas': - options = ['Todo', 'Nombre', 'Empresa','Fecha antes','Fecha despues','Año','Skill']; - break; - case'sucursales': - options = ['Todo', 'Nombre', 'Empresa','Localidad']; - break; - case'skills': - options = ['Todo']; - break; - case'familias': - options = ['Todo']; - break; - case'ciclos': - options = ['Todo','Familia','Codigo']; - break; - case'alumnos': - options = ['Todo', 'Nombre', 'Apellido','Ciclo','Keywords','Nia' ,'Dni']; - break; - default: - options = []; - } - // Add the new options to the secondary dropdown - for (var i = 0; i < options.length; i++) { - var option = document.createElement('option'); - option.text = options[i]; - secondaryDropdown.add(option); - } - }); // Trigger the change event manually to populate the secondary dropdown when the page loads primaryDropdown.dispatchEvent(new Event('change')); // Reset the form document.querySelector('form').reset(); - - // Set the value of the search bar to "Todo" and disable it searchBar.value = 'Todo'; searchBar.disabled = true; function submitForm() { var searchOption = document.getElementById('searchOption').value; var secondaryOption = document.getElementById('secondaryDropdown').value; - var datePicker = document.getElementById('datePicker'); - - // If the selected option is 'Fecha antes', 'Fecha despues', or 'Año', set the value of the search bar to the value of the date picker - if (['Fecha antes', 'Fecha despues', 'Año'].includes(secondaryOption)) { - datePicker.name = "query"; - } else { - datePicker.name = ""; - } - - // Log the query to the console - //console.log("Query SENT TEST: " + datePicker.value); - - // Update the value of the hidden input field document.getElementById('hiddenSecondaryOption').value = secondaryOption; this.action = "/buscador/" + searchOption + "/page/1"; return true;