Example #1
0
        /// <exception cref="InvalidModelException"/>
        public async Task <Guid> AddAsync(RoleDto role)
        {
            var validator           = new Dtos.RoleValidator();
            ValidationResult result = validator.Validate(role);

            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 id  = Guid.NewGuid();
            var now = DateTime.UtcNow;

            var roleEntity = new Entities.Role
            {
                Id        = id,
                CreatedAt = now,
                UpdatedAt = now,
                Name      = role.Name
            };

            await _context.AddAsync(roleEntity);

            await _context.SaveChangesAsync();

            return(id);
        }
Example #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);
        }