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

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(filmStudios);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FilmStudiosExists(filmStudios.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CountriesId"] = new SelectList(_context.Countries, "Id", "Name", filmStudios.CountriesId);
            return(View(filmStudios));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,Name,CountriesId")] FilmStudios filmStudios)
        {
            int counter = 0;

            foreach (var a in _context.FilmStudios)
            {
                if (a.Name == filmStudios.Name)
                {
                    counter++; break;
                }
            }
            if (counter != 0)
            {
                ModelState.AddModelError("Name", "Така студія вже існує");
                return(View(filmStudios));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    _context.Add(filmStudios);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["CountriesId"] = new SelectList(_context.Countries, "Id", "Name", filmStudios.CountriesId);
                return(View(filmStudios));
            }
        }
コード例 #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))
                        {
                            // var lang = _context.BookGenres.Include(b => b.Book).Include(b => b.Book.Language).ToList();
                            //перегляд усіх листів (в даному випадку категорій)
                            foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову
                                Awards newaw;
                                var    c = (from aw in _context.Awards
                                            where aw.Name.Contains(worksheet.Name)
                                            select aw).ToList();
                                if (c.Count > 0)
                                {
                                    newaw = c[0];
                                }
                                else
                                {
                                    newaw      = new Awards();
                                    newaw.Name = worksheet.Name;
                                    _context.Awards.Add(newaw);
                                }
                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try {
                                        Cartoons cartoon = new Cartoons();
                                        int      counter = 0;
                                        foreach (var ca in _context.Cartoons)
                                        {
                                            if (ca.Name == row.Cell(1).Value.ToString())
                                            {
                                                counter++; cartoon = ca; break;
                                            }
                                        }
                                        if (counter > 0)
                                        {
                                            int count = 0;
                                            foreach (var award in _context.CartoonAwards)
                                            {
                                                if ((cartoon.Id == award.CartoonsId) && (newaw.Id == award.AwardsId))
                                                {
                                                    count++; break;
                                                }
                                            }
                                            if (count > 0)
                                            {
                                                goto link1;// якщо такф нагорода вже існує, переходимо до наступного рядка
                                            }
                                            else
                                            {
                                                CartoonAwards cartaw = new CartoonAwards();
                                                cartaw.Cartoons = cartoon;
                                                cartaw.Awards   = newaw;
                                                try
                                                {
                                                    cartaw.Year = Convert.ToInt32(row.Cell(2).Value);
                                                    if (cartaw.Year <= cartoon.Year)
                                                    {
                                                        goto link1;
                                                    }
                                                }
                                                catch { goto link1; }
                                                _context.CartoonAwards.Add(cartaw);
                                                goto link1;// переходимо до наступного рядка, бо вже маємо інформацію про нагороду цього мульта
                                            }
                                        }
                                        else
                                        {
                                            cartoon      = new Cartoons();
                                            cartoon.Name = row.Cell(1).Value.ToString();

                                            try
                                            {
                                                cartoon.Year = Convert.ToInt32(row.Cell(3).Value);
                                                if (cartoon.Year <= 0)
                                                {
                                                    goto link1;
                                                }
                                                cartoon.Duration = Convert.ToInt32(row.Cell(4).Value);
                                            }
                                            catch { goto link1; }
                                            FilmStudios filmstudio = new FilmStudios();
                                            counter = 0;
                                            foreach (var fst in _context.FilmStudios)
                                            {
                                                if (fst.Name == row.Cell(5).Value.ToString())
                                                {
                                                    counter++; filmstudio = fst; break;
                                                }
                                            }
                                            if (counter > 0)
                                            {
                                                cartoon.FilmStudios = filmstudio;
                                            }
                                            else
                                            {
                                                Countries country = new Countries();
                                                counter = 0;
                                                foreach (var co in _context.Countries)
                                                {
                                                    if (co.Name == row.Cell(6).Value.ToString())
                                                    {
                                                        counter++; country = co; break;
                                                    }
                                                }
                                                if (counter > 0)
                                                {
                                                    filmstudio.CountriesId = country.Id;
                                                }
                                                else
                                                {
                                                    country      = new Countries();
                                                    country.Name = row.Cell(6).Value.ToString();
                                                    _context.Countries.Add(country);
                                                    filmstudio.CountriesId = country.Id;
                                                }

                                                // BookGenres bg = new BookGenres();
                                                // cartoon.FilmStudios = filmstudio;
                                                filmstudio.Name = row.Cell(5).Value.ToString();
                                                _context.FilmStudios.Add(filmstudio);
                                            }
                                            _context.Cartoons.Add(cartoon);
                                            CartoonAwards cartaw = new CartoonAwards();
                                            cartaw.Cartoons = cartoon;
                                            cartaw.Awards   = newaw;
                                            try
                                            {
                                                cartaw.Year = Convert.ToInt32(row.Cell(2).Value);
                                                if (cartaw.Year <= cartoon.Year)
                                                {
                                                    goto link1;
                                                }
                                            }
                                            catch { goto link1; }
                                            _context.CartoonAwards.Add(cartaw);
                                        }
                                    }

                                    catch (Exception e)
                                    {
                                        throw new InvalidOperationException("Дані в файлі некоректні", e);
                                        // ModelState.AddModelError("", "Username or Password is wrong.");

                                        //logging самостійно :)
                                    }


                                    link1 :;
                                    await _context.SaveChangesAsync();
                                }
                                //Genres newgen;
                                //var c = (from gen in _context.Genres
                                //         where gen.Name.Contains(worksheet.Name)
                                //         select gen).ToList();
                                //if (c.Count > 0)
                                //{
                                //    newgen = c[0];
                                //}
                                //else
                                //{
                                //    newgen = new Genres();
                                //    newgen.Name = worksheet.Name;
                                //    _context.Genres.Add(newgen);
                                //}
                                //перегляд усіх рядків
                            }
                        }
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }