content: package aspera.registration.web;
  
  import aspera.registration.model.User;
  import aspera.registration.service.UserService;
  import aspera.registration.web.dto.UserRegistrationDto;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.validation.BindingResult;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import javax.validation.Valid;
  
  /**
   * @Created 02 / 06 / 2020 - 2:39 PM
   * @project SpringRegistration
   * @Author Hamdamboy
   */
  
  @Controller
  @RequestMapping("/registration")
  public class UserRegistrationController {
  
      @Autowired
      private UserService userService;
  
      @ModelAttribute("user")
      public UserRegistrationDto userRegistrationDto(){
          return new UserRegistrationDto();
      }
  
      @GetMapping
      public String showRegistrationForm(Model model){
          return "registration";
      }
  
      @PostMapping
      public String registerUserAccount(@ModelAttribute("user") @Valid UserRegistrationDto userDto, BindingResult result) {
  
          User existing = userService.findByEmail(userDto.getEmail());
  
          if (existing != null){
              result.rejectValue("email", null, "There is already an account registered with that email");
          }
          if (result.hasErrors()){
              return "registration";
          }
  
          userService.save(userDto);
          return "redirect:/registration?success";
      }
    }
  