From 52078ec26006992640f2a25a78b5019574f69091 Mon Sep 17 00:00:00 2001 From: Joel Date: Tue, 2 Nov 2021 16:52:33 -0300 Subject: [PATCH] listar tecnologias existentes --- curriculumsearch/pom.xml | 6 ++++++ curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java | 15 +++++++++++++-- curriculumsearch/src/main/java/com/roshka/modelo/PostulanteTecnologia.java | 6 ++++-- curriculumsearch/src/main/resources/application.properties | 5 ++++- curriculumsearch/src/main/resources/static/main.js | 7 +++++-- curriculumsearch/src/main/webapp/jsp/postulante-form.jsp | 18 ++++++++++++++++-- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/curriculumsearch/pom.xml b/curriculumsearch/pom.xml index b88363f..40c2bd1 100644 --- a/curriculumsearch/pom.xml +++ b/curriculumsearch/pom.xml @@ -57,6 +57,12 @@ hibernate-validator 6.0.13.Final + + + javax.servlet + jstl + 1.2 + diff --git a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java index 704c094..7e4eec7 100644 --- a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java +++ b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java @@ -6,11 +6,13 @@ import javax.validation.ConstraintViolationException; import com.roshka.modelo.Postulante; import com.roshka.repositorio.PostulanteRepository; +import com.roshka.repositorio.TecnologiaRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.*; @@ -20,19 +22,28 @@ public class PostulanteController { @Autowired PostulanteRepository post; + @Autowired + TecnologiaRepository tecRepo; + @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/postulante") - public String getFormPostulante(){ - + public String getFormPostulante(Model model){ + model.addAttribute("tecnologias", tecRepo.findAll()); return "postulante-form"; } @PostMapping(value = "/postulante",consumes = "application/json") public String guardarPostulante(@RequestBody Postulante postulante){ + //se obtiene referencia de todas las tecnologias existentes + postulante.getTecnologias().stream().filter( + tec -> tec.getTecnologia().getId() != 0 + ).forEach( + tec -> tec.setTecnologia(tecRepo.getById(tec.getTecnologia().getId())) + ); post.save(postulante); return "redirect:/"; } diff --git a/curriculumsearch/src/main/java/com/roshka/modelo/PostulanteTecnologia.java b/curriculumsearch/src/main/java/com/roshka/modelo/PostulanteTecnologia.java index f4e9263..4f4747b 100644 --- a/curriculumsearch/src/main/java/com/roshka/modelo/PostulanteTecnologia.java +++ b/curriculumsearch/src/main/java/com/roshka/modelo/PostulanteTecnologia.java @@ -9,13 +9,15 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; +import javax.persistence.UniqueConstraint; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotBlank; import com.fasterxml.jackson.annotation.JsonBackReference; @Entity -@Table(name="postulante_tecnologia") +@Table(name="postulante_tecnologia", +uniqueConstraints=@UniqueConstraint(columnNames={"postulante_id", "tecnologia_id"})) public class PostulanteTecnologia { @Id @@ -27,7 +29,7 @@ public class PostulanteTecnologia { @Min(value = 1) @Max(value = 5) private Long nivel; - @ManyToOne(cascade = CascadeType.PERSIST) + @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE},optional = false) @JoinColumn private Tecnologia tecnologia; diff --git a/curriculumsearch/src/main/resources/application.properties b/curriculumsearch/src/main/resources/application.properties index d6c6325..192b323 100644 --- a/curriculumsearch/src/main/resources/application.properties +++ b/curriculumsearch/src/main/resources/application.properties @@ -1,7 +1,10 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect - +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +logging.level.org.hibernate.SQL=DEBUG +logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE spring.sql.init.mode=always spring.sql.init.platform=postgres diff --git a/curriculumsearch/src/main/resources/static/main.js b/curriculumsearch/src/main/resources/static/main.js index 0cd5fdf..bacc868 100644 --- a/curriculumsearch/src/main/resources/static/main.js +++ b/curriculumsearch/src/main/resources/static/main.js @@ -56,10 +56,11 @@ function agregarFieldTecnologia(){ pairs[name] = value } tecnologias[cont_tecnologia]={} - tecnologias[cont_tecnologia]["tecnologia"]={nombre: pairs.nombre} + tecnologias[cont_tecnologia]["tecnologia"]=pairs["tecnologia-id"]=="-1"?{nombre: pairs["tecnologia-nombre"]}:{id: pairs["tecnologia-id"],nombre:document.querySelector('option[value="'+pairs["tecnologia-id"]+'"]').innerHTML} tecnologias[cont_tecnologia]["nivel"]=pairs.nivel //tecnologias[cont_tecnologia] = pairs; formtecn.reset(); + document.querySelector("#tecnologia-nombre").classList.add('d-none') //imprimir lista actualizada const div = document.querySelector("#tecnologias") const div1 = document.createElement('div'); @@ -149,4 +150,6 @@ form.addEventListener("submit",(evt)=>{ } }); evt.preventDefault(); -} ); \ No newline at end of file +} ); + +document.querySelector("#btn-new-tech").addEventListener('click',()=>{document.querySelector("#tecnologia-nombre").classList.remove('d-none')}) \ No newline at end of file diff --git a/curriculumsearch/src/main/webapp/jsp/postulante-form.jsp b/curriculumsearch/src/main/webapp/jsp/postulante-form.jsp index 5ee55ad..f0969d4 100644 --- a/curriculumsearch/src/main/webapp/jsp/postulante-form.jsp +++ b/curriculumsearch/src/main/webapp/jsp/postulante-form.jsp @@ -1,3 +1,6 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> @@ -158,8 +161,19 @@