Exemplo n.º 1
0
        public void MapModelToBO()
        {
            var mapper = new BOLPostMapper();
            ApiPostRequestModel model = new ApiPostRequestModel();

            model.SetProperties(1, 1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), 1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1, 1, 1, 1, 1, "A", "A", 1);
            BOPost response = mapper.MapModelToBO(1, model);

            response.AcceptedAnswerId.Should().Be(1);
            response.AnswerCount.Should().Be(1);
            response.Body.Should().Be("A");
            response.ClosedDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.CommentCount.Should().Be(1);
            response.CommunityOwnedDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.FavoriteCount.Should().Be(1);
            response.LastActivityDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.LastEditDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.LastEditorDisplayName.Should().Be("A");
            response.LastEditorUserId.Should().Be(1);
            response.OwnerUserId.Should().Be(1);
            response.ParentId.Should().Be(1);
            response.PostTypeId.Should().Be(1);
            response.Score.Should().Be(1);
            response.Tag.Should().Be("A");
            response.Title.Should().Be("A");
            response.ViewCount.Should().Be(1);
        }
Exemplo n.º 2
0
        public void CreatePatch()
        {
            var mapper = new ApiPostModelMapper();
            var model  = new ApiPostRequestModel();

            model.SetProperties(1, 1, "A", DateTime.Parse("1/1/1987 12:00:00 AM"), 1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), 1, DateTime.Parse("1/1/1987 12:00:00 AM"), DateTime.Parse("1/1/1987 12:00:00 AM"), "A", 1, 1, 1, 1, 1, "A", "A", 1);

            JsonPatchDocument <ApiPostRequestModel> patch = mapper.CreatePatch(model);
            var response = new ApiPostRequestModel();

            patch.ApplyTo(response);
            response.AcceptedAnswerId.Should().Be(1);
            response.AnswerCount.Should().Be(1);
            response.Body.Should().Be("A");
            response.ClosedDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.CommentCount.Should().Be(1);
            response.CommunityOwnedDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.FavoriteCount.Should().Be(1);
            response.LastActivityDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.LastEditDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.LastEditorDisplayName.Should().Be("A");
            response.LastEditorUserId.Should().Be(1);
            response.OwnerUserId.Should().Be(1);
            response.ParentId.Should().Be(1);
            response.PostTypeId.Should().Be(1);
            response.Score.Should().Be(1);
            response.Tag.Should().Be("A");
            response.Title.Should().Be("A");
            response.ViewCount.Should().Be(1);
        }
Exemplo n.º 3
0
        public virtual BOPost MapModelToBO(
            int id,
            ApiPostRequestModel model
            )
        {
            BOPost boPost = new BOPost();

            boPost.SetProperties(
                id,
                model.AcceptedAnswerId,
                model.AnswerCount,
                model.Body,
                model.ClosedDate,
                model.CommentCount,
                model.CommunityOwnedDate,
                model.CreationDate,
                model.FavoriteCount,
                model.LastActivityDate,
                model.LastEditDate,
                model.LastEditorDisplayName,
                model.LastEditorUserId,
                model.OwnerUserId,
                model.ParentId,
                model.PostTypeId,
                model.Score,
                model.Tag,
                model.Title,
                model.ViewCount);
            return(boPost);
        }
Exemplo n.º 4
0
        public async void TestDelete()
        {
            var builder = new WebHostBuilder()
                          .UseEnvironment("Production")
                          .UseStartup <TestStartup>();
            TestServer testServer = new TestServer(builder);

            var client = new ApiClient(testServer.CreateClient());

            var createModel = new ApiPostRequestModel();

            createModel.SetProperties(2, 2, "B", DateTime.Parse("1/1/1988 12:00:00 AM"), 2, DateTime.Parse("1/1/1988 12:00:00 AM"), DateTime.Parse("1/1/1988 12:00:00 AM"), 2, DateTime.Parse("1/1/1988 12:00:00 AM"), DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 2, 2, 2, 2, 2, "B", "B", 2);
            CreateResponse <ApiPostResponseModel> createResult = await client.PostCreateAsync(createModel);

            createResult.Success.Should().BeTrue();

            ApiPostResponseModel getResponse = await client.PostGetAsync(2);

            getResponse.Should().NotBeNull();

            ActionResponse deleteResult = await client.PostDeleteAsync(2);

            deleteResult.Success.Should().BeTrue();

            ApiPostResponseModel verifyResponse = await client.PostGetAsync(2);

            verifyResponse.Should().BeNull();
        }
Exemplo n.º 5
0
        private async Task <ApiPostResponseModel> CreateRecord(ApiClient client)
        {
            var model = new ApiPostRequestModel();

            model.SetProperties(2, 2, "B", DateTime.Parse("1/1/1988 12:00:00 AM"), 2, DateTime.Parse("1/1/1988 12:00:00 AM"), DateTime.Parse("1/1/1988 12:00:00 AM"), 2, DateTime.Parse("1/1/1988 12:00:00 AM"), DateTime.Parse("1/1/1988 12:00:00 AM"), "B", 2, 2, 2, 2, 2, "B", "B", 2);
            CreateResponse <ApiPostResponseModel> result = await client.PostCreateAsync(model);

            result.Success.Should().BeTrue();
            return(result.Record);
        }
Exemplo n.º 6
0
        public virtual async Task <IActionResult> Create([FromBody] ApiPostRequestModel model)
        {
            CreateResponse <ApiPostResponseModel> result = await this.PostService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Posts/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Exemplo n.º 7
0
        public virtual async Task <CreateResponse <ApiPostResponseModel> > Create(
            ApiPostRequestModel model)
        {
            CreateResponse <ApiPostResponseModel> response = new CreateResponse <ApiPostResponseModel>(await this.PostModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                var bo     = this.BolPostMapper.MapModelToBO(default(int), model);
                var record = await this.PostRepository.Create(this.DalPostMapper.MapBOToEF(bo));

                response.SetRecord(this.BolPostMapper.MapBOToModel(this.DalPostMapper.MapEFToBO(record)));
            }

            return(response);
        }
Exemplo n.º 8
0
        private async Task <ApiPostRequestModel> PatchModel(int id, JsonPatchDocument <ApiPostRequestModel> patch)
        {
            var record = await this.PostService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiPostRequestModel request = this.PostModelMapper.MapResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Exemplo n.º 9
0
        public async void Create()
        {
            var mock  = new ServiceMockFacade <IPostRepository>();
            var model = new ApiPostRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Post>())).Returns(Task.FromResult(new Post()));
            var service = new PostService(mock.LoggerMock.Object,
                                          mock.RepositoryMock.Object,
                                          mock.ModelValidatorMockFactory.PostModelValidatorMock.Object,
                                          mock.BOLMapperMockFactory.BOLPostMapperMock,
                                          mock.DALMapperMockFactory.DALPostMapperMock);

            CreateResponse <ApiPostResponseModel> response = await service.Create(model);

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.PostModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiPostRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Post>()));
        }
Exemplo n.º 10
0
        public async void Delete()
        {
            var mock  = new ServiceMockFacade <IPostRepository>();
            var model = new ApiPostRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new PostService(mock.LoggerMock.Object,
                                          mock.RepositoryMock.Object,
                                          mock.ModelValidatorMockFactory.PostModelValidatorMock.Object,
                                          mock.BOLMapperMockFactory.BOLPostMapperMock,
                                          mock.DALMapperMockFactory.DALPostMapperMock);

            ActionResponse response = await service.Delete(default(int));

            response.Should().NotBeNull();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>()));
            mock.ModelValidatorMockFactory.PostModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
        }
Exemplo n.º 11
0
        public virtual async Task <UpdateResponse <ApiPostResponseModel> > Update(
            int id,
            ApiPostRequestModel model)
        {
            var validationResult = await this.PostModelValidator.ValidateUpdateAsync(id, model);

            if (validationResult.IsValid)
            {
                var bo = this.BolPostMapper.MapModelToBO(id, model);
                await this.PostRepository.Update(this.DalPostMapper.MapBOToEF(bo));

                var record = await this.PostRepository.Get(id);

                return(new UpdateResponse <ApiPostResponseModel>(this.BolPostMapper.MapBOToModel(this.DalPostMapper.MapEFToBO(record))));
            }
            else
            {
                return(new UpdateResponse <ApiPostResponseModel>(validationResult));
            }
        }
Exemplo n.º 12
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiPostRequestModel model)
        {
            ApiPostRequestModel request = await this.PatchModel(id, this.PostModelMapper.CreatePatch(model));

            if (request == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                UpdateResponse <ApiPostResponseModel> result = await this.PostService.Update(id, request);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Exemplo n.º 13
0
 public async Task <ValidationResult> ValidateUpdateAsync(int id, ApiPostRequestModel model)
 {
     this.AcceptedAnswerIdRules();
     this.AnswerCountRules();
     this.BodyRules();
     this.ClosedDateRules();
     this.CommentCountRules();
     this.CommunityOwnedDateRules();
     this.CreationDateRules();
     this.FavoriteCountRules();
     this.LastActivityDateRules();
     this.LastEditDateRules();
     this.LastEditorDisplayNameRules();
     this.LastEditorUserIdRules();
     this.OwnerUserIdRules();
     this.ParentIdRules();
     this.PostTypeIdRules();
     this.ScoreRules();
     this.TagRules();
     this.TitleRules();
     this.ViewCountRules();
     return(await this.ValidateAsync(model, id));
 }
Exemplo n.º 14
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiPostRequestModel> patch)
        {
            ApiPostResponseModel record = await this.PostService.Get(id);

            if (record == null)
            {
                return(this.StatusCode(StatusCodes.Status404NotFound));
            }
            else
            {
                ApiPostRequestModel model = await this.PatchModel(id, patch);

                UpdateResponse <ApiPostResponseModel> result = await this.PostService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Exemplo n.º 15
0
        public virtual async Task <UpdateResponse <ApiPostResponseModel> > PostUpdateAsync(int id, ApiPostRequestModel item)
        {
            HttpResponseMessage httpResponse = await this.client.PutAsJsonAsync($"api/Posts/{id}", item).ConfigureAwait(false);

            return(JsonConvert.DeserializeObject <UpdateResponse <ApiPostResponseModel> >(httpResponse.Content.ContentToString()));
        }