java-api/src/main/java/restService/UserResource.java
2019-04-11 21:44:53 +02:00

125 lines
5.8 KiB
Java

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<String,String> 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<StringResponse> 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<StringResponse> login(@Valid @RequestBody User user){
try{
UserImplementation userImplementation = new UserImplementation();
Map<String,String> 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<StringResponse> 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);
}
}