/// <summary> /// Adds new asset inventory to DB /// </summary> /// <param name="a">Asset inventory</param> /// <returns>Asset inventory</returns> public AssetInventoryModel BuyAsset(AssetInventoryModel a) { var newAssetInventory = new assetInventory { assetId = a.AssetId, teamId = a.GroupId, share = a.Share }; _db.assetInventories.InsertOnSubmit(newAssetInventory); try { _db.SubmitChanges(); } catch (Exception e) { Console.WriteLine(e); } AssetInventoryModel outgoing = GetAssetInventory(a.AssetId, a.GroupId); return(outgoing); }
/// <summary> /// Performs asset trade /// </summary> /// <param name="tm">Trade</param> /// <returns>Changed asset inventories</returns> public List <AssetInventoryModel> TradeAsset(TradingModel tm) { var assetInventories = new List <AssetInventoryModel>(); var assetInventory = new AssetInventoryModel(); IQueryable <assetInventory> query = (from assetInv in _db.assetInventories where assetInv.assetId == tm.AssetId && assetInv.teamId == tm.Buyer select assetInv); if (query.Count() != 0) { assetInventory result = query.First(); result.share += tm.Amount; if (result.share != null) { assetInventory = new AssetInventoryModel(result.Id, tm.AssetId, tm.Buyer, (int)result.share); } } else { var newAssetInventory = new AssetInventoryModel(0, tm.AssetId, tm.Buyer, tm.Amount); assetInventory = _inventoryDal.BuyAsset(newAssetInventory); } // Submit the changes to the database. try { _db.SubmitChanges(); assetInventories.Add(assetInventory); ValueDal.IncreaseOrDecreaseCash(tm.Buyer, -tm.Price); } catch (Exception e) { Console.WriteLine(e); // Provide for exceptions. } if (tm.Owner != 0) { query = (from assetInv in _db.assetInventories where assetInv.assetId == tm.AssetId && assetInv.teamId == tm.Owner select assetInv); if (query.Count() != 0) { assetInventory result = query.First(); result.share -= tm.Amount; if (result.share != null) { assetInventory = new AssetInventoryModel(result.Id, tm.AssetId, tm.Owner, (int)result.share); } } // Submit the changes to the database. try { _db.SubmitChanges(); assetInventories.Add(assetInventory); ValueDal.IncreaseOrDecreaseCash(tm.Owner, tm.Price); } catch (Exception e) { Console.WriteLine(e); // Provide for exceptions. } } return(assetInventories); }