streaming.controller.GenreController.java Source code

Java tutorial

Introduction

Here is the source code for streaming.controller.GenreController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package streaming.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import streaming.entity.Film;
import streaming.entity.Genre;
import streaming.service.FilmCrudService;
import streaming.service.GenreCrudService;

/**
 *
 * @author admin
 */
@Controller
@RequestMapping(value = "/genre")
public class GenreController {

    @Autowired
    private GenreCrudService genreCrudService;

    @Autowired
    private FilmCrudService filmCrudService;

    @RequestMapping(value = "lister", method = RequestMethod.GET)
    public String lister(Model model) {

        Iterable<Genre> listGenre = genreCrudService.findAll();

        model.addAttribute("maListeGenre", listGenre);

        return "genre/listerGenre";
    }

    @RequestMapping(value = "ajouter", method = RequestMethod.GET)
    public String ajouter(Model model) {
        model.addAttribute("monGenre", new Genre());
        return "genre/ajouterGenre";
    }

    @RequestMapping(value = "ajouter", method = RequestMethod.POST)
    public String ajouterPost(@ModelAttribute(value = "monGenre") Genre g) {

        genreCrudService.save(g);

        return "redirect:/genre/lister";
    }

    @RequestMapping(value = "modifier/{idGenre}", method = RequestMethod.GET)
    public String modifier(Model model, @PathVariable(value = "idGenre") long monIdGenre) {
        Genre g = genreCrudService.findOne(monIdGenre);

        model.addAttribute("monGenre", g);
        return "genre/modifierGenre";
    }

    @RequestMapping(value = "modifier", method = RequestMethod.POST)
    public String modifierPost(@ModelAttribute(value = "monGenre") Genre g) {

        genreCrudService.save(g);

        return "redirect:/genre/lister";
    }

    @RequestMapping(value = "supprimer/{idGenre}", method = RequestMethod.GET)
    public String supprimer(@PathVariable(value = "idGenre") long monIdGenre) {
        Genre g = genreCrudService.findOne(monIdGenre);
        List<Film> listFilm = filmCrudService.findByGenreDuFilmId(monIdGenre);

        for (Film f : listFilm) {
            f.setGenreDuFilm(null);
            filmCrudService.save(f);
        }

        genreCrudService.delete(g);
        return "redirect:/genre/lister";
    }
}