Created
March 5, 2017 20:59
-
-
Save DenisZm/a32550e1cf0ec167a2113b4e0cb4d76c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.royalrangers.controller; | |
import com.google.gson.*; | |
import com.royalrangers.security.repository.UserRepository; | |
import lombok.extern.slf4j.Slf4j; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.RestController; | |
@Slf4j | |
@RestController | |
public class EmailVerificationController { | |
@Autowired | |
private UserRepository userRepository; | |
@PostMapping("/open/checkEmail") | |
public ResponseEntity<?> checkEmail(@RequestBody String email) { | |
Gson gson = new GsonBuilder().setPrettyPrinting().create(); | |
JsonObject responseJson = new JsonObject(); | |
HttpStatus status = null; | |
try { | |
JsonElement jsonElement = new JsonParser().parse(email); | |
JsonObject jsonObject = jsonElement.getAsJsonObject(); | |
String mail = jsonObject.get("email").getAsString(); | |
log.info("checking email: " + mail); | |
int count = userRepository.countByEmail(mail); | |
if (count > 0) { | |
responseJson.addProperty("exist", true); | |
responseJson.addProperty("message", "user with this email already exist"); | |
} else { | |
responseJson.addProperty("exist", false); | |
responseJson.addProperty("message", "user with this email not exist"); | |
} | |
status = HttpStatus.OK; | |
} catch (JsonSyntaxException | NullPointerException e) { | |
responseJson.addProperty("message", "Illegal request"); | |
status = HttpStatus.BAD_REQUEST; | |
} catch (Exception e) { | |
responseJson.addProperty("message", e.getMessage()); | |
status = HttpStatus.INTERNAL_SERVER_ERROR; | |
} | |
return new ResponseEntity<String>(gson.toJson(responseJson), null , status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment