99 lines
4.2 KiB
Java
99 lines
4.2 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<StringResponse> registerUser(@Valid @RequestBody User user){
|
|
try{
|
|
UserImplementation userImplementation = new UserImplementation();
|
|
Map<String,String> userHashResponse = userImplementation.registerUser(user);
|
|
|
|
if(Boolean.parseBoolean(userHashResponse.get("response"))){
|
|
StringResponse responseS = new StringResponse("Ok",userHashResponse.get("userHash"));
|
|
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 = "/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("Forbidden");
|
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(responseS);
|
|
}
|
|
case "" :{
|
|
StringResponse responseS = new StringResponse("Error");
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseS);
|
|
}
|
|
case "Ok":{
|
|
StringResponse responseS = new StringResponse("Ok",response.get("userHash"));
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
@PostMapping(value = "/get", produces = "application/json")
|
|
@ResponseBody
|
|
public ResponseEntity<User> getUser(@RequestBody User user){
|
|
try{
|
|
UserImplementation userImplementation = new UserImplementation();
|
|
User response = userImplementation.getUser(user.getEmail(), user.getPassword());
|
|
if(response != null){
|
|
User userResponse = new User();
|
|
userResponse.setUser_hash(response.getUser_hash());
|
|
userResponse.setUserId(response.getUserId());
|
|
return ResponseEntity.status(HttpStatus.OK).body(userResponse);
|
|
}else{
|
|
return new ResponseEntity("Error", HttpStatus.NOT_FOUND);
|
|
}
|
|
}catch (Exception e){
|
|
return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
|
|
@PostMapping(value = "/update")
|
|
@ResponseStatus(HttpStatus.OK)
|
|
public ResponseEntity updateUser(@RequestBody User user){
|
|
return new ResponseEntity(null, HttpStatus.OK);
|
|
}
|
|
}
|