-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
41 lines (32 loc) · 1.21 KB
/
Program.cs
File metadata and controls
41 lines (32 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using Microsoft.EntityFrameworkCore;
using Scalar.AspNetCore;
using System.Net.Http.Json;
var builder = WebApplication.CreateBuilder(args);
// SERVIÇOS
builder.Services.AddOpenApi();
builder.Services.AddHttpClient();
builder.Services.AddDbContext<AppDbContext>(opt => opt.UseSqlite("Data Source=cripto.db"));
var app = builder.Build();
// PIPELINE
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
app.MapGet("/", () => Results.Redirect("/scalar/v1"));
// ENDPOINT: PREÇO REAL
app.MapGet("/precos", async (IHttpClientFactory clientFactory) =>
{
var client = clientFactory.CreateClient();
client.DefaultRequestHeaders.Add("User-Agent", "CryptoApp");
var url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd";
var dados = await client.GetFromJsonAsync<CoinGeckoDto>(url);
return Results.Ok(new { BTC = dados?.bitcoin.usd, ETH = dados?.ethereum.usd });
});
app.Run();
// MODELOS E DTOs
public class AppDbContext : DbContext {
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
public record CoinGeckoDto(PriceDetail bitcoin, PriceDetail ethereum);
public record PriceDetail(decimal usd);