1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.web.controllers;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.List;
21
22 import javax.servlet.http.HttpSession;
23
24 import org.mybatis.jpetstore.domain.Account;
25 import org.mybatis.jpetstore.domain.Product;
26 import org.mybatis.jpetstore.service.AccountService;
27 import org.mybatis.jpetstore.service.CatalogService;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.ui.Model;
31 import org.springframework.web.bind.annotation.GetMapping;
32 import org.springframework.web.bind.annotation.ModelAttribute;
33 import org.springframework.web.bind.annotation.PostMapping;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestParam;
36
37
38
39
40 @Controller
41 @RequestMapping("/account")
42 public class AccountController {
43
44
45 private static final String SIGNON_VIEW = "account/SignonForm";
46
47 private static final String NEW_ACCOUNT_VIEW = "account/NewAccountForm";
48
49 private static final String EDIT_ACCOUNT_VIEW = "account/EditAccountForm";
50
51
52 private static final List<String> LANGUAGE_LIST = Collections.unmodifiableList(Arrays.asList("english", "japanese"));
53
54 private static final List<String> CATEGORY_LIST = Collections
55 .unmodifiableList(Arrays.asList("FISH", "DOGS", "REPTILES", "CATS", "BIRDS"));
56
57
58 @Autowired
59 private AccountService accountService;
60
61 @Autowired
62 private CatalogService catalogService;
63
64
65
66
67
68
69 @GetMapping({ "", "/" })
70 public String signonForm() {
71 return SIGNON_VIEW;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @PostMapping("/signon")
89 public String signon(@RequestParam("username") String username, @RequestParam("password") String password,
90 HttpSession session, Model model) {
91 Account account = accountService.getAccount(username, password);
92 if (account == null) {
93 model.addAttribute("message", "Invalid username or password. Signon failed.");
94 return SIGNON_VIEW;
95 }
96 account.setPassword(null);
97 List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
98 session.setAttribute("accountBean", new AccountSession(account, myList, true));
99 return "redirect:/catalog";
100 }
101
102
103
104
105
106
107
108
109
110 @GetMapping("/signoff")
111 public String signoff(HttpSession session) {
112 session.invalidate();
113 return "redirect:/catalog";
114 }
115
116
117
118
119
120
121
122
123
124 @GetMapping("/new")
125 public String newAccountForm(Model model) {
126 model.addAttribute("languages", LANGUAGE_LIST);
127 model.addAttribute("categories", CATEGORY_LIST);
128 model.addAttribute("account", new Account());
129 return NEW_ACCOUNT_VIEW;
130 }
131
132
133
134
135
136
137
138
139
140 @PostMapping("/new")
141 public String newAccount(@ModelAttribute Account account) {
142 accountService.insertAccount(account);
143 return "redirect:/catalog";
144 }
145
146
147
148
149
150
151
152
153
154
155
156 @GetMapping("/edit")
157 public String editAccountForm(HttpSession session, Model model) {
158 AccountSession accountSession = (AccountSession) session.getAttribute("accountBean");
159 if (accountSession != null) {
160 model.addAttribute("account", accountSession.getAccount());
161 }
162 model.addAttribute("languages", LANGUAGE_LIST);
163 model.addAttribute("categories", CATEGORY_LIST);
164 return EDIT_ACCOUNT_VIEW;
165 }
166
167
168
169
170
171
172
173
174
175
176
177 @PostMapping("/edit")
178 public String editAccount(@ModelAttribute Account account, HttpSession session) {
179 accountService.updateAccount(account);
180 Account updatedAccount = accountService.getAccount(account.getUsername());
181 List<Product> myList = catalogService.getProductListByCategory(updatedAccount.getFavouriteCategoryId());
182 session.setAttribute("accountBean", new AccountSession(updatedAccount, myList, true));
183 return "redirect:/account/edit";
184 }
185
186
187
188
189 public static class AccountSession implements java.io.Serializable {
190
191 private static final long serialVersionUID = 1L;
192
193 private final Account account;
194
195 private final List<Product> myList;
196
197 private final boolean authenticated;
198
199
200
201
202
203
204
205
206
207
208
209 public AccountSession(Account account, List<Product> myList, boolean authenticated) {
210 this.account = account;
211 this.myList = myList;
212 this.authenticated = authenticated;
213 }
214
215
216
217
218
219
220 public Account getAccount() {
221 return account;
222 }
223
224
225
226
227
228
229 public List<Product> getMyList() {
230 return myList;
231 }
232
233
234
235
236
237
238 public boolean isAuthenticated() {
239 return authenticated && account != null && account.getUsername() != null;
240 }
241 }
242 }