GlobalController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Filters;
  3. using Microsoft.AspNetCore.WebUtilities;
  4. using economy.Helpers;
  5. using economy.Models;
  6. using economy.Models.Financial;
  7. using economy.Models.Price.Global;
  8. using NaturalGas = economy.Models.Price.Global.NaturalGas;
  9. namespace economy.Controllers.Price.Global
  10. {
  11. public class GlobalController : Controller
  12. {
  13. private readonly Alpha_API _alphaAPI;
  14. private readonly KoreaEximGoKR _koreaEximGoKR;
  15. private Dictionary<string, string> _queryString;
  16. private int _exchangeValue = 0;
  17. public GlobalController(Alpha_API alphaAPI, KoreaEximGoKR koreaEximGoKR)
  18. {
  19. _alphaAPI = alphaAPI;
  20. _koreaEximGoKR = koreaEximGoKR;
  21. InitializeAsync();
  22. }
  23. // 초기화 메서드
  24. public async Task InitializeAsync()
  25. {
  26. // 환율 조회
  27. _exchangeValue = await new FinancialModel(_koreaEximGoKR).GetExchangeValue();
  28. }
  29. public override void OnActionExecuting(ActionExecutingContext context)
  30. {
  31. _queryString = QueryHelpers.ParseQuery(HttpContext.Request.QueryString.Value).ToDictionary(k => k.Key, v => string.Join(",", v.Value));
  32. ViewBag.QueryString = _queryString;
  33. base.OnActionExecuting(context);
  34. }
  35. [HttpGet("Price/Global/NaturalGas")]
  36. public async Task<IActionResult> NaturalGas(NaturalGas.Request request)
  37. {
  38. if (!ModelState.IsValid)
  39. {
  40. return BadRequest(ModelState);
  41. }
  42. if (!new[] { "daily", "weekly", "monthly" }.Contains(request.Interval))
  43. {
  44. return BadRequest("Invalid input provided.");
  45. }
  46. GlobalModel globalModel = new GlobalModel(_alphaAPI);
  47. NaturalGas.Response itemList = await globalModel.GetNaturalGasPriceInfo(request);
  48. int total = 0;
  49. if (itemList.Data != null && itemList.Data.Any())
  50. {
  51. total = itemList.Data.Count;
  52. int listNum = Common.CalcListNumber(total, request.PageNo, request.NumOfRows);
  53. // 페이징 처리
  54. int skip = (request.PageNo - 1) * request.NumOfRows;
  55. int take = request.NumOfRows;
  56. itemList.Data = itemList.Data.Skip(skip).Take(take).Select((row, index) =>
  57. {
  58. row.Num = (listNum - index);
  59. if (int.TryParse(row.Value, out int value) && value > 0)
  60. {
  61. row.KRW = (value * _exchangeValue);
  62. }
  63. return row;
  64. }).ToList();
  65. }
  66. var viewModel = new View<NaturalGas.Request, NaturalGas.Response>();
  67. viewModel.SelectedListPerPage = request.NumOfRows;
  68. viewModel.Request = request;
  69. viewModel.Response = itemList;
  70. var queryString = new
  71. {
  72. date = request.Interval
  73. };
  74. var pagination = new Pagination(total, request.PageNo, request.NumOfRows, queryString);
  75. viewModel.Pagination = pagination;
  76. return View("/Views/Price/Global/NaturalGas.cshtml", viewModel);
  77. }
  78. }
  79. }