public ActionResult EditPicture(EditProfilePicture model) { model.TeacherId = this.User.Identity.GetUserId(); if (this.ModelState.IsValid && model.Image != null) { if (IsImage(model.Image)) { var service = CreateTeacherService(); if (service.UpdateProfilePicture(model)) { TempData["SaveResult"] = "Your picture was successfully updated."; return(RedirectToAction("Detail", new { id = model.TeacherId })); } else { TempData["ErrorMessage"] = "Profile picture could not be updated."; return(RedirectToAction("Detail", new { id = model.TeacherId })); } } else { TempData["ErrorMessage"] = "Please select an image file."; return(RedirectToAction("Detail", new { id = model.TeacherId })); } } else { TempData["ErrorMessage"] = "Profile picture could not be updated."; return(RedirectToAction("Detail", new { id = model.TeacherId })); } }
public bool UpdateProfilePicture(EditProfilePicture model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx.Teachers.Find(model.TeacherId); if (entity == null) { return(false); } else { if (entity.ProfilePicture == null) { var profilePicture = _fileHelper.BuildProfilePicture(model.Image); entity.ProfilePicture = profilePicture; var numberOfChanges = ctx.SaveChanges(); return(numberOfChanges == 2); } else { var updatedPicture = _fileHelper.UpdateProfilePicture(entity.ProfilePicture, model.Image); entity.ProfilePicture = updatedPicture; var numberOfChanges = ctx.SaveChanges(); return(numberOfChanges == 1); } } } }