예제 #1
0
 public IActionResult Create(string nameAuthor)
 {
     authors.AddAuthor(nameAuthor);
     return(RedirectToAction("Index"));
 }
예제 #2
0
        public IActionResult LoadEksmo(FileViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            else
            {
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                List <Author> authors  = new List <Author>();
                List <Book>   books    = new List <Book>();
                string        fileName = "wwwroot/file/" + UpLoadFile(model.FileName);

                using (ExcelPackage excelPackage = new ExcelPackage(new FileInfo(fileName)))
                {
                    ExcelWorksheet sheet = excelPackage.Workbook.Worksheets[0];
                    for (int i = 18; i <= sheet.Dimension.Rows; i++)
                    {
                        authors.Add(new Author()
                        {
                            NameAuthor           = sheet.Cells[i, 7].Value.ToString(),
                            NormalizedNameAuthor = sheet.Cells[i, 7].Value.ToString().ToUpper()
                        });
                    }

                    int index = 0;
                    authors.Sort(Compare);
                    while (index < authors.Count - 1)
                    {
                        if (authors[index].NormalizedNameAuthor == authors[index + 1].NormalizedNameAuthor)
                        {
                            authors.RemoveAt(index);
                        }
                        else
                        {
                            if (!_author.AllAuthors.Any(a => a.NormalizedNameAuthor == authors[index].NormalizedNameAuthor))
                            {
                                _author.AddAuthor(authors[index].NameAuthor);
                            }
                            index++;
                        }
                    }
                    for (int i = 18; i <= sheet.Dimension.Rows; i++)
                    {
                        Console.Write(i + " ");
                        string filter = sheet.Cells[i, 14].Value.ToString();
                        if (filter.Contains("Книга") || filter.Contains("Комплект"))
                        {
                            double test   = 0;
                            int    test_2 = 0;
                            if (filter.Contains("Комплект"))
                            {
                                test   = Double.Parse(sheet.Cells[i, 5].Value.ToString()) * 0.85 + 500;
                                test   = Math.Round(test);
                                test_2 = Int32.Parse(test.ToString());
                            }

                            else
                            {
                                test   = Double.Parse(sheet.Cells[i, 5].Value.ToString()) * 0.85 + 200;
                                test   = Math.Round(test);
                                test_2 = Int32.Parse(test.ToString());
                            }

                            string url  = sheet.Cells[i, 9].Value.ToString();
                            string file = null;
                            using (WebClient client = new WebClient())
                            {
                                file = "../../../../book-site/wwwroot/img/catalog/" + DateTime.Now.DayOfYear + "_" + i + ".jpg";
                                try
                                {
                                    client.DownloadFile(url, file);
                                }
                                catch
                                {
                                    continue;
                                }
                            }

                            books.Add(new Book()
                            {
                                NameBook           = sheet.Cells[i, 6].Value.ToString(),
                                NormalizedNameBook = sheet.Cells[i, 6].Value.ToString().ToUpper(),
                                PublishingHouse    = "Эксмо",
                                Price = test_2,
                                Img   = file
                            });
                        }
                    }
                }
                return(View());
            }
        }