public async Task <IActionResult> Sell([FromQuery] string userId, [FromQuery] int stockId, [FromQuery] uint amount, [FromQuery] int price) { int sellerId = new Random().Next(10000); var seller = new Seller() { SellerId = sellerId, Name = userId, Stocks = new List <Stock>() { new Stock { StockId = stockId, Amount = (int)amount, Price = price } } }; using (StaticHttpClient client = new StaticHttpClient()) { List <Seller> allSellers; var allSellsersJson = await client.GetAsync("https://itonk-grp2.firebaseio.com/Broker/Seller.json"); if (allSellsersJson != "null") { allSellers = JsonConvert.DeserializeObject <List <Seller> >(allSellsersJson); allSellers.Add(seller); } else { allSellers = new List <Seller>(); allSellers.Add(seller); } var record = await client.PutAsync("https://itonk-grp2.firebaseio.com/Broker/Seller.json", allSellers); } return(Ok()); }
public async Task <IActionResult> Buy([FromBody] ShareOwner buyer) { List <Seller> newSellers = new List <Seller>() { new Seller() { Name = "Peter", SellerId = 1001, CustomerId = 2001, Stocks = new List <Stock>() { new Stock() { StockId = 11, Name = "Shit", Price = 10.0, Amount = 10 } } } }; using (StaticHttpClient client = new StaticHttpClient()) { var host = Environment.GetEnvironmentVariable("PSOC_SERVICE_HOST"); var port = Environment.GetEnvironmentVariable("PSOC_SERVICE_PORT"); var newSellersJson = JsonConvert.SerializeObject(newSellers); await client.PutAsync("https://itonk-grp2.firebaseio.com/Broker/Seller.json", newSellers); List <Buyer> buyers = new List <Buyer>() { new Buyer() { BuyerId = 0, Name = buyer.Name, CustomerId = buyer.CustomerId, Stocks = buyer.Stocks } }; var record = await client.PutAsync("https://itonk-grp2.firebaseio.com/Broker/Buyer.json", buyers); string stocks = await client.GetAsync("https://itonk-grp2.firebaseio.com/Broker/Seller.json"); var sellers = JsonConvert.DeserializeObject <List <Seller> >(stocks); ShareOwner newOwner = null; int wantedAmount = 0; Stock stockToRemove = null; Seller soldBy = null; foreach (var seller in sellers) { foreach (var stock in seller.Stocks) { foreach (var requestedStock in buyer.Stocks) { //You can only buy 1 stock at a time - will be set multiple times, but who cares really? wantedAmount = requestedStock.Amount; if (stock.Name == requestedStock.Name) { newOwner = new ShareOwner() { Name = buyer.Name, CustomerId = buyer.CustomerId, Stocks = new List <Stock>() { stock } }; soldBy = seller; stockToRemove = stock; break; } } } } if (soldBy != null && soldBy.Stocks != null && stockToRemove != null) { soldBy.Stocks.Remove(stockToRemove); } await client.PutAsync("https://itonk-grp2.firebaseio.com/Broker/Seller.json", sellers); // post to Shareowner if (newOwner != null) { //await client.PostAsync(host + port + "/api/shareowner",newOwner); } else { return(BadRequest()); } //await client.PostAsync() // Post to Buyer } return(Ok()); }