Commit ebe8f8e8 authored by Clint Mourlevat's avatar Clint Mourlevat

Finishing autorefreshing with SignalR

parent 9ace9991
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Stratis.Guru.Models;
namespace Stratis.Guru.Controllers
{
[Route("api")]
[ApiController]
public class ApiController : ControllerBase
{
private readonly IMemoryCache _memoryCache;
public ApiController(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
[HttpGet]
[Route("price")]
public ActionResult<object> Price(bool stringify)
{
try
{
dynamic coinmarketcap = JsonConvert.DeserializeObject(_memoryCache.Get("Coinmarketcap").ToString());
if (stringify)
{
return new TickerApi
{
UsdPrice = coinmarketcap.data.quotes.USD.price.ToString("C"),
Last24Change = (coinmarketcap.data.quotes.USD.percent_change_24h / 100).ToString("P2")
};
}
return new Ticker
{
UsdPrice = coinmarketcap.data.quotes.USD.price,
Last24Change = coinmarketcap.data.quotes.USD.percent_change_24h / 100
};
}
catch (Exception e)
{
//TODO: implement errors / logging
return null;
}
}
}
}
\ No newline at end of file
namespace Stratis.Guru.Models
{
public class TickerApi
{
public string UsdPrice { get; set; }
public string Last24Change { get; set; }
}
}
\ No newline at end of file
...@@ -28,13 +28,13 @@ ...@@ -28,13 +28,13 @@
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
var stratisPrice = @Model.UsdPrice; var stratisPrice = @Model.UsdPrice;
var signalr = new signalR.HubConnectionBuilder().withUrl("/update").build(); var signalr = new signalR.HubConnectionBuilder().withUrl("/update").build();
signalr.on("UpdateTicker", function (message) { signalr.on("UpdateTicker", function (message) {
$.getJSON("/api/price?stringify=true", function(e) {
$("#amount").text(e.usdPrice);
$("#lastchange").text(e.last24Change);
});
}); });
signalr.start(); signalr.start();
$(".click-edit").click(function() { $(".click-edit").click(function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment