| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using GuestName.Models;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace GuestName.Controllers
- {
- public class HomeController : Controller
- {
- private readonly ILogger<HomeController> _logger;
- public HomeController(ILogger<HomeController> logger)
- {
- _logger = logger;
- }
- public IActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public JsonResult RefreshGuestName(string nextStatus)
- {
- try
- {
- using (var client = new HttpClient())
- {
- string url = "http://10.226.234.12:8039/merchant/getGuestForWeb?nextStatus=" + nextStatus;
- var response = client.GetAsync(url).Result;
- if (response.IsSuccessStatusCode)
- {
- var responseContent = response.Content;
- // by calling .Result you are synchronously reading the result
- string responseString = responseContent.ReadAsStringAsync().Result;
- GuestResponse res = GuestResponse.Parse(responseString);
- if (res.errorCode == "00000")
- {
- if (res.guest != null)
- {
- return Json(new
- {
- error = "0",
- content = res.message,
- name = res.guest.name,
- gender = res.guest.gender == 1 ? "Mr." : "Ms.",
- organize = res.guest.organize,
- position = res.guest.position
- });
- }
- else
- {
- return Json(new
- {
- error = "1",
- content = "No more guest"
- });
- }
- }
- }
- var errorCode = response.StatusCode.ToString();
- return Json(new
- {
- error = errorCode,
- content = "System failed"
- });
- }
- }
- catch (Exception ex)
- {
- _logger.LogError("Error get guest name", ex);
- return Json(new
- {
- error = "-1",
- content = "System failed"
- });
- }
- }
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
- public IActionResult Error()
- {
- return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
- }
- }
- }
|