Пример #1
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 ApiCommentRequestModel();

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

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

            ApiCommentResponseModel getResponse = await client.CommentGetAsync(2);

            getResponse.Should().NotBeNull();

            ActionResponse deleteResult = await client.CommentDeleteAsync(2);

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

            ApiCommentResponseModel verifyResponse = await client.CommentGetAsync(2);

            verifyResponse.Should().BeNull();
        }
Пример #2
0
 public async Task <ValidationResult> ValidateUpdateAsync(int id, ApiCommentRequestModel model)
 {
     this.CreationDateRules();
     this.PostIdRules();
     this.ScoreRules();
     this.TextRules();
     this.UserIdRules();
     return(await this.ValidateAsync(model, id));
 }
Пример #3
0
        private async Task <ApiCommentResponseModel> CreateRecord(ApiClient client)
        {
            var model = new ApiCommentRequestModel();

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

            result.Success.Should().BeTrue();
            return(result.Record);
        }
Пример #4
0
        public virtual async Task <IActionResult> Create([FromBody] ApiCommentRequestModel model)
        {
            CreateResponse <ApiCommentResponseModel> result = await this.CommentService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Comments/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Пример #5
0
        public void MapModelToBO()
        {
            var mapper = new BOLCommentMapper();
            ApiCommentRequestModel model = new ApiCommentRequestModel();

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

            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.PostId.Should().Be(1);
            response.Score.Should().Be(1);
            response.Text.Should().Be("A");
            response.UserId.Should().Be(1);
        }
Пример #6
0
        public void MapResponseToRequest()
        {
            var mapper = new ApiCommentModelMapper();
            var model  = new ApiCommentResponseModel();

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

            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.PostId.Should().Be(1);
            response.Score.Should().Be(1);
            response.Text.Should().Be("A");
            response.UserId.Should().Be(1);
        }
Пример #7
0
        private async Task <ApiCommentRequestModel> PatchModel(int id, JsonPatchDocument <ApiCommentRequestModel> patch)
        {
            var record = await this.CommentService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiCommentRequestModel request = this.CommentModelMapper.MapResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Пример #8
0
        public virtual async Task <CreateResponse <ApiCommentResponseModel> > Create(
            ApiCommentRequestModel model)
        {
            CreateResponse <ApiCommentResponseModel> response = new CreateResponse <ApiCommentResponseModel>(await this.CommentModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                var bo     = this.BolCommentMapper.MapModelToBO(default(int), model);
                var record = await this.CommentRepository.Create(this.DalCommentMapper.MapBOToEF(bo));

                response.SetRecord(this.BolCommentMapper.MapBOToModel(this.DalCommentMapper.MapEFToBO(record)));
            }

            return(response);
        }
Пример #9
0
        public virtual BOComment MapModelToBO(
            int id,
            ApiCommentRequestModel model
            )
        {
            BOComment boComment = new BOComment();

            boComment.SetProperties(
                id,
                model.CreationDate,
                model.PostId,
                model.Score,
                model.Text,
                model.UserId);
            return(boComment);
        }
Пример #10
0
        public void CreatePatch()
        {
            var mapper = new ApiCommentModelMapper();
            var model  = new ApiCommentRequestModel();

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

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

            patch.ApplyTo(response);
            response.CreationDate.Should().Be(DateTime.Parse("1/1/1987 12:00:00 AM"));
            response.PostId.Should().Be(1);
            response.Score.Should().Be(1);
            response.Text.Should().Be("A");
            response.UserId.Should().Be(1);
        }
Пример #11
0
        public async void Create()
        {
            var mock  = new ServiceMockFacade <ICommentRepository>();
            var model = new ApiCommentRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Comment>())).Returns(Task.FromResult(new Comment()));
            var service = new CommentService(mock.LoggerMock.Object,
                                             mock.RepositoryMock.Object,
                                             mock.ModelValidatorMockFactory.CommentModelValidatorMock.Object,
                                             mock.BOLMapperMockFactory.BOLCommentMapperMock,
                                             mock.DALMapperMockFactory.DALCommentMapperMock);

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

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.CommentModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiCommentRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Comment>()));
        }
Пример #12
0
        public async void Delete()
        {
            var mock  = new ServiceMockFacade <ICommentRepository>();
            var model = new ApiCommentRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new CommentService(mock.LoggerMock.Object,
                                             mock.RepositoryMock.Object,
                                             mock.ModelValidatorMockFactory.CommentModelValidatorMock.Object,
                                             mock.BOLMapperMockFactory.BOLCommentMapperMock,
                                             mock.DALMapperMockFactory.DALCommentMapperMock);

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

            response.Should().NotBeNull();
            mock.RepositoryMock.Verify(x => x.Delete(It.IsAny <int>()));
            mock.ModelValidatorMockFactory.CommentModelValidatorMock.Verify(x => x.ValidateDeleteAsync(It.IsAny <int>()));
        }
Пример #13
0
        public virtual async Task <UpdateResponse <ApiCommentResponseModel> > Update(
            int id,
            ApiCommentRequestModel model)
        {
            var validationResult = await this.CommentModelValidator.ValidateUpdateAsync(id, model);

            if (validationResult.IsValid)
            {
                var bo = this.BolCommentMapper.MapModelToBO(id, model);
                await this.CommentRepository.Update(this.DalCommentMapper.MapBOToEF(bo));

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

                return(new UpdateResponse <ApiCommentResponseModel>(this.BolCommentMapper.MapBOToModel(this.DalCommentMapper.MapEFToBO(record))));
            }
            else
            {
                return(new UpdateResponse <ApiCommentResponseModel>(validationResult));
            }
        }
Пример #14
0
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiCommentRequestModel model)
        {
            ApiCommentRequestModel request = await this.PatchModel(id, this.CommentModelMapper.CreatePatch(model));

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Пример #15
0
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiCommentRequestModel> patch)
        {
            ApiCommentResponseModel record = await this.CommentService.Get(id);

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

                UpdateResponse <ApiCommentResponseModel> result = await this.CommentService.Update(id, model);

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
Пример #16
0
        public virtual async Task <UpdateResponse <ApiCommentResponseModel> > CommentUpdateAsync(int id, ApiCommentRequestModel item)
        {
            HttpResponseMessage httpResponse = await this.client.PutAsJsonAsync($"api/Comments/{id}", item).ConfigureAwait(false);

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