BeneficioController.java 2.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package com.roshka.controller;

import com.roshka.modelo.Beneficio;
import com.roshka.repositorio.BeneficioRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Controller
@RequestMapping("/beneficios")
public class BeneficioController {

    BeneficioRepository beneficioRepository;


    @Autowired
    public BeneficioController(BeneficioRepository beneficioRepository){
        this.beneficioRepository = beneficioRepository;
    }

    @GetMapping(path = {"/agregar","/modificar/{id}"})
    public String addBeneficioView(Model model, @PathVariable(required = false) Long id) {


        if(id == null) model.addAttribute("beneficio", new Beneficio());
        else model.addAttribute("beneficio", beneficioRepository.getById(id));
        return "beneficio-form";
    }

    @RequestMapping()
    public String menuBeneficios(Model model,@RequestParam(required = false) String nombre,@RequestParam(defaultValue = "0")Integer nroPagina) {
        final Integer CANTIDAD_POR_PAGINA = 10;
        Pageable page = PageRequest.of(nroPagina,CANTIDAD_POR_PAGINA,Sort.by("id"));

        if(nombre == null || nombre.trim().isEmpty()) {
            Page<Beneficio> beneficioPag=beneficioRepository.findAllBeneficio(page);
            model.addAttribute("beneficios", beneficioPag.getContent());
            model.addAttribute("pages", beneficioPag.getTotalPages());
        }
        else {
            Page<Beneficio> beneficioPag=beneficioRepository.findByTituloContainingIgnoreCase(nombre.trim(),page);
            model.addAttribute("pages", beneficioPag.getTotalPages());
            model.addAttribute("beneficios", beneficioPag.getContent());
        }
        return "beneficios";
    }

    @PostMapping(path = {"/agregar","/modificar/{id}"})
    public String addBeneficio(@Valid @ModelAttribute Beneficio beneficio, BindingResult result, @PathVariable(required = false) Long id, Model model) {
        if(result.hasErrors() || (id==null && beneficioRepository.existsByTituloIgnoreCase(beneficio.getTitulo()))){
            model.addAttribute("mismoNombre", true);
            return "beneficio-form";
        }
        if(id != null ) beneficio.setId(id);
        beneficioRepository.save(beneficio);
        return "redirect:/beneficios";
    }
}