Пример #1
0
        public async Task CreateFieldReturnsGuidOnSuccess()
        {
            // Given
            CreateFieldModel model = ModelFactory.CreationModel();

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

            // Then
            fieldId.Should().NotBe(Guid.Empty);
        }
Пример #2
0
        public void CreateFieldThrowsExceptionOnNameConflict()
        {
            // Given
            Field existingField = ModelFactory.DomainModel();

            SeedDatabase(existingField);
            CreateFieldModel model = ModelFactory.CreationModel();

            // When
            Func <Task> createField = async() => await _commands.Create(model);

            // Then
            createField.Should().Throw <FieldWithNameAlreadyExistsException>();
        }
Пример #3
0
        public async Task <Guid> Create(CreateFieldModel model)
        {
            IQueryable <Field> existingFieldsWithName = from existingField in _database.Fields
                                                        where existingField.Name == model.Name
                                                        select existingField;

            if (await existingFieldsWithName.AnyAsync())
            {
                throw new FieldWithNameAlreadyExistsException(model.Name);
            }

            Field field = _mapper.Map <Field>(model);
            await _database.Fields.AddAsync(field);

            await _database.SaveAsync();

            return(field.Id);
        }
Пример #4
0
        public async Task <ActionResult> Post([FromBody] CreateFieldViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                CreateFieldModel createFieldModel = _mapper.Map <CreateFieldModel>(model);
                Guid             fieldId          = await _commands.Create(createFieldModel);

                return(CreatedAtRoute(nameof(GetField), new { id = fieldId }, null));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }