コード例 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,MangaId,AuthorId")] AuthorsManga authorsManga)
        {
            if (id != authorsManga.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(authorsManga);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorsMangaExists(authorsManga.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AuthorId"] = new SelectList(_context.Authors, "Id", "Name", authorsManga.AuthorId);
            ViewData["MangaId"]  = new SelectList(_context.Mangas, "Id", "Name", authorsManga.MangaId);
            return(View(authorsManga));
        }
コード例 #2
0
        public async Task <IActionResult> Create(int authorId, [Bind("Id,MangaId,AuthorId")] AuthorsManga authorsManga)
        {
            if (ModelState.IsValid)
            {
                _context.Add(authorsManga);
                await _context.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("Index", "AuthorsMangas", new { id = authorId, name = _context.Authors.Where(c => c.Id == authorId).FirstOrDefault().Name }));
            }
            //ViewData["AuthorId"] = new SelectList(_context.Authors, "Id", "Name", authorsManga.AuthorId);
            ViewData["MangaId"] = new SelectList(_context.Mangas, "Id", "Name", authorsManga.MangaId);
            //return View(authorsManga);
            return(RedirectToAction("Index", "AuthorsMangas", new { id = authorId, name = _context.Authors.Where(c => c.Id == authorId).FirstOrDefault().Name }));
        }
コード例 #3
0
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                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)
                            {
                                //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову
                                Category newcat;
                                var      c = (from cat in _context.Categories
                                              where cat.Name.Contains(worksheet.Name)
                                              select cat).ToList();
                                if (c.Count > 0)
                                {
                                    newcat = c[0];
                                }
                                else
                                {
                                    newcat      = new Category();
                                    newcat.Name = worksheet.Name;
                                    //додати в контекст
                                    _context.Categories.Add(newcat);
                                }
                                //перегляд усіх рядків
                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try
                                    {
                                        Manga book = new Manga();
                                        book.Name     = row.Cell(1).Value.ToString();
                                        book.Info     = row.Cell(6).Value.ToString();
                                        book.Category = newcat;
                                        _context.Mangas.Add(book);
                                        //у разі наявності автора знайти його, у разі відсутності - додати
                                        for (int i = 2; i <= 5; i++)
                                        {
                                            if (row.Cell(i).Value.ToString().Length > 0)
                                            {
                                                Author author;

                                                var a = (from aut in _context.Authors
                                                         where aut.Name.Contains(row.Cell(i).Value.ToString())
                                                         select aut).ToList();
                                                if (a.Count > 0)
                                                {
                                                    author = a[0];
                                                }
                                                else
                                                {
                                                    author      = new Author();
                                                    author.Name = row.Cell(i).Value.ToString();
                                                    author.Info = "from EXCEL";
                                                    //додати в контекст
                                                    _context.Add(author);
                                                }
                                                AuthorsManga ab = new AuthorsManga();
                                                ab.Manga  = book;
                                                ab.Author = author;
                                                _context.AuthorsMangas.Add(ab);
                                            }
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        //logging самостійно :)
                                    }
                                }
                            }
                        }
                    }
                }

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