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) { GameDevCompanies newCompanies; var c = (from company in _context.GameDevCompanies where company.Name.Contains(worksheet.Name) select company).ToList(); if (c.Count > 0) { newCompanies = c[0]; } else { newCompanies = new GameDevCompanies(); newCompanies.Name = worksheet.Name; newCompanies.Location = "From EXCEL"; newCompanies.DirectorFullName = "From EXCEL"; _context.GameDevCompanies.Add(newCompanies); } foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { Subsidiaries sub = new Subsidiaries(); sub.Name = row.Cell(1).Value.ToString(); sub.Location = row.Cell(2).Value.ToString(); sub.ManagerFullName = row.Cell(3).Value.ToString(); sub.Company = newCompanies; var arr = _context.Subsidiaries.Where(s => s.Name == sub.Name).ToList(); if (arr.Count() == 0) { _context.Subsidiaries.Add(sub); } } catch (Exception e) { } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create([Bind("Id,Name,Location,DirectorFullName")] GameDevCompanies gameDevCompanies) { GameDevCompanies existingCompany = await _context.GameDevCompanies.FirstOrDefaultAsync( c => c.Name == gameDevCompanies.Name && c.Location == gameDevCompanies.Location); if (existingCompany != null) { ModelState.AddModelError(string.Empty, "This company already exists."); } else if (ModelState.IsValid) { _context.Add(gameDevCompanies); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(gameDevCompanies)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Location,DirectorFullName")] GameDevCompanies gameDevCompanies) { if (id != gameDevCompanies.Id) { return(NotFound()); } GameDevCompanies existingCompany = await _context.GameDevCompanies.FirstOrDefaultAsync( c => c.Name == gameDevCompanies.Name && c.Location == gameDevCompanies.Location); if (existingCompany != null) { ModelState.AddModelError(string.Empty, "This company already exists."); } else if (ModelState.IsValid) { try { _context.Update(gameDevCompanies); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GameDevCompaniesExists(gameDevCompanies.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(gameDevCompanies)); }