コード例 #1
0
        public async Task <IActionResult> EmergencyContact(EmergencyContactViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Countries = _mapper.Map <List <CountryViewModel> >(await _nomenclatureService.GetCountries());
                model.Cities    = _mapper.Map <List <CityViewModel> >(await _nomenclatureService.GetCitiesByCountryId(model.CountryId));
                return(View(model));
            }

            var contact = await _accountManageService.GetEmergencyContactAsync(User.Identity.Name);

            var city = await _nomenclatureService.GetCity(model.CityId);

            var serviceModel = _mapper.Map(model, contact);

            serviceModel.Location.City = city;

            if (contact != null)
            {
                await _accountManageService.UpdateEmergencyContact(serviceModel);
            }
            else
            {
                serviceModel.Employee = await _accountManageService.GetEmployeeByEmailAsync(User.Identity.Name);

                await _accountManageService.CreateEmergencyContact(serviceModel);
            }

            StatusMessage = "Your emergency contact has been updated!";

            return(RedirectToAction(nameof(EmergencyContact)));
        }
コード例 #2
0
        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)));
        }