예제 #1
0
        public Task Add([FromBody] CreateNutrientModel model)
        {
            this.TryValidateModel(model);

            var command = new CreateNutrient(model.Title);

            return(this.createNutrientHandler.HandleCommandAsync(command));
        }
예제 #2
0
        public async Task CreateNutrientReturnsGuidOnSuccess()
        {
            // Given
            CreateNutrientModel model = ModelFactory.CreationModel();

            // When
            Guid nutrientId = await _commands.Create(model);

            // Then
            nutrientId.Should().NotBe(Guid.Empty);
        }
예제 #3
0
        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));
        }
예제 #4
0
        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>();
        }
예제 #5
0
        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);
        }
예제 #6
0
        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)));
            }
        }