Commit 6e4ec89c authored by Kevin's avatar Kevin
Browse files

endpoint kecil-kecilan

parent 4572a1f7
{"kevin":{"r21":0,"r32":0,"r31":0,"password":"123","r12":0,"r23":0,"r11":0,"r22":0,"r14":0,"r13":0,"position":"nanti_dipikirin","r41":0}}
\ No newline at end of file
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Kevin Zhong Local
*/
public class EndpointMethod {
public String inventory(String username) throws IOException, ParseException {
JSONObject resultJson = new JSONObject();
JSONArray inventory_list = new JSONArray();
JSONParser parser = new JSONParser();
try {
String basePath = new File("").getAbsolutePath();
Object obj = parser.parse(new FileReader(basePath + "//file_saved.json"));
if (obj != null) {
JSONObject jsonFile = (JSONObject) obj;
JSONObject userJson = (JSONObject) jsonFile.get(username.toLowerCase());
inventory_list.add(userJson.get("r11"));
inventory_list.add(userJson.get("r12"));
inventory_list.add(userJson.get("r13"));
inventory_list.add(userJson.get("r14"));
inventory_list.add(userJson.get("r21"));
inventory_list.add(userJson.get("r22"));
inventory_list.add(userJson.get("r23"));
inventory_list.add(userJson.get("r31"));
inventory_list.add(userJson.get("r32"));
inventory_list.add(userJson.get("r41"));
resultJson.put("status", "ok");
resultJson.put("inventory", inventory_list);
return resultJson.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
resultJson.put("status", "error");
return resultJson.toString();
}
public static void main(String argv[]) throws IOException, ParseException{
EndpointMethod tes=new EndpointMethod();
// System.out.println(tes.inventory("Kevi"));
}
}
...@@ -10,14 +10,15 @@ ...@@ -10,14 +10,15 @@
*/ */
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.UUID;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
class TCPServer class TCPServer {
{
public static void main(String argv[]) throws Exception public static void main(String argv[]) throws Exception {
{ // ConnectionHandler CH = new ConnectionHandler();
ConnectionHandler CH = new ConnectionHandler(); // CH.start();
CH.start();
} }
} }
...@@ -18,7 +18,7 @@ import org.json.simple.parser.JSONParser; ...@@ -18,7 +18,7 @@ import org.json.simple.parser.JSONParser;
*/ */
public class crudDB { public class crudDB {
public boolean signUp(String username, String password) throws IOException { public String signUp(String username, String password) throws IOException {
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
try { try {
String basePath = new File("").getAbsolutePath(); String basePath = new File("").getAbsolutePath();
...@@ -26,7 +26,10 @@ public class crudDB { ...@@ -26,7 +26,10 @@ public class crudDB {
if (obj != null) { if (obj != null) {
JSONObject jsonFile = (JSONObject) obj; JSONObject jsonFile = (JSONObject) obj;
if (jsonFile.containsKey(username.toLowerCase())) { if (jsonFile.containsKey(username.toLowerCase())) {
return false; JSONObject resultJson = new JSONObject();
resultJson.put("status", "fail");
resultJson.put("description", "username exists");
return resultJson.toString();
} else { } else {
JSONObject userObj = new JSONObject(); JSONObject userObj = new JSONObject();
userObj.put("password", password); userObj.put("password", password);
...@@ -47,13 +50,17 @@ public class crudDB { ...@@ -47,13 +50,17 @@ public class crudDB {
file.write(jsonFile.toJSONString()); file.write(jsonFile.toJSONString());
file.flush(); file.flush();
file.close(); file.close();
return true; JSONObject resultJson = new JSONObject();
resultJson.put("status", "ok");
return resultJson.toString();
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return false; JSONObject resultJson = new JSONObject();
resultJson.put("status", "error");
return resultJson.toString();
} }
public void editAtt(String username, String att, String newValue) { public void editAtt(String username, String att, String newValue) {
...@@ -79,7 +86,7 @@ public class crudDB { ...@@ -79,7 +86,7 @@ public class crudDB {
} }
} }
public boolean login(String username, String password) { public String login(String username, String password) {
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
try { try {
String basePath = new File("").getAbsolutePath(); String basePath = new File("").getAbsolutePath();
...@@ -89,15 +96,33 @@ public class crudDB { ...@@ -89,15 +96,33 @@ public class crudDB {
if (jsonFile.containsKey(username.toLowerCase())) { if (jsonFile.containsKey(username.toLowerCase())) {
JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase()); JSONObject usrObj = (JSONObject) jsonFile.get(username.toLowerCase());
if (password.equals(usrObj.get("password").toString())) { if (password.equals(usrObj.get("password").toString())) {
return true; JSONObject resultJson = new JSONObject();
resultJson.put("status", "ok");
resultJson.put("token", "skill kripto");
resultJson.put("x", "");
resultJson.put("y", "");
long epoch=System.currentTimeMillis()/1000;
resultJson.put("time", epoch);
return resultJson.toString();
} else { } else {
return false; JSONObject resultJson = new JSONObject();
resultJson.put("status", "fail");
resultJson.put("description","username or password combination is not found");
return resultJson.toString();
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return false; JSONObject resultJson = new JSONObject();
resultJson.put("status", "error");
return resultJson.toString();
}
public static void main(String argv[]) throws Exception {
crudDB tes=new crudDB();
// System.out.println(tes.signUp("Kevin", "123"));
// System.out.println(tes.login("Kevin", "123"));
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment