app-mobile/app/src/main/java/monnethic/mobile/restApi/UserApiHandler.java
2019-04-16 21:30:27 +02:00

122 lines
4.4 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 java.util.logging.Logger;
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"));
Log.i("UserApiHandler-register","user_hash :"+jsonObject.getString("user_hash"));
JSONObject jsonReturn = new JSONObject();
jsonReturn.put("user_hash",u.getUser_hash());
//jsonReturn.put("session_id",String.valueOf(jsonObject.getString("session_id"))); //TODO
jsonReturn.put("session_id",String.valueOf(0));
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 {
System.out.println("OUT OUT OUT / "+jsonObject.toString());
u.setUser_hash(jsonObject.getString("user_hash"));
JSONObject jsonReturn = new JSONObject();
jsonReturn.put("status",200);
jsonReturn.put("user_hash",u.getUser_hash());
//jsonReturn.put("session_id",String.valueOf(jsonObject.getString("session_id"))); //TODO
jsonReturn.put("session_id",String.valueOf(0));
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 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"));
return returnUser;
}else{
return null;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}