Пример #1
0
        private static void UpdateDynamoDB(string type, string aisleIdentifier, string itemsStr, int customerId)
        {
            Document document = null;

            if ((string.IsNullOrWhiteSpace(aisleIdentifier)) || (string.IsNullOrWhiteSpace(itemsStr)) || (customerId < 1))
            {
                Console.WriteLine($"Incorrect event type({type}), data aisle({aisleIdentifier}), items({itemsStr}), cusomter({customerId})");
                return;
            }

            string[] items = itemsStr.Split(new char[] { ',' });
            for (int j = 0; j < items.Length; j++)
            {
                items[j] = items[j].Trim();
            }

            switch (type)
            {
            case "TurnOff":
                foreach (string item in items)
                {
                    document = GetRecommendation(aisleIdentifier, item);
                    if ((document != null) && (!string.IsNullOrWhiteSpace(document[AisleIdIdentifier].AsString())))
                    {
                        DynamoDBEntry customersValue = document[CustomersIdentifier];
                        if (customersValue is PrimitiveList)
                        {
                            List <Primitive> customerIds = customersValue.AsPrimitiveList().Entries;

                            bool containsCustomer = false;
                            foreach (Primitive cusId in customerIds)
                            {
                                if (cusId.AsInt() == customerId)
                                {
                                    containsCustomer = true;
                                    break;
                                }
                            }

                            // If this is the only customer interested in that recommendation, delete else update
                            if (containsCustomer)
                            {
                                if (customerIds.Count <= 1)
                                {
                                    DeleteRecommendation(document);
                                }
                                else
                                {
                                    for (int k = customerIds.Count - 1; k > -1; k--)
                                    {
                                        if (customerIds[k].AsInt() == customerId)
                                        {
                                            customerIds.RemoveAt(k);
                                            break;
                                        }
                                    }
                                    document[CustomersIdentifier] = customerIds;
                                    UpdateRecommendation(document);
                                }
                            }
                        }
                    }
                }
                break;

            case "Highlight":
            case "Restock":
                foreach (string item in items)
                {
                    document = GetRecommendation(aisleIdentifier, item);
                    if ((document != null) && (!string.IsNullOrWhiteSpace(document[AisleIdIdentifier].AsString())))
                    {
                        Console.WriteLine("Updating recommendation");
                        Console.WriteLine($"Event type({type}), data aisle({aisleIdentifier}), items({item}), cusomter({customerId})");
                        if (type == "Highlight")
                        {
                            document[HighlightIdentifier] = new DynamoDBBool(true);
                        }
                        else
                        {
                            document[RestockIdentifier] = new DynamoDBBool(true);
                        }

                        DynamoDBEntry customersValue = document[CustomersIdentifier];
                        if (customersValue is PrimitiveList)
                        {
                            List <Primitive> customerIds = customersValue.AsPrimitiveList().Entries;
                            if (!customerIds.Contains(customerId))
                            {
                                customersValue.AsPrimitiveList().Add(customerId);
                            }
                            document[CustomersIdentifier] = customersValue;
                        }
                        UpdateRecommendation(document);
                    }
                    else
                    {
                        Console.WriteLine("Creating recommendation");
                        Console.WriteLine($"Event type({type}), data aisle({aisleIdentifier}), items({item}), cusomter({customerId})");
                        if (type == "Highlight")
                        {
                            CreateEntry(aisleIdentifier, item, customerId, true, false);
                        }
                        else
                        {
                            CreateEntry(aisleIdentifier, item, customerId, false, true);
                        }
                    }
                }
                break;
            }
        }