Exemplo n.º 1
0
 public async Task <ValidationResult> ValidateUpdateAsync(int id, ApiCommentsRequestModel model)
 {
     this.CreationDateRules();
     this.PostIdRules();
     this.ScoreRules();
     this.TextRules();
     this.UserIdRules();
     return(await this.ValidateAsync(model, id));
 }
Exemplo n.º 2
0
        private async Task <ApiCommentsResponseModel> CreateRecord()
        {
            var model = new ApiCommentsRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1988 12:00:00 AM"), 2, 2, "B", 2);
            CreateResponse <ApiCommentsResponseModel> result = await this.Client.CommentsCreateAsync(model);

            result.Success.Should().BeTrue();
            return(result.Record);
        }
        public virtual async Task <IActionResult> Create([FromBody] ApiCommentsRequestModel model)
        {
            CreateResponse <ApiCommentsResponseModel> result = await this.CommentsService.Create(model);

            if (result.Success)
            {
                return(this.Created($"{this.Settings.ExternalBaseUrl}/api/Comments/{result.Record.Id}", result));
            }
            else
            {
                return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
            }
        }
Exemplo n.º 4
0
        public void MapResponseToRequest()
        {
            var mapper = new ApiCommentsModelMapper();
            var model  = new ApiCommentsResponseModel();

            model.SetProperties(1, DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, "A", 1);
            ApiCommentsRequestModel 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);
        }
Exemplo n.º 5
0
        public void MapModelToBO()
        {
            var mapper = new BOLCommentsMapper();
            ApiCommentsRequestModel model = new ApiCommentsRequestModel();

            model.SetProperties(DateTime.Parse("1/1/1987 12:00:00 AM"), 1, 1, "A", 1);
            BOComments 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);
        }
        private async Task <ApiCommentsRequestModel> PatchModel(int id, JsonPatchDocument <ApiCommentsRequestModel> patch)
        {
            var record = await this.CommentsService.Get(id);

            if (record == null)
            {
                return(null);
            }
            else
            {
                ApiCommentsRequestModel request = this.CommentsModelMapper.MapResponseToRequest(record);
                patch.ApplyTo(request);
                return(request);
            }
        }
Exemplo n.º 7
0
        public virtual async Task <CreateResponse <ApiCommentsResponseModel> > Create(
            ApiCommentsRequestModel model)
        {
            CreateResponse <ApiCommentsResponseModel> response = new CreateResponse <ApiCommentsResponseModel>(await this.commentsModelValidator.ValidateCreateAsync(model));

            if (response.Success)
            {
                var bo     = this.bolCommentsMapper.MapModelToBO(default(int), model);
                var record = await this.commentsRepository.Create(this.dalCommentsMapper.MapBOToEF(bo));

                response.SetRecord(this.bolCommentsMapper.MapBOToModel(this.dalCommentsMapper.MapEFToBO(record)));
            }

            return(response);
        }
        public virtual BOComments MapModelToBO(
            int id,
            ApiCommentsRequestModel model
            )
        {
            BOComments boComments = new BOComments();

            boComments.SetProperties(
                id,
                model.CreationDate,
                model.PostId,
                model.Score,
                model.Text,
                model.UserId);
            return(boComments);
        }
Exemplo n.º 9
0
        public void CreatePatch()
        {
            var mapper = new ApiCommentsModelMapper();
            var model  = new ApiCommentsRequestModel();

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

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

            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);
        }
Exemplo n.º 10
0
        public async void Create()
        {
            var mock  = new ServiceMockFacade <ICommentsRepository>();
            var model = new ApiCommentsRequestModel();

            mock.RepositoryMock.Setup(x => x.Create(It.IsAny <Comments>())).Returns(Task.FromResult(new Comments()));
            var service = new CommentsService(mock.LoggerMock.Object,
                                              mock.RepositoryMock.Object,
                                              mock.ModelValidatorMockFactory.CommentsModelValidatorMock.Object,
                                              mock.BOLMapperMockFactory.BOLCommentsMapperMock,
                                              mock.DALMapperMockFactory.DALCommentsMapperMock);

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

            response.Should().NotBeNull();
            mock.ModelValidatorMockFactory.CommentsModelValidatorMock.Verify(x => x.ValidateCreateAsync(It.IsAny <ApiCommentsRequestModel>()));
            mock.RepositoryMock.Verify(x => x.Create(It.IsAny <Comments>()));
        }
Exemplo n.º 11
0
        public async void Delete()
        {
            var mock  = new ServiceMockFacade <ICommentsRepository>();
            var model = new ApiCommentsRequestModel();

            mock.RepositoryMock.Setup(x => x.Delete(It.IsAny <int>())).Returns(Task.CompletedTask);
            var service = new CommentsService(mock.LoggerMock.Object,
                                              mock.RepositoryMock.Object,
                                              mock.ModelValidatorMockFactory.CommentsModelValidatorMock.Object,
                                              mock.BOLMapperMockFactory.BOLCommentsMapperMock,
                                              mock.DALMapperMockFactory.DALCommentsMapperMock);

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

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

            if (validationResult.IsValid)
            {
                var bo = this.bolCommentsMapper.MapModelToBO(id, model);
                await this.commentsRepository.Update(this.dalCommentsMapper.MapBOToEF(bo));

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

                return(new UpdateResponse <ApiCommentsResponseModel>(this.bolCommentsMapper.MapBOToModel(this.dalCommentsMapper.MapEFToBO(record))));
            }
            else
            {
                return(new UpdateResponse <ApiCommentsResponseModel>(validationResult));
            }
        }
        public virtual async Task <IActionResult> Update(int id, [FromBody] ApiCommentsRequestModel model)
        {
            ApiCommentsRequestModel request = await this.PatchModel(id, this.CommentsModelMapper.CreatePatch(model));

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

                if (result.Success)
                {
                    return(this.Ok(result));
                }
                else
                {
                    return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, result));
                }
            }
        }
        public virtual async Task <IActionResult> Patch(int id, [FromBody] JsonPatchDocument <ApiCommentsRequestModel> patch)
        {
            ApiCommentsResponseModel record = await this.CommentsService.Get(id);

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

                UpdateResponse <ApiCommentsResponseModel> result = await this.CommentsService.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 <ApiCommentsResponseModel> > CommentsUpdateAsync(int id, ApiCommentsRequestModel item)
        {
            HttpResponseMessage httpResponse = await this.client.PutAsJsonAsync($"api/Comments/{id}", item).ConfigureAwait(false);

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