public async Task <bool> CreateAsync(string Url, NationalParkCreateDto objToCreate, string token)
        {
            if (objToCreate != null)
            {
                HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, Url);
                httpRequest.Content = new StringContent(JsonConvert.SerializeObject(objToCreate), 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.Created)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        [ProducesResponseType(StatusCodes.Status500InternalServerError)]                  // 5. Part 8
        public IActionResult CreateNationalPark([FromBody] NationalParkCreateDto nationalParkCreateDto)
        {
            // if nationalParkDto == null
            if (nationalParkCreateDto == null)
            {
                return(BadRequest());
            }

            // if NationalPark already exists, add error to the modelstate
            if (npRepository.NationalParkExists(nationalParkCreateDto.Name))
            {
                ModelState.AddModelError("", "National Park already exists!");
                return(BadRequest(ModelState)); // In the tutorial, we returned 404 status code, which isn't convinced to me "return StatusCode(404, ModelState);".
            }

            #region ModelState
            // We don't need that piece of code, because if the model state is not valid it will automatically return badrequest with the model validations messages.

            /*
             * // We comment that block of code in "4. Part 6"
             *
             * // if ModelState not valid
             * if(!ModelState.IsValid)
             * {
             *  return BadRequest(ModelState);
             * }
             *
             */
            #endregion

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

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

            //return Ok();

            return(CreatedAtRoute("GetNationalPark", new { version = HttpContext.GetRequestedApiVersion().ToString(), // 7. Part 5
                                                           nationalParkId = nationalPark.Id }, nationalPark));        // 4. Part 6
        }
        public async Task <IActionResult> Create(NationalParkCreateDto model, IFormFile Picture)
        {
            if (ModelState.IsValid)
            {
                //var files = HttpContext.Request.Form.Files; // to get the uploaded files without specify parameter to recieve them
                if (Picture != null)
                {
                    using (var ms = new MemoryStream())
                    {
                        Picture.CopyTo(ms);
                        model.Picture = ms.ToArray();
                    }
                }

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

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