Skip to content

Commit 774b505

Browse files
committed
Calling the API from the Admin app
1 parent 02eeef9 commit 774b505

7 files changed

Lines changed: 50 additions & 38 deletions

File tree

admin/TwoWeeksReady.Admin/Data/FunctionsRepository.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
5+
using System.Net.Http.Json;
46
using System.Threading.Tasks;
57
using TwoWeeksReady.Admin.Security;
68
using TwoWeeksReady.Common.EmergencyKits;
@@ -9,11 +11,14 @@ namespace TwoWeeksReady.Admin.Data
911
{
1012
public class FunctionsRepository : IRepository
1113
{
12-
private TokenProvider _tokenProvider;
14+
private readonly HttpClient _httpClient;
15+
private readonly TokenProvider _tokenProvider;
1316

14-
public FunctionsRepository(TokenProvider tokenProvider)
17+
public FunctionsRepository(IHttpClientFactory httpClientFactory, TokenProvider tokenProvider)
1518
{
16-
this._tokenProvider = tokenProvider;
19+
this._httpClient = httpClientFactory.CreateClient("ApiClient");
20+
_tokenProvider = tokenProvider;
21+
this._httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenProvider.AccessToken);
1722
}
1823
public Task<IEnumerable<BaseKit>> GetAllBaseKits()
1924
{
@@ -25,9 +30,9 @@ public Task<IEnumerable<HazardHunt>> GetAllHazardHunts()
2530
throw new NotImplementedException();
2631
}
2732

28-
public Task<IEnumerable<HazardInfo>> GetAllHazardInfos()
33+
public async Task<IEnumerable<HazardInfo>> GetAllHazardInfos()
2934
{
30-
throw new NotImplementedException();
35+
return await _httpClient.GetFromJsonAsync<IList<HazardInfo>>("hazardinfo-list");
3136
}
3237

3338
public Task<BaseKit> GetBaseKitById(string id)
@@ -40,9 +45,16 @@ public Task<HazardHunt> GetHazardHuntById(string id)
4045
throw new NotImplementedException();
4146
}
4247

43-
public Task<HazardInfo> GetHazardInfoById(string id)
48+
public async Task<HazardInfo> GetHazardInfoById(string id)
4449
{
45-
throw new NotImplementedException();
50+
try
51+
{
52+
return await _httpClient.GetFromJsonAsync<HazardInfo>($"hazardinfo-by-id/{id}");
53+
} catch (Exception ex)
54+
{
55+
return new HazardInfo();
56+
}
57+
4658
}
4759

4860
public Task<BaseKitItem> SaveBaseKitItem(BaseKitItem kit)

admin/TwoWeeksReady.Admin/Data/StubRepository.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public class StubRepository : IRepository
4343
{
4444
Id = "ABCDEFG",
4545
Name = "Bookshelf Safety",
46-
Description = "Strap the bookshelf to the wall",
47-
Directive = "Buy straps, find stud, screw it the wall",
48-
ExternalLink = "https://twitch.tv"
46+
Description = "Strap the bookshelf to the wall"
4947
}
5048
};
5149

@@ -55,9 +53,7 @@ public class StubRepository : IRepository
5553
{
5654
Id = "BEGIN",
5755
Name = "Earthquakes",
58-
Description = "All about earthquakes",
59-
Directive = "Cover your head",
60-
ExternalLink = "https://twitch.tv"
56+
Description = "All about earthquakes"
6157
}
6258
};
6359

admin/TwoWeeksReady.Admin/Pages/HazardHunts/Details.razor

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ Hazard Name:
1010
<br />
1111
Short Description:
1212
<input type="text" @bind="@Hazard.Description" />
13-
<br />
14-
Instructions:
15-
<input type="text" @bind="@Hazard.Directive" />
16-
<br />
17-
More Info Link:
18-
<input type="text" @bind="@Hazard.ExternalLink" />
19-
<br />
2013
<span class="btn btn-secondary float-right" style="cursor: pointer" @onclick="@Save">Save</span>
2114

2215
@code {

admin/TwoWeeksReady.Admin/Pages/HazardInfos/Details.razor

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
@inject IRepository repository
55
@inject IJSRuntime JS
66

7-
<h3>Details</h3>
8-
Hazard Name:
9-
<input type="text" @bind="@Hazard.Name" />
10-
<br />
11-
Short Description:
12-
<input type="text" @bind="@Hazard.Description" />
13-
<br />
14-
Instructions:
15-
<input type="text" @bind="@Hazard.Directive" />
16-
<br />
17-
More Info Link:
18-
<input type="text" @bind="@Hazard.ExternalLink" />
19-
<br />
20-
<span class="btn btn-secondary float-right" style="cursor: pointer" @onclick="@Save">Save</span>
7+
@if (Hazard == null)
8+
{
9+
<p><em>Loading...</em></p>
10+
}
11+
else
12+
{
13+
<h3>Details</h3>
14+
<label for="name">Hazard Name:</label>
15+
<input type="text" @bind="@Hazard.Name" />
16+
<br />
17+
<label for="description">Short Description:</label>
18+
<input type="text" @bind="@Hazard.Description" />
19+
<span class="btn btn-secondary float-right" style="cursor: pointer" @onclick="@Save">Save</span>
20+
21+
}
2122

2223
@code {
2324

admin/TwoWeeksReady.Admin/Pages/Kits/Details.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ else
3333

3434
Kit = await repository.GetBaseKitById(Id);
3535

36-
base.OnInitializedAsync();
36+
await base.OnInitializedAsync();
3737

3838
}
3939

admin/TwoWeeksReady.Admin/Startup.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
8+
89
using Microsoft.AspNetCore.Http;
910
using Microsoft.AspNetCore.Authentication.Cookies;
1011
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
1112
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
1213
using Microsoft.IdentityModel.Tokens;
1314
using TwoWeeksReady.Admin.Security;
15+
using System;
16+
using System.Net.Http;
1417

1518
namespace TwoWeeksReady.Admin
1619
{
@@ -57,6 +60,7 @@ public void ConfigureServices(IServiceCollection services)
5760
options.Scope.Clear();
5861
options.Scope.Add("openid");
5962
options.Scope.Add("profile");
63+
options.Scope.Add("roles");
6064

6165
options.CallbackPath = new PathString("/callback");
6266
options.ClaimsIssuer = "Auth0";
@@ -104,9 +108,14 @@ public void ConfigureServices(IServiceCollection services)
104108
});
105109

106110
services.AddHttpContextAccessor();
111+
services.AddHttpClient("ApiClient", (HttpClient client) =>
112+
{
113+
client.BaseAddress = new Uri(Configuration["ApiUrl"]);
114+
});
115+
107116
services.AddScoped<TokenProvider>();
108-
services.AddScoped<IRepository, StubRepository>();
109-
//services.AddScoped<IRepository, FunctionsRepository>();
117+
//services.AddScoped<IRepository, StubRepository>();
118+
services.AddScoped<IRepository, FunctionsRepository>();
110119
}
111120

112121
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

admin/TwoWeeksReady.Admin/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"Microsoft.Hosting.Lifetime": "Information"
77
}
88
},
9-
"AllowedHosts": "*",
9+
"AllowedHosts": "*",
10+
"ApiUrl": "http://localhost:7071/api/",
1011
"Auth0": {
1112
"Domain": "YOUR_AUTH0_DOMAIN",
1213
"ClientId": "YOUR_CLIENT_ID",

0 commit comments

Comments
 (0)