package restService; import database.user.User; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import restImplementation.UserImplementation; import javax.validation.Valid; import java.util.Map; @RestController @RequestMapping(value = "/api/rest/user") public class UserResource { @RequestMapping(value = "/register", method = RequestMethod.POST,produces = "application/json") @ResponseStatus(HttpStatus.CREATED) public ResponseEntity registerUser(@Valid @RequestBody User user){ try{ UserImplementation userImplementation = new UserImplementation(); Map userHashResponse = userImplementation.registerUser(user); if(Boolean.parseBoolean(userHashResponse.get("response"))){ //TEMPORARY AUTOMATIC APPROVE user.setUser_hash(userHashResponse.get("user_hash")); userImplementation.approveUser(user); // StringResponse responseS = new StringResponse("Ok",userHashResponse.get("user_hash")); return ResponseEntity.status(HttpStatus.OK).body(responseS); }else { StringResponse responseS = new StringResponse("User already exist"); return ResponseEntity.status(HttpStatus.FOUND).body(responseS); } }catch (Exception e){ StringResponse responseS = new StringResponse(e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); } } @RequestMapping(value = "/approve", method = RequestMethod.POST, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity approveUser(@Valid @RequestBody User user){ try{ UserImplementation userImplementation = new UserImplementation(); userImplementation.approveUser(user); StringResponse response = new StringResponse("Ok",user.getUser_hash()); return ResponseEntity.status(HttpStatus.OK).body(response); }catch (Exception e){ StringResponse response = new StringResponse("Error: "+e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); } } @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity login(@Valid @RequestBody User user){ try{ UserImplementation userImplementation = new UserImplementation(); Map response = userImplementation.userLogger(user); switch (response.get("response")){ case "Not Exist" : { StringResponse responseS = new StringResponse("Not Found"); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseS); } case "Not Allowed" :{ StringResponse responseS = new StringResponse("Wrong authentication"); return ResponseEntity.status(HttpStatus.FORBIDDEN).body(responseS); } case "" :{ StringResponse responseS = new StringResponse("Error"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); } case "true":{ StringResponse responseS = new StringResponse("Ok",response.get("user_hash")); return ResponseEntity.status(HttpStatus.OK).body(responseS); } default:{ StringResponse responseS = new StringResponse("Error"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); } } }catch (Exception e){ StringResponse responseS = new StringResponse(e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); } } @RequestMapping(value = "/get", method = RequestMethod.GET, params = {"user_email"}, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity getUser(@RequestParam(value = "user_email") String user_email){ try{ UserImplementation userImplementation = new UserImplementation(); Boolean response = userImplementation.getUser(user_email); if(response){ return ResponseEntity.status(HttpStatus.FOUND).body("{\"response\":"+response.toString()+"}"); }else { return ResponseEntity.status(HttpStatus.OK).body("{\"response\":"+response.toString()+"}"); } }catch (Exception e){ return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } } @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json") @ResponseStatus(HttpStatus.OK) public ResponseEntity removeUser(@Valid @RequestBody User user){ try{ UserImplementation userImplementation = new UserImplementation(); userImplementation.deleteUser(user); StringResponse responseS = new StringResponse("Ok"); return ResponseEntity.status(HttpStatus.OK).body(responseS); }catch (Exception e){ StringResponse responseS = new StringResponse(e.getMessage()); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS); } } @RequestMapping(value = "/update", method = RequestMethod.POST) @ResponseStatus(HttpStatus.OK) public ResponseEntity updateUser(@RequestBody User user){ return new ResponseEntity(null, HttpStatus.SERVICE_UNAVAILABLE); } }