forked from Realhedin/topjava02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootController.java
More file actions
97 lines (85 loc) · 3.42 KB
/
RootController.java
File metadata and controls
97 lines (85 loc) · 3.42 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ru.javawebinar.topjava.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import ru.javawebinar.topjava.LoggedUser;
import ru.javawebinar.topjava.service.UserService;
import ru.javawebinar.topjava.to.DateTimeFilter;
import ru.javawebinar.topjava.to.UserTo;
import ru.javawebinar.topjava.util.UserUtil;
import javax.validation.Valid;
/**
* User: gkislin
* Date: 22.08.2014
*/
@Controller
public class RootController {
@Autowired
private UserService userService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String root() {
return "redirect:meals";
}
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(ModelMap model,
@RequestParam(value = "error", required = false) boolean error,
@RequestParam(value = "message", required = false) String message) {
model.put("error", error);
model.put("message", message);
return "login";
}
@RequestMapping(value = "/meals", method = RequestMethod.GET)
public String mealList(Model model) {
model.addAttribute("filter", new DateTimeFilter());
return "mealList";
}
// @Secured("ROLE_ADMIN")
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/users", method = RequestMethod.GET)
public String userList() {
return "userList";
}
@RequestMapping(value = "/profile", method = RequestMethod.GET)
public String profile(ModelMap model) {
return "profile";
}
@RequestMapping(value = "/profile", method = RequestMethod.POST)
public String updateProfile(@Valid UserTo userTo, BindingResult result, SessionStatus status, ModelMap model) {
if (result.hasErrors()) {
return "profile";
} else {
status.setComplete();
LoggedUser.get().updateUserTo(userTo);
userService.update(userTo);
return "redirect:meals";
}
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(ModelMap model) {
model.addAttribute("userTo", new UserTo());
model.addAttribute("register", true);
return "profile";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String saveRegister(@Valid UserTo userTo, BindingResult result, SessionStatus status, ModelMap model) {
if (!result.hasErrors()) {
try {
userService.save(UserUtil.createFromTo(userTo));
status.setComplete();
return "redirect:login?message=app.registered";
} catch (DataIntegrityViolationException ex) {
result.rejectValue("email", "error.user", "User with this email already present in application.");
}
}
model.addAttribute("register", true);
return "profile";
}
}