PostulanteController.java 3.21 KB
Newer Older
1 2 3
package com.roshka.controller;


4 5
import java.util.List;

6
import javax.validation.ConstraintViolationException;
7

8 9
import com.roshka.modelo.Disponibilidad;
import com.roshka.modelo.Modalidad;
10
import com.roshka.modelo.Postulante;
11
import com.roshka.repositorio.ExperienciaRepository;
12
import com.roshka.repositorio.PostulanteRepository;
13
import com.roshka.repositorio.TecnologiaRepository;
14 15

import org.springframework.beans.factory.annotation.Autowired;
16 17
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
18
import org.springframework.stereotype.Controller;
19
import org.springframework.ui.Model;
20 21
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
22 23 24 25
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLOutput;
import java.util.List;
26
import java.util.Locale;
27 28


29 30


31 32 33 34
@Controller
public class PostulanteController {
    @Autowired
    PostulanteRepository post;  
35

36 37 38
    @Autowired
    TecnologiaRepository tecRepo;

39 40
    @Autowired
    ExperienciaRepository expRepo;
41
    @RequestMapping("/")
42
    public String index() {
43 44 45 46 47 48
      List<Postulante> j=  post.personasConExperienciaMayor(30);
      for (Postulante postulante : j) {
          System.out.println(postulante.getNombre());

      }  
      return "index";
49 50
    }

Joel Florentin committed
51
    @RequestMapping("/postulantes")
Joel Florentin committed
52 53
    public String postulantes(Model model,
                            @RequestParam(required = false,name = "tec")Long tecnologidaId) {
Joel Florentin committed
54
        model.addAttribute("tecnologias", tecRepo.findAll());
Joel Florentin committed
55 56
        if(tecnologidaId==null) model.addAttribute("postulantes", post.findAll());
        else model.addAttribute("postulantes", post.buscarPostulantesPorTecnologia(tecnologidaId));
Joel Florentin committed
57 58 59
        return "postulantes";
    }

60
    @RequestMapping("/postulante")
61 62
    public String getFormPostulante(Model model){
        model.addAttribute("tecnologias", tecRepo.findAll());
63 64
        model.addAttribute("modalidades", Modalidad.values());
        model.addAttribute("disponibilidades", Disponibilidad.values());
65 66
        return "postulante-form";
    }
67

68 69
    @PostMapping(value = "/postulante",consumes = "application/json")
    public String guardarPostulante(@RequestBody Postulante postulante){
70 71 72 73 74 75
        //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()))
                    );
76
        post.save(postulante);
77
        return "redirect:/";
78 79
    }

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseEntity<String> handleValidationExceptions(
            MethodArgumentNotValidException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({ConstraintViolationException.class})
    public ResponseEntity<String> handleValidationExceptions2(
            ConstraintViolationException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
    }

96

97
}