TecnologiaController.java 2.85 KB
Newer Older
1 2
package com.roshka.controller;

3 4
import java.util.List;

5 6
import javax.validation.Valid;

7 8 9 10 11 12
import com.roshka.modelo.Tecnologia;
import com.roshka.repositorio.TecnologiaRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
13
import org.springframework.validation.BindingResult;
14 15
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
16
import org.springframework.web.bind.annotation.PathVariable;
17 18
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestParam;
20 21 22 23
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
24 25 26 27 28 29 30 31 32 33 34 35 36

@Controller
public class TecnologiaController {
 
    TecnologiaRepository tecRepo;


@Autowired 
public TecnologiaController(TecnologiaRepository tecRepo){
   this.tecRepo = tecRepo;

}

37 38 39 40 41 42
@GetMapping(path = {"/tecnologia","/tecnologia/{id}"})
public String addtecnologiaView(Model model,@PathVariable(required = false) Long id) {
    
    
    if(id == null) model.addAttribute("tecnologia", new Tecnologia());
    else model.addAttribute("tecnologia", tecRepo.getById(id));
43 44
    return "tecnologia-form";
}
45 46

@RequestMapping("/tecnologias")
47
    public String menuTecnologias(Model model,@RequestParam(required = false) String nombre,@RequestParam(defaultValue = "0")Integer nroPagina) {
48
        final Integer CANTIDAD_POR_PAGINA = 10;
49
        Pageable page = PageRequest.of(nroPagina,CANTIDAD_POR_PAGINA,Sort.by("id"));
50 51 52 53 54 55 56 57 58 59 60
        
        if(nombre == null || nombre.trim().isEmpty()) {
            Page<Tecnologia> tecnologiaPag=tecRepo.findAllTecnologia(page);
            model.addAttribute("tecnologias", tecnologiaPag.getContent());
            model.addAttribute("pages", tecnologiaPag.getTotalPages());
        }
        else {
            Page<Tecnologia> tecnologiaPag=tecRepo.findByNombreContainingIgnoreCase(nombre,page);    
            model.addAttribute("pages", tecnologiaPag.getTotalPages());
            model.addAttribute("tecnologias", tecnologiaPag.getContent());
        }
61 62 63 64
        return "tecnologias";
    }

@PostMapping(path = {"/tecnologia","/tecnologia/{id}"})
65 66 67 68 69
    public String addtecnologia(@Valid @ModelAttribute Tecnologia tecnologia, BindingResult result, @PathVariable(required = false) Long id, Model model) {
        if(result.hasErrors() || (id==null && tecRepo.existsByNombreIgnoreCase(tecnologia.getNombre()))){
            model.addAttribute("mismoNombre", true);
            return "tecnologia-form";
        } 
70 71
        if(id != null ) tecnologia.setId(id);
        tecRepo.save(tecnologia);
72
        return "redirect:/tecnologias";
73 74 75 76 77 78
    } 




}