View Javadoc
1   /*
2    * Copyright 2018-2022 Medical Information Systems Research Group (https://medical.zcu.cz),
3    * Department of Computer Science and Engineering, University of West Bohemia.
4    * Address: Univerzitni 8, 306 14 Plzen, Czech Republic.
5    *
6    * Author Petr Vcelak (vcelak@kiv.zcu.cz).
7    *
8    * This file is part of MRECore project.
9    *
10   * MRECore is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU General Public License as published by
12   * the Free Software Foundation, either version 3 of the License.
13   *
14   * MRECore is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with MRECore. If not, see <http://www.gnu.org/licenses/>.
21   */
22  package cz.zcu.mre.controller;
23  
24  import cz.zcu.mre.data.account.Account;
25  import cz.zcu.mre.data.account.RegistrationForm;
26  import cz.zcu.mre.data.support.MessageHelper;
27  import cz.zcu.mre.service.account.AccountService;
28  import jakarta.validation.Valid;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.stereotype.Component;
31  import org.springframework.stereotype.Controller;
32  import org.springframework.ui.Model;
33  import org.springframework.validation.Errors;
34  import org.springframework.web.bind.annotation.*;
35  import org.springframework.web.servlet.mvc.support.RedirectAttributes;
36  
37  /**
38   * 
39   * @author Petr Vcelak (vcelak@kiv.zcu.cz)
40   */
41  @Component
42  @Controller
43  public class RegistrationController {
44  
45      public static final String VIEW_REGISTRATION = "account/registration";
46  
47      @Autowired
48      private AccountService accountService;
49  
50      @RequestMapping(value = "/account/registration")
51      public String registration(Model model) {
52          model.addAttribute(new RegistrationForm());
53          return VIEW_REGISTRATION;
54      }
55  
56      @RequestMapping(value = "/account/registration", method = RequestMethod.POST)
57      public String registration(@Valid @ModelAttribute RegistrationForm registrationForm, Errors errors, RedirectAttributes ra) {
58          if (errors.hasErrors()) {
59              return VIEW_REGISTRATION;
60          }
61          Account account = accountService.save(registrationForm.createAccount());
62          accountService.login(account);
63          MessageHelper.addSuccessAttribute(ra, "mre.account.registration.success");
64          return "redirect:/";
65      }
66  }