public Task Add([FromBody] CreateNutrientModel model) { this.TryValidateModel(model); var command = new CreateNutrient(model.Title); return(this.createNutrientHandler.HandleCommandAsync(command)); }
public async Task CreateNutrientReturnsGuidOnSuccess() { // Given CreateNutrientModel model = ModelFactory.CreationModel(); // When Guid nutrientId = await _commands.Create(model); // Then nutrientId.Should().NotBe(Guid.Empty); }
public Task CreateNutrientAsync(string title) { if (title == null) { throw new ArgumentNullException(nameof(title)); } var uri = UrisProvider.AddNutrient; var content = new CreateNutrientModel(title); return(this.httpClient.PostAsJsonAsync(uri, content)); }
public void CreateNutrientThrowsExceptionOnNameConflict() { // Given Nutrient existingNutrient = ModelFactory.DomainModel(); SeedDatabase(existingNutrient); CreateNutrientModel model = ModelFactory.CreationModel(); // When Func <Task> createNutrient = async() => await _commands.Create(model); // Then createNutrient.Should().Throw <NutrientWithNameAlreadyExistsException>(); }
public async Task <Guid> Create(CreateNutrientModel model) { IQueryable <Nutrient> existingNutrientsWithName = from existingNutrient in _database.Nutrients where existingNutrient.Name == model.Name select existingNutrient; if (await existingNutrientsWithName.AnyAsync()) { throw new NutrientWithNameAlreadyExistsException(model.Name); } Nutrient nutrient = _mapper.Map <Nutrient>(model); await _database.Nutrients.AddAsync(nutrient); await _database.SaveAsync(); return(nutrient.Id); }
public async Task <ActionResult> Post([FromBody] CreateNutrientViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { CreateNutrientModel createNutrientModel = _mapper.Map <CreateNutrientModel>(model); Guid nutrientId = await _commands.Create(createNutrientModel); return(CreatedAtRoute(nameof(GetNutrient), new { id = nutrientId }, null)); } catch (Exception ex) when(ex is ResourceStateException) { return(Conflict(new ErrorViewModel(ex))); } }