Esempio n. 1
0
        /// <summary>
        /// A Lambda function that returns a list of orders. Open orders on default.
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The list of orders</returns>
        public async Task <APIGatewayProxyResponse> GetOrdersAsync(APIGatewayProxyRequest request, ILambdaContext context)
        {
            var search = this.DDBContext.ScanAsync <CustomerOrders>(null);
            var page   = await search.GetNextSetAsync();

            //Create a list that will store orders with extra fields cust id and name
            List <Order_AllOrders> orderList = new List <Order_AllOrders>();
            bool isOpen = true;

            //Check for filters
            //check for status(isOpen) filter
            if (request.QueryStringParameters != null && request.QueryStringParameters.ContainsKey("isOpen"))
            {
                isOpen = bool.Parse(request.QueryStringParameters["isOpen"]);
            }

            //Cycle through users and get the orders
            foreach (var customer in page)
            {
                foreach (var item in customer.Orders)
                {
                    //instantiate all order object to convert orders later
                    var allorder = new Order_AllOrders();
                    if (isOpen)
                    {
                        //call function to get customer from customer table
                        LoadDatabase();
                        //call dynamodb
                        var cust = await DDBContext2.LoadAsync <Customers>(customer.CustomerId);

                        if (cust == null)
                        {
                            continue;
                        }

                        //assign all fields appropriately
                        allorder.CustomerId   = customer.CustomerId;
                        allorder.CustomerName = cust.Name;
                        allorder.DateTime     = item.DateTime;
                        allorder.Id           = item.Id;
                        allorder.IsOpen       = item.IsOpen;
                        allorder.Products     = item.Products;
                        allorder.Signature    = item.Signature;
                        allorder.TotalPrice   = item.TotalPrice;

                        //check status
                        if (item.IsOpen == true)
                        {
                            orderList.Add(allorder);
                        }
                    }
                    else
                    {
                        //call function to get customer from customer table
                        LoadDatabase();
                        //call dynamodb
                        var cust = await DDBContext2.LoadAsync <Customers>(customer.CustomerId);

                        //convert order to allorders -> assign all fields appropriately
                        allorder.CustomerId   = customer.CustomerId;
                        allorder.CustomerName = cust.Name;
                        allorder.DateTime     = item.DateTime;
                        allorder.Id           = item.Id;
                        allorder.IsOpen       = item.IsOpen;
                        allorder.Products     = item.Products;
                        allorder.Signature    = item.Signature;
                        allorder.TotalPrice   = item.TotalPrice;

                        //add allorders to list
                        orderList.Add(allorder);
                    }
                }
            }

            //Sort order list by date Ascending
            orderList.Sort((o1, o2) => DateTime.Compare(o2.DateTime, o1.DateTime));
            context.Logger.LogLine($"Found {page.Count} orders");

            //Response
            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = LowercaseJsonSerializer.SerializeObject(orderList),
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "application/json; charset=utf-8" }
                }
            };

            return(response);
        }
Esempio n. 2
0
        /// <summary>
        /// A Lambda function that returns orders from a single customer. WORST CODE IN THIS PROJECT - DO IT BETTER PLS
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <APIGatewayProxyResponse> GetOrdersByCustAsync(APIGatewayProxyRequest request, ILambdaContext context)
        {
            //Two parameters should be passed. Customer Id is required to load orders from customer
            bool   isOpen       = true;
            string customerId   = null;
            string customerName = null;
            List <Order_AllOrders> orderList = new List <Order_AllOrders>();

            //check for customerId parameter
            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];
            }
            //check for status(isOpen) filter
            if (request.QueryStringParameters != null && request.QueryStringParameters.ContainsKey("isOpen"))
            {
                isOpen = bool.Parse(request.QueryStringParameters["isOpen"]);
            }

            //Check if CustomerId exist; else send bad request response.
            if (string.IsNullOrEmpty(customerId))
            {
                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.BadRequest,
                    Body = $"Missing required parameter {ID_QUERY_STRING_NAME}"
                });
            }

            //Function to load from dynamodb
            context.Logger.LogLine($"Getting orders from customer {customerId}");
            var orders = new CustomerOrders();

            try
            {
                orders = await DDBContext.LoadAsync <CustomerOrders>(customerId);

                orders.Orders.Sort((o1, o2) => DateTime.Compare(o2.DateTime, o1.DateTime));
            }
            catch (Exception e)
            {
                //check if customer exist in orders table
                context.Logger.LogLine($"There was an error: {e}");
            }


            //check if orders exist in customer
            if (orders == null)
            {
                return(new APIGatewayProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.NotFound,
                    Body = JsonConvert.SerializeObject(new Dictionary <string, string> {
                        { "message", "ORDER_IS_EMPTY" }
                    }),
                    Headers = new Dictionary <string, string> {
                        { "Content-Type", "application/json; charset=utf-8" }
                    }
                });
            }


            //Cycle through users and get the orders
            if (isOpen)
            {
                foreach (var item in orders.Orders)
                {
                    if (item.IsOpen == true)
                    {
                        //call function to get customer from customer table
                        LoadDatabase();
                        //call dynamodb
                        var search = this.DDBContext2.ScanAsync <Customers>(null);
                        var page   = await search.GetNextSetAsync();

                        foreach (var cust in page)
                        {
                            if (cust.Id == customerId)
                            {
                                customerName = cust.Name;
                            }
                        }
                        //assign all fields appropriately
                        //instantiate all order object to convert orders later
                        var allorder = new Order_AllOrders
                        {
                            CustomerId   = orders.CustomerId,
                            CustomerName = customerName,
                            DateTime     = item.DateTime,
                            Id           = item.Id,
                            IsOpen       = item.IsOpen,
                            Products     = item.Products,
                            Signature    = item.Signature,
                            TotalPrice   = item.TotalPrice
                        };
                        orderList.Add(allorder);
                    }
                }
            }
            else
            {
                foreach (var item in orders.Orders)
                {
                    //call function to get customer from customer table
                    LoadDatabase();
                    //call dynamodb
                    var search = this.DDBContext2.ScanAsync <Customers>(null);
                    var page   = await search.GetNextSetAsync();

                    foreach (var cust in page)
                    {
                        if (cust.Id == customerId)
                        {
                            customerName = cust.Name;
                        }
                    }
                    //assign all fields appropriately
                    //instantiate all order object to convert orders later
                    var allorder = new Order_AllOrders
                    {
                        CustomerId   = orders.CustomerId,
                        CustomerName = customerName,
                        DateTime     = item.DateTime,
                        Id           = item.Id,
                        IsOpen       = item.IsOpen,
                        Products     = item.Products,
                        Signature    = item.Signature,
                        TotalPrice   = item.TotalPrice
                    };
                    orderList.Add(allorder);
                }
            }



            //response
            var response = new APIGatewayProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body       = LowercaseJsonSerializer.SerializeObject(orderList),
                Headers    = new Dictionary <string, string> {
                    { "Content-Type", "application/json; charset=utf-8" }
                }
            };

            return(response);
        }