|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Net.Http.Headers; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Authorization; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using Microsoft.AspNetCore.Mvc; |
| 10 | +using Microsoft.Extensions.Configuration; |
| 11 | +using Newtonsoft.Json; |
| 12 | +using Frontend.Models; |
| 13 | + |
| 14 | +// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 |
| 15 | + |
| 16 | +namespace Frontend.Controllers |
| 17 | +{ |
| 18 | + |
| 19 | + public class OrderController : Controller |
| 20 | + { |
| 21 | + private static HttpClient httpOrderClient = new HttpClient(); |
| 22 | + public OrderController(IConfiguration configuration) |
| 23 | + { |
| 24 | + Configuration = configuration; |
| 25 | + } |
| 26 | + public IConfiguration Configuration { get; } |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + //CREATE |
| 31 | + [HttpGet] |
| 32 | + public async Task<IActionResult> Order() |
| 33 | + { |
| 34 | + ViewBag.LogMessage = HttpContext.Session.GetString("UserName"); |
| 35 | + await Task.Delay(1000); |
| 36 | + return View(); |
| 37 | + } |
| 38 | + |
| 39 | + [HttpPost] |
| 40 | + public async Task<IActionResult> Order(Order orderNo) |
| 41 | + { |
| 42 | + |
| 43 | + var serializedProductToCreate = JsonConvert.SerializeObject(orderNo); |
| 44 | + var request = new HttpRequestMessage(HttpMethod.Post, Configuration.GetValue<string>("WebAPIBaseUrl") + "/Order"); |
| 45 | + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 46 | + request.Content = new StringContent(serializedProductToCreate); |
| 47 | + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
| 48 | + var response = await httpOrderClient.SendAsync(request); |
| 49 | + |
| 50 | + if (response.IsSuccessStatusCode) |
| 51 | + { |
| 52 | + return RedirectToAction("OrderMessage", "Messages"); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + return RedirectToAction("Load4", "Error"); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + //INDEX |
| 64 | + [HttpGet] |
| 65 | + public async Task<IActionResult> Index() |
| 66 | + { |
| 67 | + httpOrderClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 68 | + httpOrderClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token")); |
| 69 | + var response = await httpOrderClient.GetAsync(Configuration.GetValue<string>("WebAPIBaseUrl") + "/Order/Order"); |
| 70 | + var content = await response.Content.ReadAsStringAsync(); |
| 71 | + |
| 72 | + ViewBag.LogMessage = HttpContext.Session.GetString("UserName"); |
| 73 | + |
| 74 | + if (response.IsSuccessStatusCode) |
| 75 | + { |
| 76 | + var orderNo = new List<Order>(); |
| 77 | + if (response.Content.Headers.ContentType.MediaType == "application/json") |
| 78 | + { |
| 79 | + orderNo = JsonConvert.DeserializeObject<List<Order>>(content); |
| 80 | + } |
| 81 | + return View(orderNo); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + return RedirectToAction("Load4", "Error"); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + //MyOrder |
| 91 | + [HttpGet] |
| 92 | + public async Task<IActionResult> MyOrder(string email) |
| 93 | + { |
| 94 | + httpOrderClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 95 | + httpOrderClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token")); |
| 96 | + var response = await httpOrderClient.GetAsync(Configuration.GetValue<string>("WebAPIBaseUrl") + $"/Order/Email/admin@localhost"); |
| 97 | + var content = await response.Content.ReadAsStringAsync(); |
| 98 | + |
| 99 | + ViewBag.LogMessage = HttpContext.Session.GetString("UserName"); |
| 100 | + |
| 101 | + if (response.IsSuccessStatusCode) |
| 102 | + { |
| 103 | + var orderNo = new List<Order>(); |
| 104 | + if (response.Content.Headers.ContentType.MediaType == "application/json") |
| 105 | + { |
| 106 | + orderNo = JsonConvert.DeserializeObject<List<Order>>(content); |
| 107 | + } |
| 108 | + return View(orderNo); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + return RedirectToAction("Load4", "Error"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | +} |
0 commit comments