Skip to content

Commit c8317d0

Browse files
committed
WIP
1 parent 9a9900d commit c8317d0

File tree

12 files changed

+102
-19
lines changed

12 files changed

+102
-19
lines changed

src/Learn.WebApp/Client/Learn.WebApp.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>
17+
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.8" />
1718
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.13.1.21947">
1819
<PrivateAssets>all</PrivateAssets>
1920
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Learn.WebApp.Shared.CoursePath;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Net.Http;
5+
using System.Net.Http.Json;
6+
using System.Threading.Tasks;
7+
8+
namespace Learn.WebApp.Client
9+
{
10+
public class LearnClient
11+
{
12+
private readonly HttpClient _client;
13+
14+
public LearnClient(HttpClient client)
15+
{
16+
_client = client ?? throw new ArgumentNullException(nameof(client));
17+
}
18+
19+
public async Task<IEnumerable<CoursePathResponseModel>> GetCoursePathsAsync()
20+
{
21+
try
22+
{
23+
return await _client
24+
.GetFromJsonAsync<IEnumerable<CoursePathResponseModel>>("CoursePath")
25+
.ConfigureAwait(false);
26+
}
27+
catch (NotSupportedException ex)
28+
{
29+
/* todo */
30+
throw;
31+
}
32+
}
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
@page "/admin/course-paths"
2+
@inject LearnClient Client
23

34
<h1>Course Paths</h1>
45

6+
@if (_models != null)
7+
{
8+
@foreach (var model in _models)
9+
{
10+
<div>
11+
@model.Key
12+
</div>
13+
}
14+
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
namespace Learn.WebApp.Client.Pages.Admin
1+
using Learn.WebApp.Shared.CoursePath;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
5+
namespace Learn.WebApp.Client.Pages.Admin
26
{
37
public partial class ListCoursePaths
48
{
9+
private IEnumerable<CoursePathResponseModel> _models = null!;
10+
11+
protected override async Task OnInitializedAsync()
12+
{
13+
_models = await Client.GetCoursePathsAsync().ConfigureAwait(true);
514

15+
await base.OnInitializedAsync().ConfigureAwait(true);
16+
}
617
}
718
}

src/Learn.WebApp/Client/Pages/FetchData.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ else
3737

3838
@code {
3939

40-
private WeatherForecast[]? forecasts;
40+
private WeatherForecastModel[]? forecasts;
4141

4242
protected override async Task OnInitializedAsync()
4343
{
44-
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
44+
forecasts = await Http.GetFromJsonAsync<WeatherForecastModel[]>("WeatherForecast");
4545
}
4646

4747
}

src/Learn.WebApp/Client/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public static async Task Main(string[] args)
1515

1616
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
1717

18+
builder.Services.AddHttpClient<LearnClient>(client =>
19+
{
20+
client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress);
21+
});
22+
1823
await builder.Build().RunAsync().ConfigureAwait(false);
1924
}
2025
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
using AutoMapper;
2+
using Learn.Server.Shared;
3+
using Learn.WebApp.Shared;
4+
using Learn.WebApp.Shared.CoursePath;
25

36
namespace Learn.WebApp.Server
47
{
58
internal class AutoMapperProfile : Profile
69
{
710
public AutoMapperProfile()
811
{
9-
CreateMap<Shared.WeatherForecast, Learn.Server.Shared.WeatherForecast>().ReverseMap();
10-
CreateMap<Shared.CoursePathApiModel, Learn.Server.Shared.CoursePath>().ReverseMap();
12+
// request models to business models
13+
CreateMap<WeatherForecastModel, WeatherForecast>();
14+
CreateMap<CoursePathPostRequestModel, CoursePath>();
15+
CreateMap<CoursePathDeleteRequestModel, CoursePath>();
16+
17+
// business models to response models
18+
CreateMap<CoursePath, CoursePathResponseModel>();
1119
}
1220
}
1321
}

src/Learn.WebApp/Server/Controllers/CoursePathController.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Learn.Server.Data.Exceptions;
33
using Learn.Server.Data.Repositories;
44
using Learn.Server.Shared;
5-
using Learn.WebApp.Shared;
65
using Learn.WebApp.Shared.Conflict;
76
using Learn.WebApp.Shared.CoursePath;
87
using Microsoft.AspNetCore.Mvc;
@@ -36,15 +35,15 @@ public CoursePathController(ILogger<CoursePathController> logger, ICoursePathRep
3635
}
3736

3837
[HttpGet]
39-
public async Task<ActionResult<IEnumerable<CoursePathApiModel>>> GetAllAsync(CancellationToken cancellationToken)
38+
public async Task<ActionResult<IEnumerable<CoursePathResponseModel>>> GetAllAsync(CancellationToken cancellationToken)
4039
{
4140
var result = await _repository.GetAllAsync(cancellationToken).ConfigureAwait(false);
4241

43-
return Ok(_mapper.Map<IEnumerable<CoursePathApiModel>>(result));
42+
return Ok(_mapper.Map<IEnumerable<CoursePathResponseModel>>(result));
4443
}
4544

4645
[HttpGet("{key}")]
47-
public async Task<ActionResult<CoursePathApiModel>> GetAsync([FromRoute, Required] Guid key)
46+
public async Task<ActionResult<CoursePathResponseModel>> GetAsync([FromRoute, Required] Guid key)
4847
{
4948
// get the cached entity
5049
var result = await _factory.GetCoursePathGrain(key).GetAsync().ConfigureAwait(false);
@@ -53,12 +52,12 @@ public async Task<ActionResult<CoursePathApiModel>> GetAsync([FromRoute, Require
5352
if (result is null) return NotFound();
5453

5554
// the entity exists
56-
return Ok(_mapper.Map<CoursePathApiModel>(result));
55+
return Ok(_mapper.Map<CoursePathResponseModel>(result));
5756
}
5857

5958
[HttpPost]
6059
[SwaggerResponse(409, Type = typeof(ConflictApiResponseModel))]
61-
public async Task<ActionResult<CoursePathApiModel>> PostAsync([FromBody, Required, DisallowNull] CoursePathApiModel input)
60+
public async Task<ActionResult<CoursePathResponseModel>> PostAsync([FromBody, Required, DisallowNull] CoursePathPostRequestModel input)
6261
{
6362
// to keep the compiler happy
6463
if (input is null) return BadRequest();
@@ -115,7 +114,7 @@ public async Task<ActionResult<CoursePathApiModel>> PostAsync([FromBody, Require
115114
});
116115
}
117116

118-
return Ok(_mapper.Map<CoursePathApiModel>(result));
117+
return Ok(_mapper.Map<CoursePathResponseModel>(result));
119118
}
120119

121120
[HttpDelete]

src/Learn.WebApp/Server/Controllers/WeatherForecastController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ public WeatherForecastController(IGrainFactory factory, IMapper mapper)
2323
}
2424

2525
[HttpGet]
26-
public async Task<ActionResult<IEnumerable<WeatherForecast>>> GetAsync()
26+
public async Task<ActionResult<IEnumerable<WeatherForecastModel>>> GetAsync()
2727
{
2828
var forecasts = await _factory
2929
.GetWeatherForecastGrain()
3030
.GetAllAsync()
3131
.ConfigureAwait(false);
3232

33-
var result = _mapper.Map<IEnumerable<WeatherForecast>>(forecasts);
33+
var result = _mapper.Map<IEnumerable<WeatherForecastModel>>(forecasts);
3434

3535
return Ok(result);
3636
}

src/Learn.WebApp/Shared/CoursePathApiModel.cs renamed to src/Learn.WebApp/Shared/CoursePath/CoursePathPostRequestModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System;
22
using System.ComponentModel.DataAnnotations;
33

4-
namespace Learn.WebApp.Shared
4+
namespace Learn.WebApp.Shared.CoursePath
55
{
6-
public class CoursePathApiModel
6+
public class CoursePathPostRequestModel
77
{
88
public Guid? Key { get; set; }
99

10-
[Required, MaxLength(1000)]
10+
[Required]
1111
public string? Name { get; set; }
1212

13-
[Required, MaxLength(1000)]
13+
[Required]
1414
public string? Slug { get; set; }
1515

1616
public Guid? Version { get; set; }

0 commit comments

Comments
 (0)