CargoController.java 2.74 KB
Newer Older
1 2
package com.roshka.controller;

3 4
import java.util.Date;

5 6 7 8 9
import com.roshka.modelo.Cargo;
import com.roshka.modelo.ConvocatoriaCargo;
import com.roshka.repositorio.CargoRepository;
import com.roshka.repositorio.ConvocatoriaRepository;

10 11 12
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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;
    }

34
    @RequestMapping("/convocatorias")
35 36 37 38
    public String menuCargos(Model model,
                            @RequestParam(required = false) Long cargoId,
                            @RequestParam(required = false) Integer isOpen//1: true, 0: false
                            ) {
39 40

        model.addAttribute("cargos", cargoRepo.findAll());
41 42 43
        
        model.addAttribute("convocatorias", convoRepo.f1ndByCargoAndEstado(new TypedParameterValue(LongType.INSTANCE, cargoId), new Date(), new TypedParameterValue(IntegerType.INSTANCE, isOpen)));
        //model.addAttribute("convocatorias",cargoId==null? convoRepo.findAll() : convoRepo.findByCargoId(cargoId));
44
        return "convocatorias";
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    }

    @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";
    }
}