public Task Add([FromBody] CreateNutrientModel model) { this.TryValidateModel(model); var command = new CreateNutrient(model.Title); return(this.createNutrientHandler.HandleCommandAsync(command)); }
public void HandleCommandAsync_WithNullCommand_Throws() { // Arrange const CreateNutrient command = default; // Act Func <Task> run = async() => await this.sut.HandleCommandAsync(command !); // Assert run.Should().ThrowExactly <ArgumentNullException>() .Where(e => e.Message.Contains(nameof(command))); }
public static async Task Main(string[] args) { var repository = new NutrientRepository(); var commandHandler = new CreateNutrientHandler(repository); var command = new CreateNutrient("Vitamin D"); await commandHandler.HandleCommandAsync(command); var queryHandler = new ListNutrientsHandler(repository); var query = new ListNutrients(o => o.Title == "Vitamin D"); var result = await queryHandler.HandleQueryAsync(query); System.Console.ReadKey(); }
public async Task HandleCommandAsync_WithValidArgs_CreatesAndSavesNutrient() { // Arrange var nutrientTitle = new Fixture().Create <string>(); var command = new CreateNutrient(nutrientTitle); // Act await this.sut.HandleCommandAsync(command); // Assert A.CallTo(() => this.repository .InsertOneAsync(A <Nutrient> .That.Matches(n => n.Title == command.Title))) .MustHaveHappenedOnceExactly(); }
public async Task HandleCommandAsync(CreateNutrient command) { if (command is null) { throw new ArgumentNullException(nameof(command)); } var nutrient = await this.repository.FindOneAsync(n => n.Title.Equals(command.Title)); if (nutrient != null) { throw new InvalidOperationException($"Nutrient with the title {nutrient.Title} already exists"); } nutrient = Nutrient.Create(command.Title); await this.repository.InsertOneAsync(nutrient); }
public void HandleCommandAsync_WithExistingNutrient_Throws() { // Arrange var nutrientTitle = new Fixture().Create <string>(); var fakeNutrient = Nutrient.Create(nutrientTitle); var command = new CreateNutrient(nutrientTitle); A.CallTo(() => this.repository.FindOneAsync(A <Expression <Func <Nutrient, bool> > > .Ignored)) .Returns(fakeNutrient); // Act Func <Task> run = async() => await this.sut.HandleCommandAsync(command); // Assert run.Should().ThrowExactly <InvalidOperationException>() .WithMessage($"Nutrient with the title {nutrientTitle} already exists"); A.CallTo(() => this.repository.FindOneAsync(A <Expression <Func <Nutrient, bool> > > .Ignored)) .MustHaveHappenedOnceExactly(); }