public async Task <T> AddAsync(T entity)
        {
            _dbContext.Set <T>().Add(entity);
            await _dbContext.SaveChangesAsync();

            return(entity);
        }
Beispiel #2
0
        public static async Task SeedAsync(GroceryContext context)
        {
            if (!context.CatalogItems.Any(x => x.Id == -1) && context.CatalogTypes.Any())
            {
                context.CatalogItems.Add(new CatalogItem
                {
                    Id          = -1,
                    Name        = "Produto Personalizado",
                    ShowOnShop  = false,
                    CatalogType = context.CatalogTypes.First()
                });

                await context.SaveChangesAsync();
            }

            if (!context.invoiceConfigs.Any())
            {
                foreach (var item in _weeks)
                {
                    context.invoiceConfigs.Add(new InvoiceConfig
                    {
                        Week = item
                    });
                }
                await context.SaveChangesAsync();
            }
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                await PopulateListAsync();

                return(Page());
            }

            if (!CatalogCategoryModel.Any(x => x.Selected))
            {
                await PopulateListAsync();

                ModelState.AddModelError("", "Selecciona pelo menos uma categoria");
                return(Page());
            }

            CatalogItem.Sku = _context.CatalogTypes.Find(CatalogItem.CatalogTypeId).Code;
            //Save Main Image
            if (Picture?.Length > 0)
            {
                var lastCatalogItemId = (await _context.CatalogItems.AnyAsync()) ? (await _context.CatalogItems.LastAsync()).Id : 0;

                CatalogItem.PictureUri = _service.SaveFile(Picture, _backofficeSettings.GroceryProductsPictureFullPath, _backofficeSettings.GroceryProductsPictureUri, (++lastCatalogItemId).ToString(), true, 500, 500).PictureUri;
            }

            //Catalog Catagories
            CatalogItem.CatalogCategories = new List <CatalogCategory>();
            foreach (var item in CatalogCategoryModel.Where(x => x.Selected).ToList())
            {
                CatalogItem.CatalogCategories.Add(new CatalogCategory
                {
                    CategoryId = item.CategoryId
                });
                foreach (var child in item.Childs.Where(x => x.Selected).ToList())
                {
                    CatalogItem.CatalogCategories.Add(new CatalogCategory
                    {
                        CategoryId = child.CategoryId
                    });
                }
            }

            _context.CatalogItems.Add(CatalogItem);
            await _context.SaveChangesAsync();

            //Update SKU
            CatalogItem.Sku += "-" + CatalogItem.Id;
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            PopulateList();
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //check if name exists
            if (_context.Categories.Any(x => x.Name.ToUpper() == Category.Name.ToUpper()))
            {
                ModelState.AddModelError("", $"O nome da Categoria '{Category.Name}' já existe!");
                return(Page());
            }

            if (Category.ParentId == 0)
            {
                Category.ParentId = null;
            }

            _context.Categories.Add(Category);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Beispiel #5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //check if code exists
            if (_context.CatalogTypes.Any(x => x.Code.ToUpper() == CatalogType.Code.ToUpper() && x.Id != CatalogType.Id))
            {
                ModelState.AddModelError("", $"O nome do Tipo de Produto '{CatalogType.Code}' já existe!");
                return(Page());
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CatalogTypeExists(CatalogType.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Category = await _context.Categories.FindAsync(id);

            if (Category != null)
            {
                _context.Categories.Remove(Category);
                await _context.SaveChangesAsync();
            }

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

            //check if code exists
            if (_context.CatalogTypes.Any(x => x.Code.ToUpper() == CatalogType.Code.ToUpper()))
            {
                ModelState.AddModelError("", $"O nome do Tipo de Produto '{CatalogType.Code}' já existe!");
                return(Page());
            }

            _context.CatalogTypes.Add(CatalogType);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Beispiel #8
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                await PopulateLists();

                return(Page());
            }

            if (!CatalogCategoryModel.Any(x => x.Selected))
            {
                await PopulateLists();

                ModelState.AddModelError("", "Selecciona pelo menos uma categoria");
                return(Page());
            }

            //Sku
            CatalogItem.Sku = $"{_context.CatalogTypes.Find(CatalogItem.CatalogTypeId).Code}-{CatalogItem.Id}";

            //Save Image
            if (Picture?.Length > 0)
            {
                if (!string.IsNullOrEmpty(CatalogItem.PictureUri))
                {
                    _service.DeleteFile(_backofficeSettings.GroceryProductsPictureFullPath, Utils.GetFileName(CatalogItem.PictureUri));
                }
                CatalogItem.PictureUri = _service.SaveFile(Picture, _backofficeSettings.GroceryProductsPictureFullPath, _backofficeSettings.GroceryProductsPictureUri, CatalogItem.Id.ToString(), true, 500, 500).PictureUri;
            }

            //CatalogCategories
            var catalogCategoriesDb = await _context.CatalogCategories
                                      .Include(x => x.Category)
                                      .Where(x => x.CatalogItemId == CatalogItem.Id)
                                      .ToListAsync();

            //Novos
            foreach (var item in CatalogCategoryModel.Where(x => x.Selected).ToList())
            {
                if (catalogCategoriesDb == null || !catalogCategoriesDb.Any(x => x.CategoryId == item.CategoryId))
                {
                    CatalogItem.CatalogCategories.Add(new CatalogCategory
                    {
                        CategoryId = item.CategoryId
                    });
                }
                foreach (var child in item.Childs.Where(x => x.Selected).ToList())
                {
                    if (catalogCategoriesDb == null || !catalogCategoriesDb.Any(x => x.CategoryId == child.CategoryId))
                    {
                        CatalogItem.CatalogCategories.Add(new CatalogCategory
                        {
                            CategoryId = child.CategoryId
                        });
                    }
                }
            }
            //Remover
            foreach (var item in CatalogCategoryModel.ToList())
            {
                if (!item.Selected)
                {
                    var catalogCategory = catalogCategoriesDb.SingleOrDefault(x => x.CategoryId == item.CategoryId);
                    if (catalogCategory != null)
                    {
                        _context.Entry(catalogCategory).State = EntityState.Deleted;
                    }
                }


                foreach (var child in item.Childs.Where(x => !x.Selected).ToList())
                {
                    var catalogCategory = catalogCategoriesDb.SingleOrDefault(x => x.CategoryId == child.CategoryId);
                    if (catalogCategory != null)
                    {
                        _context.Entry(catalogCategory).State = EntityState.Deleted;
                    }
                }
            }

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CatalogItemExists(CatalogItem.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

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