/// <summary> /// Creates user, if it does not exist. /// Adds a role to the created user and creates user profile. /// </summary> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="AccountException"></exception> public async Task Create(UserDTO userDto) { if (userDto == null) { throw new ArgumentNullException(nameof(userDto)); } ApplicationUser user = await identityUnitOfWork.UserManager.FindByEmailAsync(userDto.Email); if (user == null) { user = new ApplicationUser { Email = userDto.Email, UserName = userDto.UserName }; await identityUnitOfWork.UserManager.CreateAsync(user, userDto.Password); // add role await identityUnitOfWork.UserManager.AddToRoleAsync(user.Id, userDto.Role); // create user profile UserProfileEntity userProfile = new UserProfileEntity { Id = user.Id }; employeeUnitOfWork.Profile.Insert(userProfile); await identityUnitOfWork.SaveAsync(); await employeeUnitOfWork.SaveAsync(); } else { throw new AccountException("User with such email already exists."); } }
/// <summary> /// Updates user skill level in the db if user skill exists, otherwise inserts user skill. /// </summary> /// <exception cref="ArgumentNullException"></exception> public async Task SaveSkillLevel(EmployeeSkillDTO employeeSkillDto, string userId) { if (employeeSkillDto == null) { throw new ArgumentNullException(nameof(employeeSkillDto)); } if (String.IsNullOrEmpty(userId)) { throw new ArgumentNullException(nameof(userId)); } UserProfileSkillsEntity skill = await employeeUnitOfWork.UserProfileSkills.GetByID(employeeSkillDto.SkillId, userId); if (skill != null) { skill.SkillLevel = (int)employeeSkillDto.Level; } else { skill = new UserProfileSkillsEntity { DomainId = employeeSkillDto.DomainId, SkillId = employeeSkillDto.SkillId, SkillLevel = (int)employeeSkillDto.Level, UserProfileID = userId }; employeeUnitOfWork.UserProfileSkills.Insert(skill); } await employeeUnitOfWork.SaveAsync(); }