private void ProcessOrderDeleteMessage(string msg) { var orderDeleteMessage = OrderDeleteMessage.Parse(msg); OrderDelete?.Invoke(orderDeleteMessage); }
public async Task <IActionResult> DeleteOrder([FromHeader] string Authorization, [FromBody] OrderDelete orderDelete) { try { long timestamp = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; if (orderDelete.OrderId == null) { return(BadRequest("missing variables")); } AmazonDynamoDBClient amazonDynamoDBClient = new AmazonDynamoDBClient(); Table UserTable = Table.LoadTable(amazonDynamoDBClient, "UserDetailsRegistry"); Document searchdetails = new Document(); string socket; try { string[] autharray = Authorization.Split(' '); string authtype = autharray[0]; string authstring = autharray[1]; if (authtype == "Basic") { byte[] data = Convert.FromBase64String(authstring); string decodedString = Encoding.UTF8.GetString(data); string[] splitauth = decodedString.Split(":"); string id = splitauth[0]; string secret = splitauth[1]; Console.WriteLine(id + secret); ScanOperationConfig scanOperation = new ScanOperationConfig(); ScanFilter scanFilter = new ScanFilter(); scanFilter.AddCondition("DefaultId", ScanOperator.Equal, id); Search search = UserTable.Scan(scanOperation); List <Document> details = await search.GetRemainingAsync(); foreach (Document doc in details) { if (doc["DefaultSecret"] == secret && doc["DefaultId"] == id) { Console.WriteLine("successful auth"); searchdetails = doc; break; } else { continue; } } searchdetails["Email"].ToString(); } else { return(BadRequest("Bad authorization")); } } catch { return(BadRequest("authorizaion failed")); } Table ordertable = Table.LoadTable(amazonDynamoDBClient, "OrderRegistry"); try { Document deletedoc = await ordertable.GetItemAsync(orderDelete.OrderId); if (deletedoc["Email"].ToString() != searchdetails["Email"].ToString()) { return(BadRequest("order not found")); } deletedoc["Modify"] = "true"; await ordertable.UpdateItemAsync(deletedoc); /*deletedoc = await ordertable.DeleteItemAsync(orderDelete.OrderId); * var returnobj = new { Id = orderDelete.OrderId, Message = "delete successful", Timestamp = timestamp.ToString() }; * string returnstring = JsonConvert.SerializeObject(returnobj);*/ // send tcp message to engine try { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("52.213.34.99", port); // use the ipaddress as in the server program Console.WriteLine("Connected"); Console.Write("Enter the string to be transmitted : "); var enginepayload = new { Method = "Delete", Id = orderDelete.OrderId, User = searchdetails["Email"].ToString(), AvailableBalance = searchdetails["AvailableBalance"].ToString() }; using (SslStream sslStream = new SslStream(tcpclnt.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null)) { sslStream.AuthenticateAsClient("52.213.34.99"); // This is where you read and send data String str = "enginekey" + JsonConvert.SerializeObject(enginepayload); //Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(str); Console.WriteLine("Transmitting....."); sslStream.Write(ba, 0, ba.Length); sslStream.Close(); /*byte[] bb = new byte[1000]; * int k = await sslStream.ReadAsync(bb, 0, 1000); * * var socketresult = Encoding.UTF8.GetString(bb).TrimEnd('\0'); * Console.WriteLine(socketresult); * socket = socketresult;*/ } tcpclnt.Close(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); return(BadRequest("error with engine")); } var returnobj = new { Timestamp = timestamp.ToString(), Result = "sucess" }; string returnjson = JsonConvert.SerializeObject(returnobj); return(Content(returnjson, "application/json")); } catch { return(BadRequest("order not found")); } } catch { return(BadRequest("something went wrong")); } }