Esempio n. 1
0
        public async Task DisplayConfirmDeal(IDialogContext context, IAwaitable <string> result)
        {
            var amazonUrl = await result;

            if (!UrlValidator.IsCountryFromUrlAllowed(amazonUrl))
            {
                await context.PostAsync("Sorry, we only accept links from amazon.com");

                context.Wait(this.MessageReceivedAsync);
            }
            //Not the best way to handle exception, but I don't know any other way.
            else
            {
                var asin = UrlValidator.GetAsin(amazonUrl);

                if (asin.IsNullOrWhiteSpace())
                {
                    await context.PostAsync("Could not validate the amazon url");

                    context.Wait(this.MessageReceivedAsync);
                }
                //Not the best way to handle exception, but I don't know any other way.
                else
                {
                    var message = context.MakeMessage();
                    var item    = _amazonClient.GetDeal(asin);

                    message.Attachments.Add(DialogHelper.CreateDealCard(item));

                    var data = JsonConvert.SerializeObject(_cookie);

                    //TODO: Using name is probably not very safe..
                    //Use internalId instead?
                    var user = _userRepo.Find(x => x.Name == _cookie.UserName).FirstOrDefault();
                    if (user == null)
                    {
                        Debug.WriteLine($"New user. Creating {_cookie.UserName} with {item.Name}");
                        _userRepo.Create(new User
                        {
                            ResumptionCookie = data,
                            Name             = _cookie.UserName,
                            InternalId       = context.Activity.From.Id,
                            Deals            = new List <Deal> {
                                item
                            }
                        });
                    }
                    else
                    {
                        Debug.WriteLine($"Existing user. Updating {user.Name} with {item.Name}");
                        if (user.Deals.All(x => x.Code != item.Code))
                        {
                            user.Deals.Add(item);
                            user.ResumptionCookie = data;
                            _userRepo.Update(user);
                        }
                        else
                        {
                            await context.PostAsync($"Already tracking {item.Name}");

                            context.Wait(this.MessageReceivedAsync);
                        }
                    }

                    await context.PostAsync(message);
                }
            }
        }