app-mobile/app/src/main/java/monnethic/mobile/restApi/UserApiHandler.java
2019-06-10 14:56:37 +02:00

153 lines
5.8 KiB
Java

package monnethic.mobile.restApi;
import android.util.Log;
import org.json.JSONObject;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import monnethic.mobile.database.User;
public class UserApiHandler {
public JSONObject registerUser(User u){ //return JSON with session ID
HttpCallHandler httpCallHandler = new HttpCallHandler();
try{
String url = Config.USER_REGISTER;
Map<String, String> map = new HashMap<>();
map.put("name",u.getName());
map.put("firstname",u.getFirstname());
map.put("email",u.getEmail());
map.put("phone",u.getPhone());
map.put("association",u.getAssociation());
map.put("password",u.getPassword());
String responseCall = httpCallHandler.postHttp(new URL(url), map);
JSONObject jsonObject = new JSONObject(responseCall);
u.setUser_hash(jsonObject.getString("user_hash"));
JSONObject jsonReturn = new JSONObject();
jsonReturn.put("user_hash",u.getUser_hash());
String urlSession = Config.START_SESSION;
Map<String, String> mapSession = new HashMap<>();
mapSession.put("user_email",u.getEmail());
mapSession.put("user_hash",u.getUser_hash());
String responseCallSession = httpCallHandler.postHttp(new URL(urlSession), mapSession);
JSONObject jsonObjectSession = new JSONObject(responseCallSession);
jsonReturn.put("session_id",String.valueOf(jsonObjectSession.getInt("session_id")));
return jsonReturn;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public JSONObject loginUser(User u){ //return JSON with session ID
HttpCallHandler httpCallHandler = new HttpCallHandler();
try{
String url = Config.USER_LOGIN;
Map<String, String> map = new HashMap<>();
map.put("email",u.getEmail());
map.put("password",u.getPassword());
String responseCall = httpCallHandler.postHttp(new URL(url), map);
JSONObject jsonObject = new JSONObject(responseCall);
if(jsonObject.getInt("status")!=200){
return jsonObject;
} else {
u.setUser_hash(jsonObject.getString("user_hash"));
JSONObject jsonReturn = new JSONObject();
jsonReturn.put("status",200);
jsonReturn.put("user_hash",u.getUser_hash());
String urlSession = Config.START_SESSION;
Map<String, String> mapSession = new HashMap<>();
mapSession.put("user_email",u.getEmail());
mapSession.put("user_hash",u.getUser_hash());
String responseCallSession = httpCallHandler.postHttp(new URL(urlSession), mapSession);
JSONObject jsonObjectSession = new JSONObject(responseCallSession);
jsonReturn.put("session_id",String.valueOf(jsonObjectSession.getInt("session_id")));
return jsonReturn;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public Boolean checkUser(String email){
HttpCallHandler httpCallHandler = new HttpCallHandler();
Boolean response = null;
try{
String url = Config.USER_GET+"?user_email="+email;
String responseCall = httpCallHandler.getHttp(new URL(url));
response = new JSONObject(responseCall).getBoolean("response");
}catch (Exception e){
e.printStackTrace();
}
return response;
}
public Boolean checkApproval(String user_hash, String email){
HttpCallHandler httpCallHandler = new HttpCallHandler();
try{
String responseCall = httpCallHandler.getHttp(new URL(Config.USER_APPROVAL+"?user_hash="+user_hash+"&user_email="+email));
return new JSONObject(responseCall).getBoolean("approval");
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public void approveUser(String user_hash, String email){
HttpCallHandler httpCallHandler = new HttpCallHandler();
try{
Map<String, String> map = new HashMap<>();
map.put("user_hash",user_hash);
map.put("email",email);
httpCallHandler.postHttp(new URL(Config.USER_APPROVE),map);
}catch (Exception e){
e.printStackTrace();
}
}
public User getUser(int method, String email, String phone){
HttpCallHandler httpCallHandler = new HttpCallHandler();
User returnUser = new User();
try{
String responseCall="";
if(method==1){
String url = Config.FIND_BY_EMAIL+"?email="+email;
responseCall = httpCallHandler.getHttp(new URL(url));
}else if(method==2){
String url = Config.FIND_BY_PHONE+"?phone="+phone;
responseCall = httpCallHandler.getHttp(new URL(url));
} else {
String url = Config.FIND_BY_EMAIL_AND_PHONE+"?email="+email+"&phone="+phone;
responseCall = httpCallHandler.getHttp(new URL(url));
}
if(responseCall!=""){
JSONObject jsonObject = new JSONObject(responseCall);
returnUser.setName(jsonObject.getString("name"));
returnUser.setFirstname(jsonObject.getString("firstname"));
returnUser.setUser_hash(jsonObject.getString("user_hash"));
returnUser.setApproved(jsonObject.getBoolean("approved"));
return returnUser;
}else{
return null;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}