コード例 #1
0
        public async Task <ActionResult <Character> > Put([FromBody] CharacterUpdateDto modelUpdateDto, int id)
        {
            try
            {
                var character = await _repository.GetCharacterByIdAsync(id);

                if (character == null)
                {
                    return(NotFound());
                }
                if (ModelState.IsValid)
                {
                    var potterApi = new PotterApi();
                    var accio     = await potterApi.AccioHouse(modelUpdateDto.House);

                    if (accio)
                    {
                        _mapper.Map(modelUpdateDto, character);
                        await _repository.SaveChangesAsync();

                        return(NoContent());
                    }
                    else
                    {
                        ModelState.AddModelError("Accio", "Accio House didn't work");
                        return(BadRequest(ModelState));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
コード例 #2
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));
            }
        }