public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(BadRequest());
            }

            NationalParkDto nationalParkDto = await _npRepo.GetAsync(_npUrl, id.GetValueOrDefault(), HttpContext.Session.GetString("JWToken"));

            if (nationalParkDto == null)
            {
                return(NotFound());
            }

            // NationalParkDto and NationalParkUpdateDto have the same properties, but we create both of them for the seprate of concerns.
            NationalParkUpdateDto nationalParkUpdateDto = new NationalParkUpdateDto()
            {
                Id          = nationalParkDto.Id,
                Name        = nationalParkDto.Name,
                State       = nationalParkDto.State,
                Picture     = nationalParkDto.Picture,
                Established = nationalParkDto.Established
            };

            return(View(nationalParkUpdateDto));
        }
        public async Task <bool> UpdateAsync(string Url, int nationalParkId, NationalParkUpdateDto objToUpdate, string token)
        {
            if (objToUpdate != null)
            {
                HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Put, Url + nationalParkId);
                httpRequest.Content = new StringContent(JsonConvert.SerializeObject(objToUpdate), Encoding.UTF8, "application/json");

                HttpClient httpClient = _httpClientFactory.CreateClient();

                // 13. Part 6
                // ----------------------
                if (token != null && token.Length > 0)
                {
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                }
                // ----------------------

                HttpResponseMessage httpResponse = await httpClient.SendAsync(httpRequest);

                if (httpResponse.StatusCode == System.Net.HttpStatusCode.NoContent)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        [ProducesResponseType(StatusCodes.Status500InternalServerError)] // 5. Part 8
        public IActionResult UpdateNationalPark(int nationalParkId, [FromBody] NationalParkUpdateDto nationalParkUpdateDto)
        {
            if (nationalParkUpdateDto == null || nationalParkId != nationalParkUpdateDto.Id)
            {
                return(BadRequest());
            }

            if (!npRepository.NationalParkExists(nationalParkId))
            {
                return(NotFound());
            }

            var nationalPark = mapper.Map <NationalPark>(nationalParkUpdateDto);

            if (!npRepository.UpdateNationalPark(nationalPark))
            {
                ModelState.AddModelError("", $"Something went wrong, when updating the record {nationalPark.Name}");
                return(StatusCode(500, ModelState));
            }

            return(NoContent());
        }
        public async Task <IActionResult> Edit(NationalParkUpdateDto model, IFormFile PictureFile)
        {
            if (ModelState.IsValid)
            {
                if (PictureFile != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        PictureFile.CopyTo(ms);
                        model.Picture = ms.ToArray();
                    }
                }

                var success = await _npRepo.UpdateAsync(_npUrl, model.Id, model, HttpContext.Session.GetString("JWToken"));

                if (success)
                {
                    return(RedirectToAction(nameof(Index)));
                }
                ModelState.AddModelError("", "SomeError happens.");
            }
            return(View(model));
        }