public async Task <EducationEditViewModel> EditDeleteForm(int id, string userName) { var educ = await this.educationRepo.All().SingleOrDefaultAsync(e => e.Id == id); var user = await this.userRepo.All().SingleOrDefaultAsync(u => u.ResumeId == educ.ResumeId); var currentUser = user.UserName; if (userName != currentUser) { return(null); } EducationEditViewModel model = null; if (educ != null) { model = new EducationEditViewModel { Id = educ.Id, Institution = educ.Institution, Diploma = educ.Diploma, Country = educ.Country, Region = educ.Region, City = educ.City, StartDate = educ.StartDate, EndDate = educ.EndDate.HasValue ? educ.EndDate.Value : (DateTime?)null, GPA = educ.GPA, MainSubjects = educ.MainSubjects, ResumeId = educ.ResumeId }; } return(model); }
public async Task Update(EducationEditViewModel model) { if (this.resumeId == null) { throw new InvalidOperationException($"Resume id is null."); } var education = new Education { ResumeId = this.resumeId.Value, Institution = this.sanitizer.Sanitize(model.Institution), StartDate = model.StartDate, EndDate = model.EndDate.HasValue ? model.EndDate.Value : (DateTime?)null, GPA = model.GPA, MainSubjects = this.sanitizer.Sanitize(model.MainSubjects), Diploma = this.sanitizer.Sanitize(model.Diploma), City = model.City, Country = model.Country, Region = model.Region, Id = model.Id }; this.educationRepo.Update(education); try { await this.educationRepo.SaveChangesAsync(); } catch (Exception e) { throw new InvalidOperationException(e.Message); } }
public async Task Delete(EducationEditViewModel model) { if (model == null) { throw new NullReferenceException("Invalid data"); } var educ = await this.educationRepo.GetByIdAsync(model.Id); try { this.educationRepo.Delete(educ); await this.educationRepo.SaveChangesAsync(); } catch (Exception e) { throw new InvalidOperationException($"Unable to delete education model {model.Id} id.", e); } }
public async Task <IActionResult> Edit(EducationEditViewModel model) { if (!ModelState.IsValid) { return(View(model)); } try { await this.educationService.Update(model); } catch (Exception e) { this.logger.LogDebug(e, $"An exception happened for user {this.userName}"); return(this.BadRequest()); } return(this.Redirect(Url.RouteUrl(new { controller = "Resume", action = "Display" }) + $"#{model.Id}")); }