java-api/src/main/java/restService/UserResource.java
2019-04-16 18:24:33 +02:00

166 lines
7.6 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"));
String r = "{\"response\":\"ok\",\"user_hash\":\""+userHashResponse.get("user_hash")+"\"}";
return ResponseEntity.status(HttpStatus.OK).body(r);
}else {
String r = "{\"response\":\"User already exist\"}";
return ResponseEntity.status(HttpStatus.FOUND).body(r);
}
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
@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);
String r = "{\"response\":\"ok\",\"user_hash\":\""+user.getUser_hash()+"\"}";
return ResponseEntity.status(HttpStatus.OK).body(r);
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
@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<String,String> response = userImplementation.userLogger(user);
switch (response.get("response")){
case "Not Exist" : {
String r = "{\"response\":\"Not Found\"}";
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(r);
}
case "Not Allowed" :{
String r = "{\"response\":\"Wrong authentication\"}";
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(r);
}
case "" :{
String r = "{\"response\":\"Error\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
case "true":{
String r = "{\"response\":\"ok\",\"user_hash\":\""+response.get("user_hash")+"\"}";
return ResponseEntity.status(HttpStatus.OK).body(r);
}
default:{
String r = "{\"response\":\"Error\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
@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);
String r = "{\"response\":\"ok\"}";
return ResponseEntity.status(HttpStatus.OK).body(r);
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity updateUser(@RequestBody User user){
return new ResponseEntity(null, HttpStatus.SERVICE_UNAVAILABLE);
}
@RequestMapping(value = "/findByEmail", method = RequestMethod.GET, params = {"email"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity findUserByEmail(@RequestParam(value = "email") String email){
try {
UserImplementation userImplementation = new UserImplementation();
User r = userImplementation.getUserWithEmail(email);
return ResponseEntity.status(HttpStatus.OK).body(r);
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
@RequestMapping(value = "/findByPhone", method = RequestMethod.GET, params = {"phone"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity findUserByPhone(@RequestParam(value = "phone") String phone){
try {
UserImplementation userImplementation = new UserImplementation();
User r = userImplementation.getUserWithPhone(phone);
return ResponseEntity.status(HttpStatus.OK).body(r);
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
@RequestMapping(value = "/findByEmailAndPhone", method = RequestMethod.GET, params = {"email","phone"}, produces = "application/json")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity findUserByEmailAndPhone(@RequestParam(value = "email") String email, @RequestParam(value = "phone") String phone){
try {
UserImplementation userImplementation = new UserImplementation();
User r = userImplementation.getUserWithMailAndPhone(email,phone);
return ResponseEntity.status(HttpStatus.OK).body(r);
}catch (Exception e){
String r = "{\"response\":\""+e.getMessage()+"\"}";
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(r);
}
}
}