/// <inheritdoc />
        public async Task Invoke(PerformContext context)
        {
            context.WriteLine($"> Get Blueprint part and attach image");
            byte[] data;
            using (var webClient = new WebClient()) {
                var stream       = webClient.OpenRead(new Uri(BlueprintImageUrl));
                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream);

                data = memoryStream.ToArray();
            }
            var blueprintImage = await _persister.AddOrUpdateImage(new Image()
            {
                Data = data,
            });

            var blueprintPart = await _context.PrimeParts.FirstOrDefaultAsync(p => p.Name == "Blueprint");

            blueprintPart.ImageId = blueprintImage.Id;
            blueprintPart.Image   = blueprintImage;
            var collarBlueprintPart = await _context.PrimeParts.FirstOrDefaultAsync(p => p.Name == "Collar Blueprint");

            collarBlueprintPart.ImageId = blueprintImage.Id;
            collarBlueprintPart.Image   = blueprintImage;
            await _context.SaveChangesAsync();

            context.WriteLine($"< Image attached");
        }
        /// <inheritdoc />
        public async Task Invoke(PerformContext context)
        {
            List <Exception> errors = new List <Exception>();

            context.WriteLine($"> Fetch all prime items");
            var primeItems = await _context.Set <PrimeItem>()
                             .Include(item => item.PrimePartIngredients)
                             .ThenInclude(i => i.PrimePart)
                             .Include(part => part.PrimePartIngredients)
                             .ThenInclude(i => i.RelicDrops)
                             .ThenInclude(drop => drop.Relic)
                             .Include(item => item.IngredientsGroups)
                             .ThenInclude(g => g.ResourceIngredients)
                             .ThenInclude(i => i.Resource)
                             .ToArrayAsync();

            var primeItemsCount = primeItems.Length;

            context.WriteLine($"< Found {primeItemsCount} items");

            context.WriteLine($"> Start fetching data per item");
            var progressBar = context.WriteProgressBar();
            var count       = 0;

            foreach (var item in primeItems)
            {
                try {
                    var itemData = await _itemScraper.GetData(item.WikiUrl);

                    var toSave = await _persister.AddOrUpdatePrimeItem(item, itemData);
                }
                catch (Exception ex) {
                    errors.Add(ex);
                    context.SetTextColor(ConsoleTextColor.Red);
                    context.WriteLine("Error occured when trying to parse " + item.Name);
                    context.WriteLine(ex);
                    context.WriteLine("");
                    context.ResetTextColor();
                }
                count++;
                progressBar.SetValue(100 * count / primeItemsCount);
            }
            progressBar.SetValue(100);
            context.WriteLine($"< Done setting item data");

            context.WriteLine($"> Saving data");
            var rowsChanged = await _context.SaveChangesAsync();

            context.WriteLine($"< Data saved: {rowsChanged} rows changed");

            if (errors.Count > 0)
            {
                context.SetTextColor(ConsoleTextColor.Red);
                throw new AggregateException(errors);
            }
        }
        public async Task SaveItemData(int primeItemId, string url)
        {
            // Arrange
            var primeItem = await _context.Set <PrimeItem>()
                            .Include(i => i.PrimePartIngredients)
                            .ThenInclude(i => i.PrimePart)
                            .ThenInclude(p => p.Image)
                            .Include(i => i.PrimePartIngredients)
                            .ThenInclude(i => i.RelicDrops)
                            .ThenInclude(r => r.Relic)
                            .Include(i => i.IngredientsGroups)
                            .ThenInclude(g => g.ResourceIngredients)
                            .ThenInclude(i => i.Resource)
                            .FirstOrDefaultAsync(i => i.Id == primeItemId);

            //Act
            var data = await _sut.GetData(url);

            await _persister.AddOrUpdatePrimeItem(primeItem, data);

            await _context.SaveChangesAsync();
        }
        /// <inheritdoc />
        public async Task Invoke(PerformContext context)
        {
            context.WriteLine($"> Fetching page {SimpleRewardsTableUrl}");
            var document = await _fetcher.GetPage(SimpleRewardsTableUrl);

            context.WriteLine($"< Fetched page");

            context.WriteLine($"> Searching for table element and its rows");
            var table    = _finder.GetTableOfSimpleRewardsTable(document);
            var rows     = _finder.GetRowOfSimpleRewardsTable(table).ToArray();
            var rowCount = rows.Length;

            context.WriteLine($"< Found {rowCount} rows");

            context.WriteLine($"> Start parsing rows");
            var primeItems  = new Dictionary <int, PrimeItem>();
            var progressBar = context.WriteProgressBar();
            var count       = 0;

            foreach (var row in rows)
            {
                var rowData = _parser.ParseRowOfSimpleRewardsTable(row);
                if (rowData == null)
                {
                    continue;
                }
                var primeItem = await _persister.AddOrUpdateRowData(rowData);

                primeItems.TryAdd(primeItem.Id, primeItem);
                count++;
                progressBar.SetValue(100 * count / rowCount);
            }

            progressBar.SetValue(100);
            var primeItemsCount = primeItems.Count;

            context.WriteLine($"< Parsed all rows and found {primeItemsCount} prime items");

            context.WriteLine($"> Saving data");
            int rowsChanged = 0;
            int retriesLeft = 3;
            var errors      = new List <Exception>(retriesLeft);

            while (retriesLeft > 0)
            {
                try {
                    rowsChanged = await _context.SaveChangesAsync();
                }
                catch (Exception ex) {
                    retriesLeft--;
                    errors.Add(ex);
                    context.SetTextColor(ConsoleTextColor.Red);
                    context.WriteLine("An error occured while trying to save");
                    context.WriteLine(ex);
                    context.WriteLine("Retries left: " + retriesLeft);
                    context.ResetTextColor();
                    continue;
                }
                break;
            }
            if (retriesLeft == 0)
            {
                throw new AggregateException(errors);
            }
            context.WriteLine($"< Data saved: {rowsChanged} rows changed");
        }
Beispiel #5
0
        /// <inheritdoc />
        public async Task SaveUserData(UserSaveDataDto data)
        {
            // Get the user
            User user;
            var  baseQuery = _context.Users.Include(u => u.UserPrimeItemSaveData).Include(u => u.UserPrimePartIngredientSaveData);

            if (_userData.IsAnonymous)
            {
                user = await baseQuery.FirstOrDefaultAsync(u => u.AnonymousId == _userData.AnonymousId);

                if (user == null)
                {
                    user = new User()
                    {
                        AnonymousId                     = _userData.AnonymousId,
                        UserPrimeItemSaveData           = new List <UserPrimeItemSaveData>(),
                        UserPrimePartIngredientSaveData = new List <UserPrimePartIngredientSaveData>(),
                    };
                    _context.Users.Add(user);
                }
            }
            else
            {
                user = await baseQuery.FirstOrDefaultAsync(u => u.Email == _userData.Email);
            }

            // Clear all user data
            user.UserPrimeItemSaveData.Clear();
            user.UserPrimePartIngredientSaveData.Clear();

            // Fill 'er up again
            foreach (var primeItem in data.PrimeItems)
            {
                if (primeItem.Value.IsChecked)
                {
                    user.UserPrimeItemSaveData.Add(new UserPrimeItemSaveData()
                    {
                        UserId      = user.Id,
                        PrimeItemId = primeItem.Key,
                        IsChecked   = primeItem.Value.IsChecked,
                    });
                }
                foreach (var primePart in primeItem.Value.PrimePartIngredients)
                {
                    if (primePart.Value.CheckedCount > 0)
                    {
                        user.UserPrimePartIngredientSaveData.Add(new UserPrimePartIngredientSaveData()
                        {
                            UserId = user.Id,
                            PrimePartIngredientId = primePart.Key,
                            CheckedCount          = primePart.Value.CheckedCount,
                        });
                    }
                }
            }

            // Make sure User gets updated
            _context.Entry(user).State = EntityState.Modified;

            // Save
            await _context.SaveChangesAsync();
        }
Beispiel #6
0
 /// <inheritdoc />
 public virtual Task <int> SaveChangesAsync()
 {
     return(_context.SaveChangesAsync());
 }