Commit 758f8e18 by Joel Florentin

form para crear listar convocatorias y cargos

parent a2013f11
package com.roshka.controller;
import com.roshka.modelo.Cargo;
import com.roshka.modelo.ConvocatoriaCargo;
import com.roshka.repositorio.CargoRepository;
import com.roshka.repositorio.ConvocatoriaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class CargoController {
CargoRepository cargoRepo;
ConvocatoriaRepository convoRepo;
@Autowired
public CargoController(CargoRepository cargoRepo, ConvocatoriaRepository convoRepo) {
this.cargoRepo = cargoRepo;
this.convoRepo = convoRepo;
}
@RequestMapping("/cargos")
public String menuCargos(Model model,@RequestParam(required = false) Long cargoId) {
model.addAttribute("cargos", cargoRepo.findAll());
model.addAttribute("convocatorias",cargoId==null? convoRepo.findAll() : convoRepo.findByCargoId(cargoId));
return "cargo";
}
@RequestMapping("/cargo")
public String formCargo(Model model) {
model.addAttribute("cargo", new Cargo());
return "cargo-form";
}
@PostMapping("/cargo")
public String guardarCargo(@ModelAttribute Cargo cargo, BindingResult result) {
if(result.hasErrors());
cargoRepo.save(cargo);
System.out.println(cargo.getNombre());
return "redirect:/cargos";
}
@RequestMapping("/convocatoria")
public String formConvocatoria(Model model) {
model.addAttribute("cargos", cargoRepo.findAll());
model.addAttribute("convocatoria", new ConvocatoriaCargo());
return "convocatoria-form";
}
@PostMapping("/convocatoria")
public String guardarConvocatoria(@ModelAttribute ConvocatoriaCargo convocatoria, BindingResult result) {
if(result.hasErrors());
convoRepo.save(convocatoria);
System.out.println(convocatoria.getFechaInicio());
return "redirect:/cargos";
}
}
......@@ -12,6 +12,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.roshka.utils.Helper;
......@@ -24,10 +25,13 @@ public class ConvocatoriaCargo {
private long id;
@ManyToOne()
@JoinColumn
@JoinColumn(name = "cargo_id",insertable = false, updatable = false)
@JsonBackReference
private Cargo cargo;
@Column(name = "cargo_id")
private Long cargoId;
@Column(name = "fecha_inicio")
private Date fechaInicio;
......@@ -37,6 +41,14 @@ public class ConvocatoriaCargo {
@Column(name = "cupos")
private int cupos;
//para serializar en el form como string
@Transient
private String fechaFinS;
//para serializar en el form como string
@Transient
private String fechaInicioS;
@ManyToMany(mappedBy = "postulaciones")
private List<Postulante> postulantes;
......@@ -82,5 +94,25 @@ public class ConvocatoriaCargo {
public void setPostulantes(List<Postulante> postulantes) {
this.postulantes = postulantes;
}
public Long getCargoId() {
return cargoId;
}
public void setCargoId(Long cargoId) {
this.cargoId = cargoId;
}
public String getFechaFinS() {
return fechaFinS;
}
public String getFechaInicioS() {
return fechaInicioS;
}
public void setFechaFinS(String fechaFinS) {
this.fechaFinS = fechaFinS;
setFechaFin(fechaFinS);
}
public void setFechaInicioS(String fechaInicioS) {
this.fechaInicioS = fechaInicioS;
setFechaInicio(fechaInicioS);
}
}
package com.roshka.repositorio;
import com.roshka.modelo.Cargo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CargoRepository extends JpaRepository<Cargo,Long>{
}
package com.roshka.repositorio;
import java.util.List;
import com.roshka.modelo.ConvocatoriaCargo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ConvocatoriaRepository extends JpaRepository<ConvocatoriaCargo,Long> {
public List<ConvocatoriaCargo> findByCargoId(Long cargoId);
}
<%@ 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>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cargo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<form:form action="/cargo" method="post" modelAttribute="cargo" class="row row-cols-lg-auto g-3 align-items-center">
<div class="col-12">
<form:label class="form-label visually-hidden" path="nombre">Nombre del cargo </form:label>
<form:input type="text" path="nombre" class="form-control" placeholder="Nombre del cargo"/>
</div>
<div class="col-12">
<input type="submit" value="Guardar" class="btn btn-primary"/>
</div>
</form:form>
</div>
</body>
</html>
\ No newline at end of file
......@@ -8,14 +8,54 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cargo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
<h1>Hola</h1>
<c:forEach var="i" items="${postu}">
<body class="container">
<div>
Cargos:
<select class="form-select" name="cargos" id="cargos">
<option value="-1">Todos los cargos</option>
<c:forEach items="${cargos}" var="cargo">
<option value="${cargo.id}" ${param.cargoId == cargo.id ? "selected" : ""} >${cargo.nombre}</option>
</c:forEach>
</select>
<a href="/cargo">Agregar Nuevo Cargo</a>
</div>
<div>
<a href="/convocatoria">Agregar Nueva Convocatoria</a>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Fecha Desde</th>
<th scope="col">Fecha Hasta</th>
<th scope="col">Vacantes</th>
</tr>
</thead>
<tbody>
<c:forEach items="${convocatorias}" var="convocatoria" varStatus="sta">
<tr>
<td>- ${i.getPostulante().getNombre()}</td>
<th scope="row">${sta.index+1}</th>
<td>${convocatoria.getFechaInicio().toString().split(" ")[0]}</td>
<td>${convocatoria.getFechaFin().toString().split(" ")[0]}</td>
<td>${convocatoria.getCupos()}</td>
<td>Ver Postulantes</td>
</tr>
<br/>
</c:forEach>
</tbody>
</table>
</div>
<script>
document.querySelector("#cargos").addEventListener("change",evt=>{
const cargoId = evt.target.value;
if(cargoId == -1) location.assign("/cargos");
else location.assign("/cargos?cargoId="+cargoId)
})
</script>
</body>
</html>
\ 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>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cargo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<form:form action="/convocatoria" method="post" modelAttribute="convocatoria">
<div class="mb-3">
<form:label path="fechaInicioS" class="form-label">Fecha inicial</form:label>
<form:input type="date" class="form-control" path="fechaInicioS"/>
</div>
<div class="mb-3">
<form:label path="fechaFinS" class="form-label">Fecha Fin</form:label>
<form:input type="date" class="form-control" path="fechaFinS"/>
</div>
<div class="mb-3">
<form:label path="cupos" class="form-label">Cupos:</form:label>
<form:input type="number" class="form-control" path="cupos"/>
</div>
<div class="mb-3 form-check">
<form:label path="cargoId" class="form-label">Cargo</form:label>
<form:select class="form-select" path="cargoId">
<c:forEach items="${cargos}" var="cargo">
<form:option value="${cargo.id}">${cargo.nombre} </form:option>
</c:forEach>
</form:select>
</div>
<button type="submit" class="btn btn-primary">Guardar</button>
</form:form>
</div>
</body>
</html>
\ No newline at end of file
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