Exemplo n.º 1
0
        public async Task UpdateAsync(Dtos.User dto)
        {
            if (dto == null)
            {
                throw new NotFoundException("Target user not found");
            }

            if (dto.Id.Equals(Guid.Empty))
            {
                throw new InvalidModelException("Property Id failed validation. Error was: Id is empty");
            }

            var validator           = new Dtos.UserValidator();
            ValidationResult result = validator.Validate(dto);

            if (!result.IsValid)
            {
                string errMess = string.Empty;

                foreach (var failure in result.Errors)
                {
                    errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n";
                }

                throw new InvalidModelException(errMess);
            }

            var targetUser = await _context.Set <Entities.User>()
                             .Include(x => x.UserInfo)
                             .FirstOrDefaultAsync(x => x.Id == dto.Id);

            if (targetUser == null)
            {
                throw new NotFoundException("Target user not found");
            }

            targetUser.UserInfo.FirstName    = dto.FirstName;
            targetUser.UserInfo.LastName     = dto.LastName;
            targetUser.UserInfo.Patronymic   = dto.Patronymic;
            targetUser.UserInfo.FirstNameEng = dto.FirstNameEng;
            targetUser.UserInfo.LastNameEng  = dto.LastNameEng;
            targetUser.UserInfo.Email        = dto.Email;
            targetUser.UserInfo.PhoneNumber  = dto.PhoneNumber;
            targetUser.UserInfo.DateOfBirth  = dto.DateOfBirth;
            targetUser.UpdatedAt             = DateTime.UtcNow;

            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task <Guid> SignUpAsync(Dtos.User userDto, string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new RegistrationException("Password is required");
            }

            var validator           = new Dtos.UserValidator();
            ValidationResult result = validator.Validate(userDto);

            if (!result.IsValid)
            {
                string errMess = string.Empty;

                foreach (var failure in result.Errors)
                {
                    errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n";
                }

                throw new InvalidModelException(errMess);
            }

            bool isUserExists = _context.Set <Entities.User>().Include(x => x.UserInfo)
                                .Any(x => x.UserInfo != null && x.UserInfo.Email == userDto.Email);

            if (isUserExists)
            {
                throw new ServiceException(System.Net.HttpStatusCode.Conflict, $"Email \"{userDto.Email}\" is already taken");
            }

            CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);

            var userInfo = new Entities.UserInfo
            {
                Id           = Guid.NewGuid(),
                DateOfBirth  = userDto.DateOfBirth,
                Email        = userDto.Email,
                FirstName    = userDto.FirstName,
                FirstNameEng = userDto.FirstNameEng,
                LastName     = userDto.LastName,
                LastNameEng  = userDto.LastNameEng,
                Patronymic   = userDto.Patronymic,
                PhoneNumber  = userDto.PhoneNumber
            };
            await _context.AddAsync(userInfo);

            var user = new Entities.User
            {
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt,
                CreatedAt    = DateTime.UtcNow,
                UpdatedAt    = DateTime.UtcNow,
                Id           = Guid.NewGuid(),
                UserInfo     = userInfo
            };

            var role = await _context.Set <Entities.Role>()
                       .FirstOrDefaultAsync(x => x.Name == AppRoles.User);

            if (role == null)
            {
                role = new Entities.Role
                {
                    Id        = Guid.NewGuid(),
                    Name      = AppRoles.User,
                    CreatedAt = DateTime.UtcNow,
                    UpdatedAt = DateTime.UtcNow
                };

                await _context.Set <Entities.Role>().AddAsync(role);
            }

            var userRole = new Entities.UserRoles
            {
                RoleId = role.Id,
                UserId = user.Id
            };

            await _context.Set <Entities.User>().AddAsync(user);

            await _context.Set <Entities.UserRoles>().AddAsync(userRole);

            await _context.SaveChangesAsync();

            return(user.Id);
        }