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


4
import javax.validation.ConstraintViolationException;
5

6 7
import com.roshka.modelo.Disponibilidad;
import com.roshka.modelo.Modalidad;
8 9
import com.roshka.modelo.Postulante;
import com.roshka.repositorio.PostulanteRepository;
10
import com.roshka.repositorio.TecnologiaRepository;
11 12

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


@Controller
public class PostulanteController {
    @Autowired
    PostulanteRepository post;  
25

26 27 28
    @Autowired
    TecnologiaRepository tecRepo;

29
    @RequestMapping("/")
30 31
    public String index() {
        return "index";
32 33
    }

Joel Florentin committed
34 35 36 37 38 39 40
    @RequestMapping("/postulantes")
    public String postulantes(Model model) {
        model.addAttribute("tecnologias", tecRepo.findAll());
        model.addAttribute("postulantes", post.findAll());
        return "postulantes";
    }

41
    @RequestMapping("/postulante")
42 43
    public String getFormPostulante(Model model){
        model.addAttribute("tecnologias", tecRepo.findAll());
44 45
        model.addAttribute("modalidades", Modalidad.values());
        model.addAttribute("disponibilidades", Disponibilidad.values());
46 47
        return "postulante-form";
    }
48

49 50
    @PostMapping(value = "/postulante",consumes = "application/json")
    public String guardarPostulante(@RequestBody Postulante postulante){
51 52 53 54 55 56
        //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()))
                    );
57
        post.save(postulante);
58
        return "redirect:/";
59 60
    }

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    @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());
    }

77
}