コード例 #1
0
        public async Task <IActionResult> Modify(int id, [FromBody] NoveltyToAddDto dto, CancellationToken cancellationToken)
        {
            var noveltyToAdd = new NoveltyToAdd(dto.Name, dto.Description, id);
            var result       = await _repository.TryUpdate(noveltyToAdd, cancellationToken);

            if (!result.Succeeded)
            {
                _logger.LogError($"Failed to modify novelty with id: {id}. Reason: {result.FailureReason}.");
                return(BadRequest("Novelty modification was unsuccessful."));
            }

            return(Ok(result.AddedItem));
        }
コード例 #2
0
        public async Task <IActionResult> Add([FromBody] NoveltyToAddDto dto, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(dto.Name) || string.IsNullOrEmpty(dto.Description))
            {
                return(BadRequest("Cannot create novelty without name or description!"));
            }

            var noveltyToAdd = new NoveltyToAdd(dto.Name, dto.Description);
            var result       = await _repository.TryAdd(noveltyToAdd, cancellationToken);

            if (!result.Succeeded)
            {
                _logger.LogError($"Failed to create novelty. Reason: {result.FailureReason}.");
                return(BadRequest("Novelty creation was unsuccessful."));
            }

            return(Created($"novelty/{result.AddedItem.ID}", new object()));
        }
コード例 #3
0
        public async Task Modify_ShouldReturnBadRequest_IfRepositoryReturnsFailure()
        {
            // Arrange
            var repositoryReturn = Task.FromResult(AddOrUpdateResult <INovelty> .Failure());
            var repository       = Substitute.For <INoveltyRepository>();

            repository.TryUpdate(Arg.Is <NoveltyToAdd>(x => x.Name.Equals("name", StringComparison.Ordinal) && x.Description.Equals("des", StringComparison.Ordinal) && x.ID == 3), CancellationToken.None).Returns(repositoryReturn);
            var controller = new NoveltyController(_logger, repository);
            var dto        = new NoveltyToAddDto {
                Name = "name", Description = "des"
            };

            // Act
            var result = await controller.Modify(3, dto, CancellationToken.None);

            // Assert
            Assert.That(result, Is.InstanceOf <BadRequestObjectResult>());
        }
コード例 #4
0
        public async Task Add_ShouldReturnCreated_IfRepositoryReturnsEntity()
        {
            // Arrange
            var novelty          = Substitute.For <INovelty>();
            var repositoryReturn = Task.FromResult(AddOrUpdateResult <INovelty> .Success(novelty));
            var repository       = Substitute.For <INoveltyRepository>();

            repository.TryAdd(Arg.Is <NoveltyToAdd>(x => x.Name.Equals("name", StringComparison.Ordinal) && x.Description.Equals("des", StringComparison.Ordinal)), CancellationToken.None).Returns(repositoryReturn);
            var controller = new NoveltyController(_logger, repository);
            var dto        = new NoveltyToAddDto {
                Name = "name", Description = "des"
            };

            // Act
            var result = await controller.Add(dto, CancellationToken.None);

            // Assert
            Assert.That(result, Is.InstanceOf <CreatedResult>());
        }
コード例 #5
0
        public async Task Add_ShouldReturnNotFound_IfProvidedInvalidDto([Values("", null, "valid")] string name, [Values("", null, "valid")] string description)
        {
            if (name?.Equals("valid", StringComparison.Ordinal) == true && description?.Equals("valid", StringComparison.Ordinal) == true)
            {
                return;
            }

            // Arrange
            var repository = Substitute.For <INoveltyRepository>();
            var controller = new NoveltyController(_logger, repository);
            var dto        = new NoveltyToAddDto {
                Description = description, Name = name
            };

            // Act
            var result = await controller.Add(dto, CancellationToken.None);

            // Assert
            Assert.That(result, Is.InstanceOf <BadRequestObjectResult>());
        }