public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Price,Information,CompProdId,ColorId")] ModelsOfProduct modelsOfProduct)
        {
            if (id != modelsOfProduct.Id)
            {
                return(NotFound());
            }
            var compProdId = modelsOfProduct.CompProdId;
            var CompProd   = _context.CompanyProducts.Where(c => c.Id == compProdId).FirstOrDefault();

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(modelsOfProduct);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ModelsOfProductExists(modelsOfProduct.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("Index", "ModelsOfProducts", new { id = modelsOfProduct.CompProdId, companyId = CompProd.CompanyId, productId = CompProd.ProductId }));
            }
            ViewData["ColorId"]    = new SelectList(_context.Colors, "Id", "Id", modelsOfProduct.ColorId);
            ViewData["CompProdId"] = new SelectList(_context.CompanyProducts, "Id", "Id", modelsOfProduct.CompProdId);
            //return View(modelsOfProduct);
            return(RedirectToAction("Index", "ModelsOfProducts", new { id = modelsOfProduct.CompProdId, companyId = CompProd.CompanyId, productId = CompProd.ProductId }));
        }
Example #2
0
 private bool IsExistModel(ModelsOfProduct model)
 {
     foreach (var i in _context.ModelsOfProduct)
     {
         if (String.Compare(i.Name.ToUpper(), model.Name.ToUpper()) == 0 && model.Price == i.Price && model.Color == i.Color)
         {
             return(true);
         }
     }
     return(false);
 }
        public async Task <IActionResult> Create(int compProdId, [Bind("Id,Name,Price,Information,CompProdId,ColorId")] ModelsOfProduct modelsOfProduct)
        {
            modelsOfProduct.CompProdId = compProdId;
            var CompProd = _context.CompanyProducts.Where(c => c.Id == compProdId).FirstOrDefault();

            if (ModelState.IsValid)
            {
                _context.Add(modelsOfProduct);
                await _context.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("Index", "ModelsOfProducts", new { id = modelsOfProduct.CompProdId, companyId = CompProd.CompanyId, productId = CompProd.ProductId }));
            }
            //ViewData["ColorId"] = new SelectList(_context.Colors, "Id", "Id", modelsOfProduct.ColorId);
            //ViewData["CompProdId"] = new SelectList(_context.CompanyProducts, "Id", "Id", modelsOfProduct.CompProdId);
            //return View(modelsOfProduct);
            return(RedirectToAction("Index", "ModelsOfProducts", new { id = modelsOfProduct.CompProdId, companyId = CompProd.CompanyId, productId = CompProd.ProductId }));
        }
Example #4
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (fileExcel == null)
                    {
                        throw new FileLoadException("Файл не обрано");
                    }
                    if (fileExcel != null)
                    {
                        using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                        {
                            await fileExcel.CopyToAsync(stream);

                            using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled))
                            {
                                foreach (IXLWorksheet worksheet in workBook.Worksheets)
                                {
                                    Products newCategory;
                                    var      c = (from cat in _context.Products
                                                  where cat.Name.Contains(worksheet.Name)
                                                  select cat).ToList();
                                    if (c.Count > 0)
                                    {
                                        newCategory = c[0];
                                    }
                                    else
                                    {
                                        if (Regex.IsMatch(worksheet.Name, _regexCategory) == false)
                                        {
                                            throw new Exception("Некоректна назва аркуша");
                                        }
                                        newCategory      = new Products();
                                        newCategory.Name = worksheet.Name;
                                        _context.Products.Add(newCategory);
                                    }
                                    //await _context.SaveChangesAsync();

                                    CheckNameRow(worksheet); //проверка рядка с названиями столбцов
                                                             //просмотр всех рядков
                                    foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                    {
                                        if (Regex.IsMatch(row.Cell(1).Value.ToString(), _regexModel) == false)
                                        {
                                            throw new Exception("Некоректна назва товару");
                                        }
                                        ModelsOfProduct Model = new ModelsOfProduct();
                                        Model.Name = row.Cell(1).Value.ToString();
                                        if (Model.Name == "")
                                        {
                                            throw new Exception("Порожнє поле назви товару");
                                        }
                                        if (row.Cell(3).Value.ToString() != "")
                                        {
                                            try
                                            {
                                                Model.Price = Convert.ToDouble(row.Cell(3).Value);
                                            }
                                            catch (Exception)
                                            {
                                                throw new Exception("Некоректне поле ціни");
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception("Порожнє поле ціни");
                                        }
                                        Model.Information = row.Cell(4).Value.ToString();

                                        if (row.Cell(2).Value.ToString().Length > 0)
                                        {
                                            Companies Company;
                                            var       comp = (from cmp in _context.Companies
                                                              where cmp.Name.Contains(row.Cell(2).Value.ToString())
                                                              select cmp).ToList();
                                            if (comp.Count > 0)
                                            {
                                                Company = comp[0];
                                            }
                                            else
                                            {
                                                if (Regex.IsMatch(row.Cell(2).Value.ToString(), _regexCompany) == false)
                                                {
                                                    throw new Exception("Некоректна назва компанії");
                                                }
                                                Company      = new Companies();
                                                Company.Name = row.Cell(2).Value.ToString();
                                                _context.Companies.Add(Company);
                                            }
                                            //await _context.SaveChangesAsync();

                                            if (row.Cell(5).Value.ToString().Length > 0)
                                            {
                                                Colors Color;
                                                var    clr = (from col in _context.Colors
                                                              where col.Name.Contains(row.Cell(5).Value.ToString())
                                                              select col).ToList();
                                                if (clr.Count > 0)
                                                {
                                                    Color = clr[0];
                                                }
                                                else
                                                {
                                                    if (Regex.IsMatch(row.Cell(5).Value.ToString(), _regexColor) == false)
                                                    {
                                                        throw new Exception("Некоректна назва кольору");
                                                    }
                                                    Color      = new Colors();
                                                    Color.Name = row.Cell(5).Value.ToString();
                                                    _context.Colors.Add(Color);
                                                }
                                                Model.Color = Color;

                                                CompanyProducts companyProducts;
                                                if (!IsExistCompanyInProduct(newCategory, Company))
                                                {
                                                    companyProducts         = new CompanyProducts();
                                                    companyProducts.Product = newCategory;
                                                    companyProducts.Company = Company;
                                                    _context.CompanyProducts.Add(companyProducts);
                                                }
                                                else
                                                {
                                                    companyProducts = _context.CompanyProducts.Where(c => c.CompanyId == Company.Id && c.ProductId == newCategory.Id).FirstOrDefault();
                                                }
                                                await _context.SaveChangesAsync();

                                                Model.CompProdId = companyProducts.Id;
                                            }
                                            else
                                            {
                                                throw new Exception("Порожнє поле кольору");
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception("Порожня назва компанії");
                                        }
                                        if (!IsExistModel(Model))
                                        {
                                            _context.ModelsOfProduct.Add(Model);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (ArgumentException ex)
                {
                    //неправильное название столбцов
                    return(RedirectToAction("Index", "Products", new { searchString = "", errorString = ex.Message }));
                }
                catch (FileLoadException ex)
                {
                    //ошибка загрузки файла или файл не выбран
                    return(RedirectToAction("Index", "Products", new { errorString = ex.Message }));
                }
                catch (Exception ex)
                {
                    //некоретные данные
                    if (ex.Message == "")
                    {
                        return(RedirectToAction("Index", "Products", new { errorString = "Помилка завантаження файлу" }));
                    }
                    return(RedirectToAction("Index", "Products", new { errorString = ex.Message }));
                }


                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }