public async Task <IActionResult> ChangeSalary(UpdateEmployeeSalaryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var salId = (await _managerService.GetEmployeeSalary(model.EmployeeEmail)).Id; var updateModel = new SalaryServiceModel { Amount = model.Amount, Currency = await _nomenclatureService.GetCurrency(model.CurrencyId), SalaryType = await _nomenclatureService.GetSalaryType(model.SalaryTypeId), Employee = await _accountManageService.GetEmployeeByEmailAsync(model.EmployeeEmail), Id = salId }; await _managerService.UpdateEmployeeSalary(updateModel); StatusMessage = $"Successfuly updated salary for employee with email {model.EmployeeEmail}"; return(RedirectToAction(nameof(ChangeSalary), routeValues: new { email = model.EmployeeEmail })); }
public async Task <IActionResult> CreateEmployee(CreateEmployeeViewModel model) { if (!ModelState.IsValid) { var selectListModels = await FillSelectLists(model.CountryId, model.CompanyId); model = _mapper.Map(selectListModels, model); return(View(model)); } //VERY SPAGHETTI //Register Identity var result = new IdentityResult(); var user = new UserServiceModel { Email = model.Email, UserName = model.Email }; result = await _accountService.CreateUserAsync(user, "P@ssw0rd"); if (model.IsManager && result.Succeeded) { user = _mapper.Map <UserServiceModel>(await _accountService.GetByIdAsync(user.Id.ToString())); result = await _adminService.SetUserAsManager(user); } //If there was an error with identity, redisplay form if (!result.Succeeded) { var selectListModels = await FillSelectLists(model.CountryId, model.CompanyId); model = _mapper.Map(selectListModels, model); model.StatusMessage = "Error, could not create employee"; return(View(model)); } //Register Employee var department = await _adminService.GetDepartmentById(model.DepartmentId); var job = await _adminService.GetJobById(model.JobId); var createModel = new EmployeeServiceModel { Department = department, Job = job, HiredDate = DateTime.UtcNow, Salary = new SalaryServiceModel { Amount = model.SalaryAmount, Currency = await _nomenclatureService.GetCurrency(model.CurrencyId), SalaryType = await _nomenclatureService.GetSalaryType(model.SalaryTypeId) }, HrId = Guid.NewGuid() }; createModel = _mapper.Map(model, createModel); createModel.Location.City = await _nomenclatureService.GetCity(model.CityId); if (!await _adminService.CreateEmployee(createModel)) { //if employee creation failed, we need to delete the user associated with it await _adminService.DeleteUser(user); var selectListModels = await FillSelectLists(model.CountryId, model.CompanyId); model = _mapper.Map(selectListModels, model); model.StatusMessage = "Error, could not create employee"; return(View(model)); } StatusMessage = "Employee Successfuly registered!"; return(RedirectToAction(nameof(Index))); }