public async Task <IActionResult> ProfileCreate(ProfileCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            Profile = new ProfilesModel()
            {
                FirstName    = model.FirstName,
                LastName     = model.LastName,
                AddressLine1 = model.AddressLine1,
                AddressLine2 = model.AddressLine2,
                City         = model.City,
                State        = model.State,
                ZipCode      = model.ZipCode,
                UserId       = user.Id,
                ProfileImage = "000",
            };

            _context.Profiles.Add(Profile);
            await _context.SaveChangesAsync();

            Profile.AccountNumber = DateTime.Now.Year.ToString() + "-" + DateTime.Now.ToString("MM") + "-" +
                                    Profile.Id.ToString().PadLeft(7, '0').
                                    Substring(Profile.Id.ToString().PadLeft(7, '0').Length - 7);

            var userFolder =
                hostingEnvironment.WebRootPath + "\\AppUserData\\" + Profile.AccountNumber + "\\";

            if (!Directory.Exists(userFolder))
            {
                Directory.CreateDirectory(userFolder);
            }

            if (this.UserImage != null)
            {
                var fileExt  = Path.GetExtension(this.UserImage.FileName);
                var fileName = "ProfileImage_001" + fileExt;
                this.UserImage.CopyTo(new FileStream(userFolder + fileName, FileMode.Create));
                Profile.ProfileImage = "001" + fileExt;
            }

            Profile.DateCreate     = DateTime.Now;
            Profile.DateLastUpdate = DateTime.Now;
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(ProfileController.ProfileEditView), "Profile", new { statusMessage = "Profile Successfully Created!" }));
        }
Exemple #2
0
        public async Task <ActionResult> CreateAsync([FromBody] ProfileCreateViewModel createModel)
        {
            var(result, id) = await profileService.CreateAsync(createModel.FirstName, createModel.LastName, createModel.Gender, createModel.DateOfBirth, createModel.City);

            if (result.Successed)
            {
                return(Created(Url.Action("GetById", new { id }), await profileService.GetByIdAsync(id)));
            }

            return(BadRequest(result.ToProblemDetails()));
        }
Exemple #3
0
        public IActionResult Create()
        {
            var employees = employeeService.GetEmployees();
            var model     = new ProfileCreateViewModel()
            {
                Employees = employees
                            .Where(e => e.Profile == null &&
                                   (!e.DateRelieved.HasValue || e.DateRelieved.Value.Date > DateTime.Now.Date))
                            .Select(e => new SelectListItem()
                {
                    Text  = e.ToString(),
                    Value = e.Id.ToString()
                })
                            .OrderBy(i => i.Text)
            };

            return(View(model));
        }
Exemple #4
0
        public async Task <IActionResult> Create(ProfileCreateViewModel model)
        {
            var employees = employeeService.GetEmployees();

            model.Employees = employees
                              .Where(e => e.Profile == null &&
                                     (!e.DateRelieved.HasValue || e.DateRelieved.Value.Date > DateTime.Now.Date))
                              .Select(e => new SelectListItem()
            {
                Text  = e.ToString(),
                Value = e.Id.ToString()
            }).OrderBy(i => i.Text);
            if (await profileService.FindByNameAsync(model.Alias) != null)
            {
                ViewData[ErrorKey] = $"Alias '{model.Alias}' is already in use.";
                return(View(model));
            }
            if (model.DateRegistered.Date < DateTimeOffset.Now.Date)
            {
                ViewData[ErrorKey] = string.Format(DateInPastErrorMessage, "Activation date");
                return(View(model));
            }
            User profile = mapper.Map <User>(model);

            profile.EmailConfirmed = true;
            var profileCreateResult = await profileService.CreateAsync(profile, model.Password);

            if (!profileCreateResult.Succeeded)
            {
                ViewData[ErrorKey] = profileCreateResult.Errors.Select(e => e.Description);
                return(View(model));
            }
            var typeClaim = new Claim(TypeKey, nameof(Employee));
            await profileService.AddClaimAsync(profile, typeClaim);

            if (!string.IsNullOrWhiteSpace(model.EmployeeId))
            {
                var employee = await employeeService.FindByIdAsync(model.EmployeeId);

                if (employee == null)
                {
                    ViewData[ErrorKey] = string.Format(InvalidObjectErrorMessage, nameof(Employee));
                    return(View(model));
                }
                profile.Employee = employee;
                var departmentClaim = new Claim(DepartmentKey, employee.Department.ToString());
                await profileService.AddClaimAsync(profile, departmentClaim);

                var roleAssignResult = await profileService.AddToRoleAsync(profile, employee.Position.Name);

                if (!roleAssignResult.Succeeded)
                {
                    ViewData[ErrorKey] = roleAssignResult.Errors.Select(e => e.Description);
                    return(View(model));
                }
                var roleClaim = new Claim(RoleKey, employee.Position.Name);
                await profileService.AddClaimAsync(profile, roleClaim);
            }
            var infoMessages = new string[0];

            infoMessages = infoMessages.Append($"Profile '{profile.UserName}' created successfully.").ToArray();
            if (profile.Employee != null)
            {
                infoMessages = infoMessages
                               .Append($"Profile '{profile.UserName}' assigned to employee {profile.Employee.ToString()}.")
                               .ToArray();
            }
            ViewData[InfoKey] = infoMessages;
            logger.LogInformation($"'{User.Identity.Name}' created profile '{profile.UserName}'.");
            await employeeService.UpdateLastWorkedAsync(User.GetId());

            return(View(model));
        }