Beispiel #1
0
        public async Task AddCharacterAsync(CharacterForCreationDto dto)
        {
            var character = new Character(dto.Name, dto.Planet, dto.Episodes);

            await _characterRepository.AddAsync(character);

            await _characterRepository.SaveChangesAsync();
        }
Beispiel #2
0
        public async Task CreateCharacterAsync_ShouldWork()
        {
            var url  = "/api/characters";
            var body = new CharacterForCreationDto()
            {
                Name = "Luke Skywalker", Episodes = new string[] { "JEDI", "NEWHOPE", "EMPIRE" }
            };

            using var scope = _factory.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService <StarWarsDbContext>();

            var httpResponse = await _client.PostAsync(url,
                                                       new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"));

            httpResponse.EnsureSuccessStatusCode();

            var actualStatusCode   = httpResponse.StatusCode;
            var expectedStatusCode = HttpStatusCode.Created;

            Assert.Equal(expectedStatusCode, actualStatusCode);
            Assert.True(dbContext.Characters.Any(c => c.Name == "Luke Skywalker"));
        }
        public IActionResult CreateCharacter([FromBody] CharacterForCreationDto characterToCreate)
        {
            if (characterToCreate == null)
            {
                return(BadRequest());
            }
            ModelState.CheckModelForSameNameAndPlanet(characterToCreate);
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var character = Mapper.Map <Character>(characterToCreate);

            repository.AddCharacter(character);

            Save(exceptionMessage: "Creating character failed on save.");

            var characterToReturn = Mapper.Map <CharacterDto>(character);

            return(CreatedAtRoute("GetCharacter",
                                  new { id = character.Id },
                                  characterToReturn));
        }
Beispiel #4
0
        public async Task <IActionResult> CreateCharacterAsync([FromBody] CharacterForCreationDto dto)
        {
            await _characterService.AddCharacterAsync(dto);

            return(Created(string.Empty, null));
        }