public async Task <IActionResult> Create(int?id, [Bind("ProblemId,ThemeId")] ProblemTheme problemTheme) { if (id == null) { return(NotFound()); } int ptId = 0; if (_context.ProblemTheme.Count() != 0) { ptId = _context.ProblemTheme.Max(p => p.Id) + 1; } problemTheme.Id = ptId; problemTheme.ProblemId = id; problemTheme.Problem = _context.Problem.Where(p => p.Id == id).FirstOrDefault(); problemTheme.Theme = _context.Theme.Where(t => t.Id == problemTheme.ThemeId).FirstOrDefault(); if (ModelState.IsValid) { _context.Add(problemTheme); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ProblemThemes", new { id = problemTheme.ProblemId })); } //ViewData["ProblemId"] = new SelectList(_context.Problem, "Id", "Id", problemTheme.ProblemId); //ViewData["ThemeId"] = new SelectList(_context.Theme, "Id", "ThemeName", problemTheme.ThemeId); return(RedirectToAction("Index", "ProblemThemes", new { id = problemTheme.ProblemId })); }
public async Task <IActionResult> Edit(int?id, [Bind("Id,ProblemId,ThemeId")] ProblemTheme problemTheme) { if (id != problemTheme.ProblemId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(problemTheme); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProblemThemeExists(problemTheme.ProblemId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ProblemId"] = new SelectList(_context.Problem, "Id", "Id", problemTheme.ProblemId); ViewData["ThemeId"] = new SelectList(_context.Theme, "Id", "ThemeName", problemTheme.ThemeId); return(View(problemTheme)); }
public async Task <IActionResult> DeleteConfirmed(int?id) { ProblemTheme p = _context.ProblemTheme.Where(p => p.Id == id).FirstOrDefault(); int? pId = p.ProblemId; int? tId = p.ThemeId; var problemTheme = await _context.ProblemTheme.FindAsync(pId, tId); _context.ProblemTheme.Remove(problemTheme); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Problems", new { id = pId })); }
public async Task <IActionResult> Import(IFormFile fileExcel) { if (ModelState.IsValid) { if (fileExcel != null) { using (var stream = new System.IO.FileStream(fileExcel.FileName, FileMode.Create)) { await fileExcel.CopyToAsync(stream); using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled)) { foreach (IXLWorksheet worksheet in workBook.Worksheets) { /*Theme newTheme; * var t = _context.Theme.Where(p => p.ThemeName == worksheet.Name).ToList(); * //var t = (from theme in _context.Theme * // where theme.ThemeName == worksheet.Name * // select theme).ToList(); * if(t.Count > 0) * { * newTheme = t[0]; * } * else * { * newTheme = new Theme(); * newTheme.ThemeName = worksheet.Name; * _context.Theme.Add(newTheme); * }*/ Theme newTheme = new Theme(); newTheme.ThemeName = worksheet.Name; foreach (var t in _context.Theme) { if (t.ThemeName == newTheme.ThemeName) { break; } else { _context.Theme.Add(newTheme); } } foreach (IXLRow row in worksheet.RowsUsed().Skip(1)) { try { Problem problem = new Problem(); problem.Statement = row.Cell(1).Value.ToString(); problem.Solution = row.Cell(2).Value.ToString(); problem.LevelId = (int)_context.Level.Where(p => p.Name == row.Cell(3).Value.ToString()).FirstOrDefault().Id; problem.SourceId = (int)_context.Source.Where(p => p.SourceName == row.Cell(4).Value.ToString()).FirstOrDefault().Id; ProblemGrade pg = new ProblemGrade(); pg.ProblemId = problem.Id; pg.GradeId = (int)_context.Grade.Where(g => g.GradeName == row.Cell(5).Value.ToString()).FirstOrDefault().Id; _context.ProblemGrade.Add(pg); ProblemTheme pt = new ProblemTheme(); pt.ProblemId = problem.Id; pt.ThemeId = (int)_context.Theme.Where(g => g.ThemeName == newTheme.ThemeName).FirstOrDefault().Id; _context.ProblemTheme.Add(pt); } catch (Exception e) { return(RedirectToAction(nameof(Index))); } } } } } } await _context.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); }