Commit 52078ec2 by Joel Florentin

listar tecnologias existentes

parent 2c98ce83
...@@ -57,6 +57,12 @@ ...@@ -57,6 +57,12 @@
<artifactId>hibernate-validator</artifactId> <artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version> <version>6.0.13.Final</version>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
...@@ -6,11 +6,13 @@ import javax.validation.ConstraintViolationException; ...@@ -6,11 +6,13 @@ import javax.validation.ConstraintViolationException;
import com.roshka.modelo.Postulante; import com.roshka.modelo.Postulante;
import com.roshka.repositorio.PostulanteRepository; import com.roshka.repositorio.PostulanteRepository;
import com.roshka.repositorio.TecnologiaRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -20,19 +22,28 @@ public class PostulanteController { ...@@ -20,19 +22,28 @@ public class PostulanteController {
@Autowired @Autowired
PostulanteRepository post; PostulanteRepository post;
@Autowired
TecnologiaRepository tecRepo;
@RequestMapping("/") @RequestMapping("/")
public String index() { public String index() {
return "index"; return "index";
} }
@RequestMapping("/postulante") @RequestMapping("/postulante")
public String getFormPostulante(){ public String getFormPostulante(Model model){
model.addAttribute("tecnologias", tecRepo.findAll());
return "postulante-form"; return "postulante-form";
} }
@PostMapping(value = "/postulante",consumes = "application/json") @PostMapping(value = "/postulante",consumes = "application/json")
public String guardarPostulante(@RequestBody Postulante postulante){ 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); post.save(postulante);
return "redirect:/"; return "redirect:/";
} }
......
...@@ -9,13 +9,15 @@ import javax.persistence.Id; ...@@ -9,13 +9,15 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
@Entity @Entity
@Table(name="postulante_tecnologia") @Table(name="postulante_tecnologia",
uniqueConstraints=@UniqueConstraint(columnNames={"postulante_id", "tecnologia_id"}))
public class PostulanteTecnologia { public class PostulanteTecnologia {
@Id @Id
...@@ -27,7 +29,7 @@ public class PostulanteTecnologia { ...@@ -27,7 +29,7 @@ public class PostulanteTecnologia {
@Min(value = 1) @Min(value = 1)
@Max(value = 5) @Max(value = 5)
private Long nivel; private Long nivel;
@ManyToOne(cascade = CascadeType.PERSIST) @ManyToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE},optional = false)
@JoinColumn @JoinColumn
private Tecnologia tecnologia; private Tecnologia tecnologia;
......
spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 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.mode=always
spring.sql.init.platform=postgres spring.sql.init.platform=postgres
......
...@@ -56,10 +56,11 @@ function agregarFieldTecnologia(){ ...@@ -56,10 +56,11 @@ function agregarFieldTecnologia(){
pairs[name] = value pairs[name] = value
} }
tecnologias[cont_tecnologia]={} 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]["nivel"]=pairs.nivel
//tecnologias[cont_tecnologia] = pairs; //tecnologias[cont_tecnologia] = pairs;
formtecn.reset(); formtecn.reset();
document.querySelector("#tecnologia-nombre").classList.add('d-none')
//imprimir lista actualizada //imprimir lista actualizada
const div = document.querySelector("#tecnologias") const div = document.querySelector("#tecnologias")
const div1 = document.createElement('div'); const div1 = document.createElement('div');
...@@ -149,4 +150,6 @@ form.addEventListener("submit",(evt)=>{ ...@@ -149,4 +150,6 @@ form.addEventListener("submit",(evt)=>{
} }
}); });
evt.preventDefault(); 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
<%@ 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" %>
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
...@@ -158,8 +161,19 @@ ...@@ -158,8 +161,19 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<form name="tecnologia-form"> <form name="tecnologia-form">
<label for="nombre" class="form-label">Nombre</label> <label for="tecnologia-nombre" class="form-label">Tecnologia</label>
<input type="text" class="form-control" name="nombre" id="nombre" > <div class="input-group mb-3">
<select class="form-select" name="tecnologia-id" aria-label="Default select example">
<option value="-1" selected>Open this select menu</option>
<c:forEach items="${tecnologias}" var="tecnologia">
<option value="${tecnologia.id}">${tecnologia.nombre}</option>
</c:forEach>
</select>
<button class="btn btn-outline-secondary" type="button" id="btn-new-tech">Agregar nuevo</button>
</div>
<input type="text" class="form-control d-none" name="tecnologia-nombre" id="tecnologia-nombre" >
<label for="nivel" class="form-label">Nivel</label> <label for="nivel" class="form-label">Nivel</label>
<input type="number" class="form-control" name="nivel" id="nivel" > <input type="number" class="form-control" name="nivel" id="nivel" >
</form> </form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment