Example #1
0
        // GET: Corpulences/Details/5
        public async Task <IActionResult> Details(short?id)
        {
            try
            {
                if (id == null)
                {
                    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);

                // Récurération des données et convertion des données dans le bon type
                string content = await client.GetStringAsync(_configuration["URLAPI"] + $"api/Corpulences/{id}");

                Corpulence corpulence = JsonConvert.DeserializeObject <Corpulence>(content);

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

                return(View(corpulence));
            }
            catch (HttpRequestException)
            {
                return(Unauthorized());
            }
        }
Example #2
0
        public async Task <ActionResult <Corpulence> > PostCorpulence(Corpulence corpulence)
        {
            _context.Corpulences.Add(corpulence);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetCorpulence", new { id = corpulence.CorpulenceId }, corpulence));
        }
Example #3
0
        public async Task <IActionResult> Edit(short id, [Bind("CorpulenceId,CorpulenceName")] Corpulence corpulence)
        {
            try
            {
                if (id != corpulence.CorpulenceId)
                {
                    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(corpulence.ToJson(), Encoding.UTF8, "application/json");
                    HttpResponseMessage response    = await client.PutAsync(_configuration["URLAPI"] + $"api/Corpulences/{id}", httpContent);

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

                return(View(corpulence));
            }
            catch (HttpRequestException)
            {
                return(Unauthorized());
            }
        }
Example #4
0
        public async Task <IActionResult> PutCorpulence(short id, Corpulence corpulence)
        {
            if (id != corpulence.CorpulenceId)
            {
                return(BadRequest());
            }

            _context.Entry(corpulence).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CorpulenceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }