Exemplo n.º 1
0
        public async Task <ActionResult <Character> > Post(
            [FromServices] ApiVersion apiVersion,
            [FromBody] CharacterCreateDto modeCreatelDto)
        {
            try
            {
                var model = _mapper.Map <Character>(modeCreatelDto);
                if (ModelState.IsValid)
                {
                    var potterApi = new PotterApi();
                    var accio     = await potterApi.AccioHouse(modeCreatelDto.House);

                    if (accio)
                    {
                        await _repository.CreateCharacterAsync(model);

                        await _repository.SaveChangesAsync();

                        var commandReadDto = _mapper.Map <CharacterReadDto>(model);

                        return(CreatedAtRoute(nameof(GetById), new { Id = commandReadDto.Id, version = $"{apiVersion}" }, commandReadDto));
                    }
                    else
                    {
                        ModelState.AddModelError("Accio", "Accio House didn't work");
                        return(BadRequest(ModelState));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }