package com.questionanswer.controller;

import com.questionanswer.config.Routes;
import com.questionanswer.model.User;
import com.questionanswer.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@RestController
@RequestMapping(value = Routes.API_BASE_ROUTE + "/authentication")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/currentuser", method = { RequestMethod.GET })
    public Principal currentUser(Principal user) {
        return user;
    }

    @RequestMapping(value = "/register", method = { RequestMethod.POST })
    public HttpEntity<User> Register(@RequestBody User user) {
        User savedUser = this.userService.save(user);
        return new ResponseEntity<User>(savedUser, HttpStatus.CREATED);
    }
}
@@ -223,16 +223,80 @@
 owired;%0A
+import org.springframework.dao.DataIntegrityViolationException;%0A
 import o
@@ -1115,20 +1115,17 @@
 pEntity%3C
-User
+?
 %3E Regist
@@ -1145,32 +1145,247 @@
 dy User user) %7B%0A
+%0A        if (this.userService.loadUserByUsername(user.getEmail()) != null) %7B%0A            return new ResponseEntity%3C%3E(%22User with the same email already exists!%22, HttpStatus.BAD_REQUEST);%0A        %7D%0A%0A        try %7B%0A    
         User sav
@@ -1422,16 +1422,20 @@
 (user);%0A
+    
         
@@ -1498,15 +1498,349 @@
 EATED);%0A
+        %7D catch (DataIntegrityViolationException ex) %7B%0A            return prepareExceptionResponce(ex);%0A        %7D%0A    %7D%0A%0A    private HttpEntity%3C?%3E prepareExceptionResponce(Exception ex) %7B%0A        String errorMessage;%0A        errorMessage = ex + %22 %3C== error%22;%0A        return new ResponseEntity%3C%3E(errorMessage, HttpStatus.BAD_REQUEST);%0A
     %7D%0A%7D

Add some error handling during the register user action