예제 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,CartoonsId,AwardsId,Year")] CartoonAwards cartoonAwards)
        {
            if (id != cartoonAwards.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(cartoonAwards);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartoonAwardsExists(cartoonAwards.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AwardsId"]   = new SelectList(_context.Awards, "Id", "Name", cartoonAwards.AwardsId);
            ViewData["CartoonsId"] = new SelectList(_context.Cartoons, "Id", "Name", cartoonAwards.CartoonsId);
            return(View(cartoonAwards));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("CartoonsId,AwardsId,Year")] CartoonAwards cartoonAwards)
        {
            // var cartaw = await _context.Cartoons.Include(c => c.Year).FirstOrDefaultAsync();
            var cartoon = (from c in _context.Cartoons
                           where c.Id == cartoonAwards.CartoonsId
                           select c).FirstOrDefault();

            if (cartoon.Year > cartoonAwards.Year)
            {
                ModelState.AddModelError("Year", "Некоректне значення");
                return(View(cartoonAwards));
            }
            else
            {
                if (ModelState.IsValid && cartoonAwards.Year > 0)
                {
                    _context.Add(cartoonAwards);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                ViewData["AwardsId"]   = new SelectList(_context.Awards, "Id", "Name", cartoonAwards.AwardsId);
                ViewData["CartoonsId"] = new SelectList(_context.Cartoons, "Id", "Name", cartoonAwards.CartoonsId);

                return(View(cartoonAwards));
            }
        }
예제 #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)));
        }