Beispiel #1
0
        private async Task RegisterUserAsync(UserRegisterDto dto, CredentialLevel credentialLevel)
        {
            #region Validate

            if (!IsValidEmail(dto.Email))
            {
                throw HttpError.BadRequest("Invalid email address");
            }

            if (dto.Name == null)
            {
                throw HttpError.BadRequest("Username cannot be null");
            }

            if (dto.Name.Length > Consts.UsernameMaxLength)
            {
                throw HttpError.BadRequest("Username is too long");
            }

            if (dto.Description != null && dto.Description.Length > Consts.DescriptionMaxLength)
            {
                throw HttpError.BadRequest("Description is too long");
            }

            #endregion

            #region CheckIfUserExists

            if ((await _userRepository.Find(x => x.Name == dto.Name)).Any())
            {
                throw HttpError.BadRequest($"There is user with {dto.Name} username");
            }

            if ((await _userRepository.Find(x => x.Email == dto.Email)).Any())
            {
                throw HttpError.BadRequest($"There is user with {dto.Email} email address");
            }

            #endregion

            var user = new User
            {
                Id = Guid.NewGuid().ToString(),
                CredentialLevel = credentialLevel,
                Description     = dto.Description,
                Email           = dto.Email,
                Name            = dto.Name
            };
            await _userRepository.Create(user);

            await _hashRepository.CreateOrUpdate(new Hash
            {
                Id           = user.Id,
                PasswordHash = HashHelpers.CreateHash(dto.Password)
            });
        }
        public MockRepositoriesHub()
        {
            var user1 = new User
            {
                Id              = "user-1",
                Name            = "kotwica407",
                Email           = "*****@*****.**",
                CredentialLevel = CredentialLevel.User,
                Description     = "Cool user"
            };
            var user2 = new User
            {
                Id              = "user-2",
                Name            = "boring123",
                Email           = "*****@*****.**",
                CredentialLevel = CredentialLevel.User,
                Description     = "Boring user"
            };

            _users = new List <User> {
                user1, user2
            };

            var hash1 = new Hash
            {
                Id           = user1.Id,
                PasswordHash = HashHelpers.CreateHash("password1")
            };
            var hash2 = new Hash
            {
                Id           = user2.Id,
                PasswordHash = HashHelpers.CreateHash("password2")
            };

            _hashes = new List <Hash> {
                hash1, hash2
            };

            var post1 = new Post
            {
                Id       = "post-1",
                AuthorId = user1.Id,
                Title    = "Title of post no.1",
                Content  = "Content of post no.1 #tag1 #tag2 \n" +
                           "#tag3",
                CreationTime   = new DateTime(2020, 10, 1, 12, 0, 0),
                LastUpdateTime = new DateTime(2020, 10, 1, 12, 0, 0),
                Tags           = new [] { "tag1", "tag2", "tag3" }
            };
            var post2 = new Post
            {
                Id       = "post-2",
                AuthorId = user2.Id,
                Title    = "Title of post no.2",
                Content  = "Content of post no.2 #tag1 #tag2 \n" +
                           "#tag3",
                CreationTime   = new DateTime(2020, 10, 1, 12, 0, 0),
                LastUpdateTime = new DateTime(2020, 10, 1, 12, 0, 0),
                Tags           = new[] { "tag1", "tag2", "tag4" }
            };

            _posts = new List <Post> {
                post1, post2
            };
            _tags = new List <Tag>()
            {
                new Tag
                {
                    Name        = "tag1",
                    PostsNumber = 2
                },
                new Tag
                {
                    Name        = "tag2",
                    PostsNumber = 2
                },
                new Tag
                {
                    Name        = "tag3",
                    PostsNumber = 2
                },
                new Tag
                {
                    Name        = "tag4",
                    PostsNumber = 1
                }
            };

            var comment = new Comment
            {
                Id             = "comment-1",
                AuthorId       = user2.Id,
                PostId         = post1.Id,
                Content        = "Content of comment no.1 of post no.1",
                CreationTime   = new DateTime(2020, 10, 2, 10, 0, 0),
                LastUpdateTime = new DateTime(2020, 10, 2, 10, 0, 0),
            };

            _comments = new List <Comment> {
                comment
            };

            _responses     = new List <Response>();
            _postLikes     = new List <BaseLikeEntity <Context.Entities.Post> >();
            _commentLikes  = new List <BaseLikeEntity <Context.Entities.Comment> >();
            _responseLikes = new List <BaseLikeEntity <Context.Entities.Response> >();

            SetupUserRepositoryMock();
            SetupHashRepositoryMock();
            SetupPostRepositoryMock();
            SetupCommentRepositoryMock();
            SetupResponseRepositoryMock();
            SetupTagRepositoryMock();
            SetupPostLikeRepositoryMock();
            SetupCommentLikeRepositoryMock();
            SetupResponseLikeRepositoryMock();
        }