Advertisement
Guest User

ReviewController

a guest
Feb 16th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Text.Json;
  5. using System.Threading.Tasks;
  6. using BooksCatalogue.Models;
  7. using Microsoft.AspNetCore.Mvc;
  8.  
  9. namespace BooksCatalogue.Controllers
  10. {
  11.     public class ReviewController : Controller
  12.     {
  13.         private string apiEndpoint = "https://localhost:8000/api/";
  14.         public string baseurl = "https://localhost:5001/Books/Details/";
  15.         private readonly HttpClient _client;
  16.         HttpClientHandler clientHandler = new HttpClientHandler();
  17.  
  18.         public ReviewController()
  19.         {
  20.             clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
  21.             _client = new HttpClient(clientHandler);
  22.  
  23.         }
  24.  
  25.         // GET: Review/AddReview/2
  26.         public async Task<IActionResult> AddReview(int? bookId)
  27.         {
  28.             if (bookId == null)
  29.             {
  30.                 return NotFound();
  31.             }
  32.  
  33.             HttpClient client = new HttpClient();
  34.             HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, apiEndpoint + "books/" + bookId);
  35.  
  36.             HttpResponseMessage response = await _client.SendAsync(request);
  37.  
  38.             switch (response.StatusCode)
  39.             {
  40.                 case HttpStatusCode.OK:
  41.                     string responseString = await response.Content.ReadAsStringAsync();
  42.                     var book = JsonSerializer.Deserialize<Book>(responseString);
  43.  
  44.                     ViewData["BookId"] = bookId;
  45.                     return View("Add");
  46.                 case HttpStatusCode.NotFound:
  47.                     return NotFound();
  48.                 default:
  49.                     return ErrorAction("Error. Status code = " + response.StatusCode + ": " + response.ReasonPhrase);
  50.             }
  51.         }
  52.  
  53.         // TODO: Tambahkan fungsi ini untuk mengirimkan atau POST data review menuju API
  54.         // POST: Review/AddReview
  55.         [HttpPost]
  56.         [ValidateAntiForgeryToken]
  57.         public async Task<IActionResult> AddReview([Bind("Id,BookId,ReviewerName,Rating,Comment")] [FromForm] Review review)
  58.         {
  59.             MultipartFormDataContent content = new MultipartFormDataContent();
  60.  
  61.             content.Add(new StringContent(review.BookId.ToString()), "bookid");
  62.             content.Add(new StringContent(review.ReviewerName), "reviewername");
  63.             content.Add(new StringContent(review.Rating.ToString()), "rating");
  64.             content.Add(new StringContent(review.Comment), "comment");
  65.  
  66.             HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, apiEndpoint + "review/");
  67.             request.Content = content;
  68.             HttpResponseMessage response = await _client.SendAsync(request);
  69.             switch (response.StatusCode)
  70.             {
  71.                 case HttpStatusCode.OK:
  72.                 case HttpStatusCode.NoContent:
  73.                 case HttpStatusCode.Created:
  74.                     int idbooks = review.BookId;
  75.                     return Redirect(baseurl + idbooks);
  76.                 default:
  77.                     return ErrorAction("Error. Status code =" + response.StatusCode + "; " + response.ReasonPhrase);
  78.             }
  79.         }
  80.  
  81.         private ActionResult ErrorAction(string message)
  82.         {
  83.             return new RedirectResult("/Home/Error?message=" + message);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement