public async Task <IActionResult> Create( DepartmentAddEditDto department, [FromServices] IDepartmentService service) { if (ModelState.IsValid) { try { await service.CreateDepartmentAndSaveAsync(department); TempData["Message"] = Constants.SUCCESS_MESSAGE; return(RedirectToAction(nameof(Index))); } catch (Exception ex) { ViewBag.HasError = true; ViewBag.Message = ex.Message; } } await PopulateInstructorsDropDownListAsync(service, department.InstructorId); return(View($"{_viewFolder}Create.cshtml", department)); }
public async Task <int> CreateDepartmentAndSaveAsync(DepartmentAddEditDto dto) { int result; try { _context.Add(new Department { DepartmentId = dto.DepartmentId, Budget = dto.Budget, InstructorId = dto.InstructorId, Name = dto.Name, StartDate = dto.StartDate, RowVersion = dto.RowVersion }); result = await _context.SaveChangesAsync(); } catch (Exception ex) { _logger.LogError(ex, $"Failed to create new Department: {dto}"); throw ex; } return(result); }
public async Task <IActionResult> Edit( int?id, byte[] rowVersion, DepartmentAddEditDto department, [FromServices] IDepartmentService service) { if (!id.HasValue || id.Value != department.DepartmentId) { return(NotFound()); } if (ModelState.IsValid) { try { await service.UpdateDepartmentAndSaveAsync(department, rowVersion); TempData["Message"] = Constants.SUCCESS_MESSAGE; return(RedirectToAction(nameof(Index))); } catch (GeneralException ex) { ViewBag.HasError = true; ViewBag.Message = ex.Message; ModelState.Remove("RowVersion"); } catch (Exception ex) { ViewBag.HasError = true; ViewBag.Message = Constants.ERROR_MESSAGE_STANDARD + ": " + ex.Message; ModelState.Remove("RowVersion"); } } await PopulateInstructorsDropDownListAsync(service, department.InstructorId); return(View($"{_viewFolder}Edit.cshtml", department)); }
public async Task <int> UpdateDepartmentAndSaveAsync(DepartmentAddEditDto dto, byte[] rowVersion) { var result = 0; try { var departmentToUpdate = await _context.Departments .FirstOrDefaultAsync(m => m.DepartmentId == dto.DepartmentId); if (departmentToUpdate == null) { var errMsg = $"Record does not exist or has been deleted for DepartmentId={dto.DepartmentId}"; _logger.LogError("", errMsg); throw new GeneralException(errMsg); } _context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = rowVersion; departmentToUpdate.Budget = dto.Budget; departmentToUpdate.InstructorId = dto.InstructorId; departmentToUpdate.Name = dto.Name; departmentToUpdate.StartDate = dto.StartDate; // Comment out this line to enable updating only the fields with changed values //_context.Departments.Update(courseInDb); result = await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var userValues = (Department)exceptionEntry.Entity; var dbEntry = exceptionEntry.GetDatabaseValues(); var errMsg = ""; if (dbEntry == null) { errMsg = "Unable to save changes. The department was deleted by another user."; throw new GeneralException(errMsg); } else { var dbValues = (Department)dbEntry.ToObject(); if (dbValues.Name != userValues.Name) { errMsg += $"<li>Conflict field \"Name\": New value is \"{dbValues.Name}\"</li>"; } if (dbValues.Budget != userValues.Budget) { errMsg += $"<li>Conflict field \"Budget\": New value is \"{dbValues.Budget:c}\"</li>"; } if (dbValues.StartDate != userValues.StartDate) { errMsg += $"<li>Conflict field \"StartDate\": New value is \"{dbValues.StartDate:d}\"</li>"; } if (dbValues.InstructorId != userValues.InstructorId) { Instructor dbInstructor = await _context.Instructors .FirstOrDefaultAsync(i => i.InstructorId == dbValues.InstructorId); errMsg += $"<li>Conflict field \"Administrator\": New value is \"{dbInstructor?.FullName}\"</li>"; } errMsg = "The record you attempted to edit " + "was modified by another user after you got the original value." + "<br><br>The edit operation was canceled and the new values in the database displayed as below:" + "<ul>" + errMsg + "</ul>" + "<br>If you still intend to edit this record, click " + "the \"Save\" button again. Otherwise click the \"Back to List\"."; dto.RowVersion = (byte[])dbValues.RowVersion; throw new GeneralException(errMsg); } } catch (Exception ex) { _logger.LogError(ex, $"Failed to update Department: {dto}"); throw ex; } return(result); }