public async Task <IActionResult> OnPostAsync(InputModel input, string[] inputs) { await TimeStamper.TimeStampAsync(_context, HttpContext); if (!ModelState.IsValid) { return(Page()); } var gender = await _context.Genders.Where(gender => gender.Name == input.Gender).FirstOrDefaultAsync(); var state = await _context.States.Where(state => state.Name == input.State).FirstOrDefaultAsync(); var newEmployee = new Employee() { Age = input.Age, Name = input.Name, Gender = gender, State = state, Surname = input.Surname }; if (inputs != null && inputs.Length > 0) { var languages = await _context.ProgrammingLanguages.Where(lang => inputs.Contains(lang.Name)).ToListAsync(); var employeeExperience = new List <EmployeeExperience>(); foreach (var lang in languages) { employeeExperience.Add(new EmployeeExperience() { ProgrammingLanguage = lang, Employee = newEmployee }); } await _context.AddAsync <Employee>(newEmployee); await _context.AddRangeAsync(employeeExperience); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } await _context.AddRangeAsync(newEmployee); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { await TimeStamper.TimeStampAsync(_context, HttpContext); if (id == null) { return(NotFound()); } Employee = await _context.Employees.FindAsync(id); if (Employee != null) { _context.Employees.Remove(Employee); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(InputModel input, string[] inputs) { await TimeStamper.TimeStampAsync(_context, HttpContext); if (!ModelState.IsValid) { return(Page()); } var editingEmployee = await _context.Employees .Include(prop => prop.Gender) .Include(prop => prop.State) .Include(prop => prop.EmployeesExperience) .ThenInclude(prop => prop.ProgrammingLanguage) .Where(empl => empl.Id == input.Id).FirstOrDefaultAsync(); var currentProgrammingLngs = editingEmployee.EmployeesExperience.Select(prop => prop.ProgrammingLanguage).ToList(); if (ExperienceIsChanged(currentProgrammingLngs.Select(pl => pl.Name).ToList(), inputs)) { var currEmployeeExp = editingEmployee.EmployeesExperience; _context.UsersExperience.RemoveRange(currEmployeeExp); await _context.SaveChangesAsync(); var newEmployeeExperience = new List <EmployeeExperience>(); var languages = await _context.ProgrammingLanguages.ToListAsync(); languages = languages.Where(lang => inputs.Contains(lang.Name)).ToList(); foreach (var lang in languages) { newEmployeeExperience.Add(new EmployeeExperience() { Employee = editingEmployee, ProgrammingLanguage = lang }); } _context.UsersExperience.UpdateRange(newEmployeeExperience); await _context.SaveChangesAsync(); } if (editingEmployee.Gender.Name != input.Gender) { var updatedGender = await _context.Genders.Where(gender => gender.Name == input.Gender).FirstOrDefaultAsync(); editingEmployee.Gender = updatedGender; } if (editingEmployee.State.Name != input.State) { var updatedState = await _context.States.Where(state => state.Name == input.State).FirstOrDefaultAsync(); editingEmployee.State = updatedState; } editingEmployee.Age = input.Age; editingEmployee.Name = input.Name; editingEmployee.Surname = input.Surname; _context.Employees.Update(editingEmployee); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { return(RedirectToPage("./Index")); } return(RedirectToPage("./Index")); }