Exemple #1
0
        public async Task <IActionResult> Edit(short id, [Bind("Id,Name,Slug,Answer")] BotCommand botCommand)
        {
            try
            {
                if (id != botCommand.Id)
                {
                    return(NotFound());
                }

                // Préparation de l'appel à l'API
                string accessToken = await HttpContext.GetTokenAsync("access_token");

                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                if (ModelState.IsValid)
                {
                    // Préparation de la requête update à l'API
                    StringContent       httpContent = new StringContent(botCommand.ToJson(), Encoding.UTF8, "application/json");
                    HttpResponseMessage response    = await client.PutAsync(_configuration["URLAPI"] + $"api/BotCommands/{id}", httpContent);

                    if (response.StatusCode != HttpStatusCode.NoContent)
                    {
                        return(BadRequest());
                    }
                    return(RedirectToAction(nameof(Index)));
                }

                return(View(botCommand));
            }
            catch (HttpRequestException)
            {
                return(Unauthorized());
            }
        }