1 package nl.gridshore.samples.raffle.web.springmvc;
2
3 import nl.gridshore.samples.raffle.business.RaffleService;
4 import nl.gridshore.samples.raffle.domain.Raffle;
5 import nl.gridshore.samples.raffle.web.springmvc.validator.RaffleValidator;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Controller;
8 import org.springframework.ui.ModelMap;
9 import org.springframework.validation.BindingResult;
10 import org.springframework.web.bind.annotation.*;
11 import org.springframework.web.bind.support.SessionStatus;
12
13
14
15
16
17
18
19
20 @Controller
21 @RequestMapping("/editraffle.view")
22 @SessionAttributes("raffle")
23 public class EditRaffleController {
24 private final RaffleService raffleService;
25 private final RaffleValidator raffleValidator;
26
27 @Autowired
28 public EditRaffleController(RaffleService raffleService, RaffleValidator raffleValidator) {
29 this.raffleService = raffleService;
30 this.raffleValidator = raffleValidator;
31 }
32
33 @RequestMapping(method = RequestMethod.GET)
34 public String setupForm(@RequestParam("raffleId")long raffleId, ModelMap model) {
35 Raffle raffle = this.raffleService.giveRaffleById(raffleId);
36 model.addAttribute(raffle);
37 return "raffleForm";
38 }
39
40 @RequestMapping(method = RequestMethod.POST)
41 public String processSubmit(@ModelAttribute("raffle")Raffle raffle, BindingResult bindingResult, SessionStatus status) {
42 new RaffleValidator().validate(raffle, bindingResult);
43 if (bindingResult.hasErrors()) {
44 return "raffleForm";
45 } else {
46 this.raffleService.storeRaffle(raffle);
47 status.setComplete();
48 return "redirect:index.html";
49 }
50 }
51 }