Merge branch 'features/apiFront' into develop
This commit is contained in:
commit
23e8ef5b1f
|
@ -5,9 +5,9 @@ import com.j256.ormlite.support.ConnectionSource;
|
||||||
|
|
||||||
public class DatabaseHelper {
|
public class DatabaseHelper {
|
||||||
private static final String DATABASE_NAME = "monnethic";
|
private static final String DATABASE_NAME = "monnethic";
|
||||||
private static final String DATABASE_USER = "";
|
private static final String DATABASE_USER = "postgres";
|
||||||
private static final String DATABASE_PWD = "";
|
private static final String DATABASE_PWD = "L-*q~Ytaha{;u+7yJ8";
|
||||||
private final static String DATABASE_URL = "jdbc:postgresql://host:port/"+DATABASE_NAME;
|
private final static String DATABASE_URL = "jdbc:postgresql://37.187.101.44:5432/"+DATABASE_NAME;
|
||||||
|
|
||||||
public ConnectionSource setupDatabaseConnection(){
|
public ConnectionSource setupDatabaseConnection(){
|
||||||
try{
|
try{
|
||||||
|
|
|
@ -5,6 +5,8 @@ import database.user.UserDao;
|
||||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class DatabaseUserImplementation {
|
public class DatabaseUserImplementation {
|
||||||
|
|
||||||
|
@ -46,21 +48,25 @@ public class DatabaseUserImplementation {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String userLogger(User user) throws Exception {
|
public Map<String,String> userLogger(User user) throws Exception {
|
||||||
UserDao userDao = new UserDao();
|
UserDao userDao = new UserDao();
|
||||||
String response;
|
Map<String,String> response = new HashMap<String, String>();
|
||||||
|
|
||||||
User user1 = userDao.getUser(user.getEmail());
|
User user1 = userDao.getUser(user.getEmail());
|
||||||
if(user1 != null){
|
if(user1 != null){
|
||||||
String hash = user1.getPassword();
|
String hash = user1.getPassword();
|
||||||
|
|
||||||
if(!BCrypt.checkpw(user.getPassword(), hash)){
|
if(!BCrypt.checkpw(user.getPassword(), hash)){
|
||||||
response = "Not Allowed";
|
response.put("response","Not Allowed");
|
||||||
|
//response = "Not Allowed";
|
||||||
}else{
|
}else{
|
||||||
response = "Ok";
|
response.put("response","Ok");
|
||||||
|
response.put("userHash",user1.getUser_hash());
|
||||||
|
//response = "Ok";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
response = "Not Exist";
|
response.put("response","Not Exist");
|
||||||
|
//response = "Not Exist";
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package restService;
|
package restService;
|
||||||
|
|
||||||
import database.user.User;
|
import database.user.User;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import restImplementation.DatabaseUserImplementation;
|
import restImplementation.DatabaseUserImplementation;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(value = "/api/rest/user")
|
@RequestMapping(value = "/api/rest/user")
|
||||||
|
@ -14,39 +17,58 @@ public class DatabaseUserResource {
|
||||||
|
|
||||||
@PostMapping(value = "/save")
|
@PostMapping(value = "/save")
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public ResponseEntity<String> saveUser(@Valid @RequestBody User user){
|
public ResponseEntity<StringResponse> saveUser(@Valid @RequestBody User user){
|
||||||
try{
|
try{
|
||||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||||
boolean result = databaseUserImplementation.saveUser(user);
|
boolean result = databaseUserImplementation.saveUser(user);
|
||||||
if(result){
|
if(result){
|
||||||
return ResponseEntity.status(HttpStatus.OK).body("Ok");
|
StringResponse responseS = new StringResponse("Ok");
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body(responseS);
|
||||||
}else {
|
}else {
|
||||||
return ResponseEntity.status(HttpStatus.FOUND).body("User already exist");
|
StringResponse responseS = new StringResponse("User already exist");
|
||||||
|
return ResponseEntity.status(HttpStatus.FOUND).body(responseS);
|
||||||
}
|
}
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
|
StringResponse responseS = new StringResponse(e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(responseS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/login")
|
@RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json")
|
||||||
@ResponseStatus(HttpStatus.OK)
|
@ResponseStatus(HttpStatus.OK)
|
||||||
public ResponseEntity<String> login(@Valid @RequestBody User user){
|
public ResponseEntity<StringResponse> login(@Valid @RequestBody User user){
|
||||||
try{
|
try{
|
||||||
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
DatabaseUserImplementation databaseUserImplementation = new DatabaseUserImplementation();
|
||||||
String response = databaseUserImplementation.userLogger(user);
|
Map<String,String> response = databaseUserImplementation.userLogger(user);
|
||||||
switch (response){
|
switch (response.get("response")){
|
||||||
case "Not Exist" : return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found");
|
case "Not Exist" : {
|
||||||
case "Not Allowed" : return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Wrong Password!");
|
StringResponse responseS = new StringResponse("User Not Found");
|
||||||
case "" : return ResponseEntity.status(HttpStatus.CONFLICT).body("Error");
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseS);
|
||||||
case "Ok": return ResponseEntity.status(HttpStatus.OK).body("Ok");
|
}
|
||||||
default: return ResponseEntity.status(HttpStatus.CONFLICT).body("Error");
|
case "Not Allowed" :{
|
||||||
|
StringResponse responseS = new StringResponse("Wrong Password!");
|
||||||
|
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(responseS);
|
||||||
|
}
|
||||||
|
case "" :{
|
||||||
|
StringResponse responseS = new StringResponse("Error");
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).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.CONFLICT).body(responseS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
|
StringResponse responseS = new StringResponse(e.getMessage());
|
||||||
|
return ResponseEntity.status(HttpStatus.CONFLICT).body(responseS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/get")
|
@PostMapping(value = "/get", produces = "application/json")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity<User> getUser(@RequestBody User user){
|
public ResponseEntity<User> getUser(@RequestBody User user){
|
||||||
try{
|
try{
|
||||||
|
|
31
src/main/java/restService/StringResponse.java
Normal file
31
src/main/java/restService/StringResponse.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package restService;
|
||||||
|
|
||||||
|
public class StringResponse {
|
||||||
|
private String response;
|
||||||
|
private String userHash;
|
||||||
|
|
||||||
|
public StringResponse(String response){
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringResponse(String response, String userHash){
|
||||||
|
this.response=response;
|
||||||
|
this.userHash=userHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getResponse() {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponse(String response) {
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserHash() {
|
||||||
|
return userHash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserHash(String userHash) {
|
||||||
|
this.userHash = userHash;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue