public ActionResult Edit(StudentDetailsBindingModel model) { Log.Info($"{CONTROLLER}{EDIT_ACTION}. Fields to be edited:Id: {model.StudentId}, First name: {model.FirstName}, Last name: {model.LastName}"); try { if (!this.ModelState.IsValid) { Log.Error($"{CONTROLLER}{EDIT_ACTION}. Invalid Model Id: {model.StudentId}, First name: {model.FirstName}, Last name: {model.LastName}"); TempData.AddErrorMessage("Please, fill in information correctly"); return(RedirectToAction("Details", new { id = model.StudentId })); } var isEdited = this.studentsService.EditStudentDetails(model); if (isEdited) { Log.Info($"Sucessfully edited id {model.StudentId}"); TempData.AddSuccessMessage("Name/s information was successfully edited"); } else { Log.Error($"NOT Sucessfully edited id {model.StudentId}"); TempData.AddErrorMessage("Name/s information was not changed"); } return(RedirectToAction("Details", new { id = model.StudentId })); } catch (System.Exception ex) { Log.Error($"{CONTROLLER}{EDIT_ACTION}. Exceprion thrown.", ex); return(View("Error")); } }
public bool EditStudentDetails(StudentDetailsBindingModel student) { if (student == null) { Log.Error($"Inpput student model was null"); return(false); } Log.Info($"{SERVICE} {EDIT_DETAILS}. To edit studentId {student.StudentId}"); //var service = this.db.Connect(); try { using (ServiceContext context = new ServiceContext(service)) { Log.Info($"{SERVICE} {EDIT_DETAILS}. Request to CRM studentId {student.StudentId}"); var retrived = (from x in context.AccountSet where x.New_UserID.Equals(student.StudentId) select x) .FirstOrDefault(); if (retrived == null) { Log.Error($"{SERVICE} {EDIT_DETAILS}. StudentId {student.StudentId} was not found."); return(false); } bool attribuTeWasChanged = false; if (retrived.New_FirstName != student.FirstName) { Log.Info($"{SERVICE} {EDIT_DETAILS}. StudentId {student.StudentId} first name will be changed to {student.FirstName}"); retrived.New_FirstName = student.FirstName; attribuTeWasChanged = true; } if (retrived.New_FamilyName != student.LastName) { Log.Info($"{SERVICE} {EDIT_DETAILS}. StudentId {student.StudentId} last name will be changed to {student.LastName}"); retrived.New_FamilyName = student.LastName; attribuTeWasChanged = true; } if (attribuTeWasChanged) { context.UpdateObject(retrived); context.SaveChanges(); Log.Info($"{SERVICE} {EDIT_DETAILS}. StudentId {student.StudentId} changes were saved"); } else { Log.Info($"{SERVICE} {EDIT_DETAILS}. StudentId {student.StudentId} has no fileds that are to be changed"); } } return(true); } catch (Exception ex) { //TODO LOG Log.Error($"Error during Edit studentId {student.StudentId}", ex); return(false); } }