Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions GradDemo.Api/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ public async Task<ActionResult<DeviceCredentials>> RegisterUser()
ClientSecret = password
};
}
[HttpPost("register/set/currency/{currency}")]
public async Task<ActionResult<DeviceCredentials>> RegisterUser(string currency)
{
string password = GenerateRandomClientSecret();
string username = GenerateRandomClientId();

var currentUser = await _userManager.CreateAsync(new Device()
{
UserName = username,
Email = $"{username}@grademoapi.co.za",
AccessFailedCount = 0,
EmailConfirmed = true,
currency = currency
}, password);

return new DeviceCredentials
{
ClientId = username,
ClientSecret = password
};
}

private string GenerateRandomClientId()
{
Expand Down
55 changes: 45 additions & 10 deletions GradDemo.Api/Controllers/CryptoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using GradDemo.Api.Models;
using Newtonsoft.Json;
using GradDemo.Api.Models.CoinGecko;
using GradDemo.Api.Providers;
using Microsoft.AspNetCore.Identity;
using GradDemo.Api.Entities;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

Expand All @@ -21,19 +19,56 @@ namespace GradDemo.Api.Controllers
public class CryptoController : ControllerBase
{
private readonly CoinGeckoProvider _coinGeckoProvider;

public CryptoController(CoinGeckoProvider coinProv)
private readonly UserManager<Device> _userManager;
private readonly ApplicationDbContext _context;
public CryptoController(CoinGeckoProvider coinProv, UserManager<Device> userManager,ApplicationDbContext context)
{
_coinGeckoProvider = coinProv;
_userManager = userManager;
_context = context;
}

[HttpGet("value/for/{coinId}/currency/{currency}")]
public async Task<Response<CryptoCoinResponse>> GetCoin(string coinId, string currency)
[HttpGet("value/for/bitcoin/currency/{currency}")]
public async Task<Response<CryptoCoinResponse>> GetCoin(string currency)
{
var result = new CryptoCoinResponse();
var res = await _coinGeckoProvider.GetValueForCoin(currency);

var res = await _coinGeckoProvider.GetValueForCoin(coinId, currency);
if (res.HasValue)
{
return Response<CryptoCoinResponse>.Successful(new CryptoCoinResponse()
{
Value = res.Value
});
}

return Response<CryptoCoinResponse>.Error("Something went wrong");
}
[HttpGet("currencies/")]
public async Task<Response<List<string>>> GetCurrencies()
{

var res = await _coinGeckoProvider.GetCurrencies();

if (res.Count>0)
{
return Response<List<string>>.Successful(res);
}

return Response<List<string>>.Error("Something went wrong");
}
[Authorize]
[HttpGet("custom/response/")]
public async Task<Response<CryptoCoinResponse>> GetCoinCustom()
{
var user = await _userManager.GetUserAsync(User);
await _context.SaveChangesAsync();
string currency = user.currency;
if (currency == null)
{
currency = "usd";
}
var res = await _coinGeckoProvider.GetValueForCoin($"{currency}");
if (res.HasValue)
{
return Response<CryptoCoinResponse>.Successful(new CryptoCoinResponse()
Expand Down
4 changes: 3 additions & 1 deletion GradDemo.Api/Entities/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace GradDemo.Api.Entities
{
public class Device : IdentityUser
{
public string ShirtSize { get; set; }
internal string name;

public string currency { get; set; }
}
}
4 changes: 4 additions & 0 deletions GradDemo.Api/GradDemo.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Providers\" />
</ItemGroup>

</Project>
Loading