Skip to content

Commit d5fd8d2

Browse files
fixed MVC code access
1 parent 0c853b2 commit d5fd8d2

16 files changed

+165
-24
lines changed

AdventureWorks.API.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventureWorks.Client", "So
2828
EndProject
2929
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdventureWorks.Common", "Source\AdventureWorks.Common\AdventureWorks.Common.csproj", "{78787B84-7EA2-4D18-B276-9CDA7B17C4FC}"
3030
EndProject
31+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventureWorks.Domain", "Source\AdventureWorks.Domain\AdventureWorks.Domain.csproj", "{93107024-9A1B-431C-9CD1-4A48EB8FFD51}"
32+
EndProject
3133
Global
3234
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3335
Debug|Any CPU = Debug|Any CPU
@@ -60,6 +62,10 @@ Global
6062
{78787B84-7EA2-4D18-B276-9CDA7B17C4FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
6163
{78787B84-7EA2-4D18-B276-9CDA7B17C4FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
6264
{78787B84-7EA2-4D18-B276-9CDA7B17C4FC}.Release|Any CPU.Build.0 = Release|Any CPU
65+
{93107024-9A1B-431C-9CD1-4A48EB8FFD51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
66+
{93107024-9A1B-431C-9CD1-4A48EB8FFD51}.Debug|Any CPU.Build.0 = Debug|Any CPU
67+
{93107024-9A1B-431C-9CD1-4A48EB8FFD51}.Release|Any CPU.ActiveCfg = Release|Any CPU
68+
{93107024-9A1B-431C-9CD1-4A48EB8FFD51}.Release|Any CPU.Build.0 = Release|Any CPU
6369
EndGlobalSection
6470
GlobalSection(SolutionProperties) = preSolution
6571
HideSolutionNode = FALSE
@@ -73,6 +79,7 @@ Global
7379
{C6631DEC-7B3F-42E7-8659-92727CD8E918} = {EB2AA2DC-EDB4-47D2-B63B-05D92CABB774}
7480
{E818CB1E-B31B-4100-BE26-CBC2BC592BB8} = {EB2AA2DC-EDB4-47D2-B63B-05D92CABB774}
7581
{78787B84-7EA2-4D18-B276-9CDA7B17C4FC} = {637325EA-5158-4046-B167-80FE953C1F1F}
82+
{93107024-9A1B-431C-9CD1-4A48EB8FFD51} = {637325EA-5158-4046-B167-80FE953C1F1F}
7683
EndGlobalSection
7784
GlobalSection(ExtensibilityGlobals) = postSolution
7885
SolutionGuid = {282EED39-786B-4C93-A944-B13FF9C78CDF}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using AdventureWorks.Api.Extensions;
24
using AdventureWorks.Service;
35
using AdventureWorks.Service.Dtos.Collections;
46
using Microsoft.AspNetCore.Authorization;
@@ -18,42 +20,42 @@ public PersonController(IAdventureWorksService adventureWorksService)
1820
}
1921

2022
/// <summary>
21-
/// This Get method returns a persons name
23+
/// GET returns a persons name
2224
/// </summary>
2325
[HttpGet("Name")]
2426
[ProducesResponseType(StatusCodes.Status200OK)]
25-
public async Task<ActionResult> GetPersonsNameByBusinessId(int businessId)
27+
public async Task<ActionResult> GetPersonsNameByBusinessId(int businessId, CancellationToken cancellation)
2628
{
27-
var response = await _adventureWorksService.GetPersonsNameAsync(businessId);
29+
var response = await _adventureWorksService.GetPersonsNameAsync(businessId, cancellation);
2830
if (response == null)
2931
return NotFound($"Couldn't find the person for the given businessId {businessId}");
30-
return Ok(response);
32+
return Ok(response.Map());
3133
}
3234

3335
/// <summary>
34-
/// This Patch method Updates a persons Name
36+
/// PATCH Updates a persons Name
3537
/// </summary>
3638
[HttpPatch("Name")]
3739
[ProducesResponseType(StatusCodes.Status200OK)]
38-
public async Task<ActionResult> UpdatePersonBybussinessId(int businessId, [FromBody] PersonNameBaseDto personName)
40+
public async Task<ActionResult> UpdatePersonBybussinessId(int businessId, [FromBody] PersonNameBaseDto personName, CancellationToken cancellation)
3941
{
40-
var response = await _adventureWorksService.PatchPersonsNameAsync(businessId, personName);
42+
var response = await _adventureWorksService.PatchPersonsNameAsync(businessId, personName, cancellation);
4143
if (response == null)
4244
return NotFound($"Couldn't find the person for the given businessId {businessId}");
43-
return Ok(response);
45+
return Ok(response.Map());
4446
}
4547

4648
/// <summary>
47-
/// This Gets a persons basic information
49+
/// GET returns persons basic information
4850
/// </summary>
4951
[HttpGet]
5052
[ProducesResponseType(StatusCodes.Status200OK)]
51-
public async Task<ActionResult> GetPersonByBusinessEntity(int businessId)
53+
public async Task<ActionResult> GetPersonByBusinessEntity(int businessId, CancellationToken cancellation)
5254
{
53-
var response = await _adventureWorksService.GetPersonAsync(businessId);
55+
var response = await _adventureWorksService.GetPersonAsync(businessId, cancellation);
5456
if (response == null)
5557
return NotFound($"Couldn't find the person for the given businessId {businessId}");
56-
return Ok(response);
58+
return Ok(response.Map());
5759
}
5860
}
5961
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Linq;
2+
using AdventureWorks.Domain.Response;
3+
using AdventureWorks.Service.Dtos.Collections;
4+
5+
namespace AdventureWorks.Api.Extensions
6+
{
7+
public static class AdventureWorksMapperExtension
8+
{
9+
public static PersonNameResponse Map(this PersonNameDto personName) => new PersonNameResponse
10+
{
11+
FirstName = personName.FullName,
12+
LastName = personName.LastName,
13+
LastUpdated = personName.LastUpdated,
14+
MiddleName = personName.MiddleName
15+
};
16+
17+
public static PersonResponse Map(this PersonDto person) => new PersonResponse
18+
{
19+
FirstName = person.FullName,
20+
LastName = person.LastName,
21+
LastUpdated = person.LastUpdated,
22+
MiddleName = person.MiddleName,
23+
CreditCards = person.CreditCards.Select(c => new CreditCardResponse
24+
{
25+
CardNumber = c.CardNumber
26+
}),
27+
Emails = person.Emails.Select(c => new EmailResponse
28+
{
29+
Email = c.Email
30+
}),
31+
PhoneNumbers = person.PhoneNumbers.Select(c => new PhoneResponse
32+
{
33+
PhoneNumber = c.PhoneNumber
34+
})
35+
};
36+
}
37+
}

Source/AdventureWorks.API/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Extensions.DependencyInjection;
2121
using Microsoft.Extensions.Hosting;
2222
using Microsoft.Extensions.Logging;
23+
using Microsoft.IdentityModel.Tokens;
2324
using Newtonsoft.Json;
2425
using Swashbuckle.AspNetCore.SwaggerGen;
2526
using Swashbuckle.AspNetCore.SwaggerUI;
@@ -53,7 +54,11 @@ public void ConfigureServices(IServiceCollection services)
5354
{
5455
options.Authority = "http://localhost:5000";
5556
options.RequireHttpsMetadata = false;
56-
options.Audience = "api1";
57+
options.TokenValidationParameters = new TokenValidationParameters
58+
{
59+
ValidateAudience = false
60+
};
61+
//options.Audience = "AdventureWorks.OAuth";
5762
});
5863

5964
//Use this for Basic Authentiction
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AdventureWorks.Domain.Response
2+
{
3+
public class AdventureWorksResponse
4+
{
5+
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AdventureWorks.Domain.Response
2+
{
3+
public class BusinessEntityResponse
4+
{
5+
public int BusinessEntityId { get; set; }
6+
public int PersonId { get; set; }
7+
public int ContactTypeId { get; set; }
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AdventureWorks.Domain.Response
2+
{
3+
public class CreditCardResponse
4+
{
5+
public string CardNumber { get; set; }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AdventureWorks.Domain.Response
2+
{
3+
public class EmailResponse
4+
{
5+
public string Email { get; set; }
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace AdventureWorks.Domain.Response
2+
{
3+
public class PersonNameBaseResponse
4+
{
5+
public string FirstName { get; set; }
6+
public string MiddleName { get; set; }
7+
public string LastName { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)