Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            if (CatalogFile != null)
            {
                using (var ms = new MemoryStream())
                {
                    CatalogFile.CopyTo(ms);
                    Catalog.File = ms.ToArray();
                }
            }

            _context.Catalogs.Add(Catalog);
            await _context.SaveChangesAsync().ConfigureAwait(false);

            if (!SyncProducts)
            {
                return(RedirectToPage("./Index"));
            }
            var rules    = _context.DiscountRules.ToImmutableList();
            var products = _excelReaderService.GetLines(Catalog, rules);
            await _context.Products.AddRangeAsync(products).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }


            if (CatalogFile != null)
            {
                using (var ms = new MemoryStream())
                {
                    CatalogFile.CopyTo(ms);
                    Catalog.File = ms.ToArray();
                }
            }

            _context.Attach(Catalog).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CatalogExists(Catalog.Id))
                {
                    return(NotFound());
                }
                throw;
            }

            if (!SyncProducts)
            {
                return(RedirectToPage("./Index"));
            }

            _context.Products.RemoveRange(_context.Products.Where(p => p.CatalogId == Catalog.Id).Select(p => new Product {
                Id = p.Id
            }));

            await _context.SaveChangesAsync().ConfigureAwait(false);

            var rules    = _context.DiscountRules.ToImmutableList();
            var products = _excelReaderService.GetLines(Catalog, rules);
            await _context.Products.AddRangeAsync(products).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(RedirectToPage("./Index"));
        }