/// <summary> /// A Lambda function that removes an order post from Customer in Orders table. /// </summary> /// <param name="request"></param> public async Task <APIGatewayProxyResponse> RemoveOrderAsync(APIGatewayProxyRequest request, ILambdaContext context) { string customerId = null; string orderId = null; if (request.PathParameters != null && request.PathParameters.ContainsKey(ID_QUERY_STRING_NAME)) { customerId = request.PathParameters[ID_QUERY_STRING_NAME]; } if (request.QueryStringParameters != null && request.QueryStringParameters.ContainsKey("orderId")) { orderId = request.QueryStringParameters["orderId"]; } if (string.IsNullOrEmpty(orderId) || string.IsNullOrEmpty(customerId)) { return(new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.BadRequest, Body = $"Missing required parameter {ID_QUERY_STRING_NAME}" }); } var cust = await DDBContext.LoadAsync <CustomerOrders>(customerId); if (cust != null) { foreach (var order in cust.Orders.ToList()) { if (order.Id == orderId) { cust.removeList(order); } } //Update Dynamodb await DDBContext.SaveAsync <CustomerOrders>(cust); } else { context.Logger.LogLine("There is an error please call the server master"); } return(new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = JsonConvert.SerializeObject(new Dictionary <string, string> { { "message", $"Sucessfully deleted the item: {orderId}" } }), Headers = new Dictionary <string, string> { { "Content-Type", "application/json; charset=utf-8" } } }); }
public async Task <bool> SaveUser(UserProfile userProfile) { try { return(await DDBContext.SaveAsync(userProfile).ContinueWith(task => { if (task.IsFaulted) { Console.WriteLine(task.Exception.Message); } return !task.IsFaulted; })); } catch (Exception ex) { throw ex; } }
/// <summary> /// A Lambda function that adds an order by customer. /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <APIGatewayProxyResponse> AddOrderAsync(APIGatewayProxyRequest request, ILambdaContext context) { const string url = "https://maoproduce-stack-customer-signatures.s3-ap-southeast-2.amazonaws.com/"; //instantiate new order object Orders newOrder = new Orders(); string customerId = null; string lastOrderId; //check for customerId parameter from url if (request.PathParameters != null && request.PathParameters.ContainsKey(ID_QUERY_STRING_NAME)) { customerId = request.PathParameters[ID_QUERY_STRING_NAME]; } else if (request.QueryStringParameters != null && request.QueryStringParameters.ContainsKey(ID_QUERY_STRING_NAME)) { customerId = request.QueryStringParameters[ID_QUERY_STRING_NAME]; } //GET the last order number var search = this.DDBContext.ScanAsync <CustomerOrders>(null); var page = await search.GetNextSetAsync(); List <int> list = new List <int>(); if (!page.Any()) { lastOrderId = "17050"; } else { foreach (var customer in page) { list.Add(int.Parse(customer.LastOrderId)); } lastOrderId = (list.Max() + 1).ToString(); } //get the request body var requestOrder = JsonConvert.DeserializeObject <Order_AllOrders>(request?.Body); //Convert sent All Orders to Orders Model newOrder.Id = lastOrderId; newOrder.DateTime = DateTime.Now; newOrder.IsOpen = requestOrder.IsOpen; //pass signature data to AWSS3BucketSave Function string signatureTitle = newOrder.Id + "-" + String.Format("{0}.png", DateTime.Now.ToString("ddMMyyyyhhmmsstt")); AWSS3BucketSave bucket = new AWSS3BucketSave(); await bucket.WritingAnObjectAsync(requestOrder.Signature.Signature, signatureTitle); //New instance of signatture with url SignatureDetails sig = new SignatureDetails(); var sigExist = requestOrder.Signature; if (string.IsNullOrEmpty(sigExist.Signature)) { sig.Signature = ""; sig.Signee = requestOrder.Signature.Signee; } else { sig.Signature = url + signatureTitle; sig.Signee = requestOrder.Signature.Signee; } //Save new signature object newOrder.Signature = sig; newOrder.TotalPrice = requestOrder.TotalPrice; newOrder.Products = requestOrder.Products; ////load current customer data in dynamodb order table var custNewOrder = await DDBContext.LoadAsync <CustomerOrders>(customerId); if (custNewOrder != null) { custNewOrder.LastOrderId = lastOrderId; custNewOrder.Orders.Add(newOrder); //Save the order in the right customer. var saveOrder = DDBContext.SaveAsync <CustomerOrders>(custNewOrder); } else { CustomerOrders newCustOrder = new CustomerOrders(); newCustOrder.CustomerId = customerId; newCustOrder.LastOrderId = lastOrderId; newCustOrder.addList(newOrder); //Save to Dynamodb var saveOrder = DDBContext.SaveAsync <CustomerOrders>(newCustOrder); } //create success response var response = new APIGatewayProxyResponse { StatusCode = (int)HttpStatusCode.OK, Body = JsonConvert.SerializeObject(new Dictionary <string, string> { { "message", "Order sucessfully created" }, { "orderId", lastOrderId } }), Headers = new Dictionary <string, string> { { "Content-Type", "application/json; charset=utf-8" } } }; return(response); }