Ejemplo n.º 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();
        }
Ejemplo n.º 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));
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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>()));
        }
Ejemplo n.º 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>()));
        }
Ejemplo n.º 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));
            }
        }
Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 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()));
        }