コード例 #1
0
        public async Task CreateSpeciesReturnsGuidOnSuccess()
        {
            // Given
            CreateSpeciesModel model = ModelFactory.CreationModel();

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

            // Then
            speciesId.Should().NotBe(Guid.Empty);
        }
コード例 #2
0
        public void CreateSpeciesThrowsExceptionOnNameConflict()
        {
            // Given
            Domain.Species existingSpecies = ModelFactory.DomainModel();
            SeedDatabase(existingSpecies);
            CreateSpeciesModel model = ModelFactory.CreationModel();

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

            // Then
            createSpecies.Should().Throw <SpeciesWithNameAlreadyExistsException>();
        }
コード例 #3
0
        public async Task <ActionResult> Post([FromBody] CreateSpeciesViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                CreateSpeciesModel createSpeciesModel = _mapper.Map <CreateSpeciesModel>(model);
                Guid speciesId = await _commands.Create(createSpeciesModel);

                return(CreatedAtRoute(nameof(GetSpecies), new { id = speciesId }, null));
            }
            catch (Exception ex) when(ex is ResourceStateException)
            {
                return(Conflict(new ErrorViewModel(ex)));
            }
        }
コード例 #4
0
        public async Task <Guid> Create(CreateSpeciesModel model)
        {
            IQueryable <Domain.Species> anyWithNameAndLatinName =
                from existingSpeciesWithNameAndLatinName in _database.Species
                where (existingSpeciesWithNameAndLatinName.Name == model.Name) &&
                (existingSpeciesWithNameAndLatinName.LatinName == model.LatinName)
                select existingSpeciesWithNameAndLatinName;

            if (await anyWithNameAndLatinName.AnyAsync())
            {
                throw new SpeciesWithNameAlreadyExistsException(model.Name);
            }

            Domain.Species species = _mapper.Map <Domain.Species>(model);
            await _database.Species.AddAsync(species);

            await _database.SaveAsync();

            return(species.Id);
        }