Commit 77626a58 authored by Akhmad Fakhoni Listiyan Dede's avatar Akhmad Fakhoni Listiyan Dede
Browse files

Fixing save offer to database, dikurangi dulu jumlah yang ada di inv, ok

parent c228979b
......@@ -288,14 +288,29 @@ public class Database {
boolean result;
try
{
MongoCollection<Document> collection = database.getCollection("offers");
Document offerDoc = new Document("token", token)
.append("offered_item", indexOfferedItem)
.append("n1", nOffered)
.append("demanded_item", indexDemandedItem)
.append("n2", nDemanded);
collection.insertOne(offerDoc);
result = true;
MongoCollection<Document> invColllection = database.getCollection("user");
ArrayList<Integer> inv = getInventory(token);
if(inv.get(indexOfferedItem) >= nOffered) //Jika jumlah item yang ditawarkan cukup
{
MongoCollection<Document> collection = database.getCollection("offers");
Document offerDoc = new Document("token", token)
.append("offered_item", indexOfferedItem)
.append("n1", nOffered)
.append("demanded_item", indexDemandedItem)
.append("n2", nDemanded);
collection.insertOne(offerDoc);
//Update inventory
inv.set(indexOfferedItem, inv.get(indexOfferedItem) - nOffered);
updateInventory(token, inv);
result = true;
}
else
{
result = false;
}
}catch(MongoException ex){
if(log)
System.out.println("Masuk mongoExeption");
......
......@@ -474,19 +474,19 @@ public class Message {
return response;
}
public static String putOffer(String token, int offeredItem, int n1, int demandedItem, int n2)
public static String putOffer(String tokenOwner, int offeredItem, int n1, int demandedItem, int n2)
{
JSONObject jsonObj;
String response;
try{
if(/*item di inventory cukup dan token ok*/true)
if(Database.saveOfferToTradeBox(tokenOwner, offeredItem, n1, demandedItem, n2))
{
/*simpan offer di database*/
response = statusOKOnly();
}
else
{
jsonObj = new JSONObject();
jsonObj.put("status", "fail");
jsonObj.put("description", "insufficient");
response = jsonObj.toString();
......
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