Commit 749c41d2 authored by Akhmad Fakhoni Listiyan Dede's avatar Akhmad Fakhoni Listiyan Dede
Browse files

cancel offer ok. kurang fetchItem sama sendAccept

parent b39d7190
......@@ -453,6 +453,11 @@ public class Database {
return allOffer;
}
/**
* Check availability the offer to be canceled
* @param offer_token
* @return
*/
public static boolean isAvailable(String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
......@@ -481,6 +486,39 @@ public class Database {
return false;
}
}
/**
* Jika berhasil, return 1, jika already taken, return 0, jika tidak ada di database, return -1
* @param token
* @param offer_token
* @return
*/
public static int cancelOffer(String token, String offer_token)
{
MongoCollection<Document> collection = database.getCollection("tradebox");
try {
JSONObject data = new JSONObject(collection.find(eq("_id", new ObjectId(offer_token))));
if(isAvailable(offer_token)) //If isavailable untuk dicancel
{
int itemId = data.getInt("offered_item");
int nItem = data.getInt("n1");
ArrayList<Integer> inv = getInventory(token);
inv.set(itemId, inv.get(itemId) + nItem);
updateInventory(token, inv);
collection.deleteOne(eq("_id", new ObjectId(offer_token)));
return 1;
}
else //already taken
{
return 0;
}
} catch (JSONException | NullPointerException ex) {
System.out.println("[EXEPTION]: cancelOffer database exeption ");
return -1;
}
}
/**
* Mengembalikan token, username, dan password dari username yang dimasukkan
* @param username
......
......@@ -727,6 +727,29 @@ public class Message {
return response;
}
public static String fetchItem(String token, String offer_token) //15
{
return "";
}
public static String cancelOffer(String token, String offer_token)
{
int result = Database.cancelOffer(token, offer_token);
if(result == 1)
{
return statusOKOnly();
}
else if(result == 0)
{
return fail("Alrady Finished");
}
else if(result == -1)
{
return fail("Offer token not exist");
}
else
return error();
}
/**
* Fungsi yang mengembalikan status OK tanpa tambahan status lainnya
* @return String JSON status ok
......
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