public async Task <AddOrUpdateResult <INovelty> > TryAdd(NoveltyToAdd novelty, CancellationToken cancellationToken) { var now = DateTime.UtcNow; var noveltyEntity = new Novelty { Created = now, Description = novelty.Description, Name = novelty.Name, LastChanged = now, Version = 1 }; await _context.Novelties.AddAsync(noveltyEntity, cancellationToken); var added = await _context.SaveChangesAsync(cancellationToken); return(added > 0 ? AddOrUpdateResult <INovelty> .Success(noveltyEntity) : AddOrUpdateResult <INovelty> .Failure()); }
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>()); }
public async Task <AddOrUpdateResult <INovelty> > TryUpdate(NoveltyToAdd novelty, CancellationToken cancellationToken) { var now = DateTime.UtcNow; var noveltyToUpdate = await _context.Novelties.SingleOrDefaultAsync(x => x.ID == novelty.ID, cancellationToken); if (noveltyToUpdate == null) { return(AddOrUpdateResult <INovelty> .Failure(FailureReason.EntityNotFound)); } noveltyToUpdate.LastChanged = now; noveltyToUpdate.Version++; noveltyToUpdate.Name = novelty.Name; noveltyToUpdate.Description = novelty.Description; var changes = await _context.SaveChangesAsync(cancellationToken); return(changes > 0 ? AddOrUpdateResult <INovelty> .Success(noveltyToUpdate) : AddOrUpdateResult <INovelty> .Failure()); }
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>()); }