Пример #1
0
        public async Task <IActionResult> PostAsync([FromBody] SaveQbQuestionResource[] resources)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            List <SaveQbQuestionResource> resourcesList = new List <SaveQbQuestionResource>(resources);
            List <QbQuestion>             questions     = _mapper.Map <List <SaveQbQuestionResource>, List <QbQuestion> >(resourcesList);

            List <QbQuestionResource> resultsResources = new List <QbQuestionResource>();

            foreach (QbQuestion question in questions)
            {
                QbQuestionResponse result = await _qbQuestionService.SaveAsync(question);

                if (!result.Success)
                {
                    return(BadRequest(result.Message));
                }

                resultsResources.Add(_mapper.Map <QbQuestion, QbQuestionResource>(result.QbQuestion));
            }

            return(Ok(resultsResources));
        }
Пример #2
0
        public async Task <IActionResult> DeleteAsync(int id)
        {
            QbQuestionResponse result = await _qbQuestionService.DeleteAsync(id);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            QbQuestionResource questionResource = _mapper.Map <QbQuestion, QbQuestionResource>(result.QbQuestion);

            return(Ok(questionResource));
        }
        public async Task DeleteAsyncFailureTest()
        {
            // Arrange
            QbQuestionResponse response          = new QbQuestionResponse(string.Empty);
            IQbQuestionService qbQuestionService = Substitute.For <IQbQuestionService>();

            qbQuestionService.DeleteAsync(Arg.Any <int>()).Returns(response);
            QbQuestionsController controller = new QbQuestionsController(qbQuestionService, mapper);

            // Act
            IActionResult result = await controller.DeleteAsync(1);

            // Assert
            result.Should().BeOfType <BadRequestObjectResult>();
        }
        public void SaveAsyncSuccessTest()
        {
            // Arrange
            IQbQuestionRepository repository = Substitute.For <IQbQuestionRepository>();
            IUnitOfWork           unitOfWork = Substitute.For <IUnitOfWork>();

            repository.AddAsync(Arg.Any <QbQuestion>()).Returns(Task.CompletedTask);
            unitOfWork.CompleteAsync().Returns(Task.CompletedTask);
            QbQuestionService service  = new QbQuestionService(repository, unitOfWork);
            QbQuestion        question = new QbQuestion();

            // Act
            Task <QbQuestionResponse> result = service.SaveAsync(question);

            // Assert
            QbQuestionResponse response = new QbQuestionResponse(question);

            result.Result.Success.Should().Be(true);
        }
        public async Task DeleteAsyncSuccessTest()
        {
            // Arrange
            QbQuestion         question          = new QbQuestion();
            QbQuestionResponse response          = new QbQuestionResponse(question);
            IQbQuestionService qbQuestionService = Substitute.For <IQbQuestionService>();

            qbQuestionService.DeleteAsync(Arg.Any <int>()).Returns(response);
            QbQuestionResource resource = new QbQuestionResource();

            mapper.Map <QbQuestion, QbQuestionResource>(Arg.Any <QbQuestion>()).Returns(resource);
            QbQuestionsController controller = new QbQuestionsController(qbQuestionService, mapper);

            // Act
            IActionResult result = await controller.DeleteAsync(1);

            // Assert
            result.Should().BeOfType <OkObjectResult>();
        }
        public async Task UpdateAsyncFailureTest()
        {
            // Arrange
            SaveQbQuestionResource resource = new SaveQbQuestionResource();
            QbQuestion             question = new QbQuestion();

            mapper.Map <SaveQbQuestionResource, QbQuestion>(Arg.Any <SaveQbQuestionResource>()).Returns(question);
            IQbQuestionService qbQuestionService = Substitute.For <IQbQuestionService>();
            QbQuestionResponse response          = new QbQuestionResponse(string.Empty);

            qbQuestionService.UpdateAsync(Arg.Any <int>(), Arg.Any <QbQuestion>()).Returns(response);
            QbQuestionsController controller = new QbQuestionsController(qbQuestionService, mapper);

            // Act
            IActionResult result = await controller.PutAsync(1, resource);

            // Assert
            result.Should().BeOfType <BadRequestObjectResult>();
        }
Пример #7
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] SaveQbQuestionResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            QbQuestion         question = _mapper.Map <SaveQbQuestionResource, QbQuestion>(resource);
            QbQuestionResponse result   = await _qbQuestionService.UpdateAsync(id, question);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            QbQuestionResource questionResource = _mapper.Map <QbQuestion, QbQuestionResource>(result.QbQuestion);

            return(Ok(questionResource));
        }
        public async Task PostAsyncFailureTest()
        {
            // Arrange
            SaveQbQuestionResource[] resources = { new SaveQbQuestionResource() };
            List <QbQuestion>        questions = new List <QbQuestion> {
                new QbQuestion()
            };

            mapper.Map <List <SaveQbQuestionResource>, List <QbQuestion> >(Arg.Any <List <SaveQbQuestionResource> >()).Returns(questions);
            IQbQuestionService qbQuestionService = Substitute.For <IQbQuestionService>();
            QbQuestionResponse response          = new QbQuestionResponse(string.Empty);

            qbQuestionService.SaveAsync(Arg.Any <QbQuestion>()).Returns(response);
            QbQuestionsController controller = new QbQuestionsController(qbQuestionService, mapper);

            // Act
            IActionResult result = await controller.PostAsync(resources);

            // Assert
            result.Should().BeOfType <BadRequestObjectResult>();
        }