Skip to content

Commit bd3ee74

Browse files
authored
Catalog Api - Refactor (#102)
1 parent 0491627 commit bd3ee74

File tree

26 files changed

+395
-96
lines changed

26 files changed

+395
-96
lines changed

src/Services/Basket/Basket.API/Authorization/UserAuthorizationHandler.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,21 @@ protected override Task HandleRequirementAsync(
1717
UserIdRequirement requirement,
1818
string username)
1919
{
20-
2120
var userRole = GetUserRole(context.User);
22-
if (userRole == string.Empty)
23-
{
24-
context.Fail();
25-
return Task.CompletedTask;
26-
}
2721

28-
if (userRole == "admin")
22+
if (userRole != "admin")
2923
{
30-
if (!ValidateUserPermissions(
31-
context.User.Claims.ToList(),
32-
requirement.Requirements.ToList()))
24+
var usernameInClaim = GetUsername(context.User);
25+
if (string.IsNullOrEmpty(usernameInClaim) || username != usernameInClaim)
3326
{
3427
context.Fail();
3528
return Task.CompletedTask;
3629
}
37-
context.Succeed(requirement);
38-
return Task.CompletedTask;
3930
}
4031

41-
var usernameInClaim = GetUsername(context.User);
42-
if (username != usernameInClaim)
43-
{
44-
context.Fail();
45-
return Task.CompletedTask;
46-
}
47-
4832
if (!ValidateUserPermissions(
49-
context.User.Claims.ToList(),
50-
requirement.Requirements.ToList()))
33+
context.User.Claims.ToArray(),
34+
requirement.Requirements.ToArray()))
5135
{
5236
context.Fail();
5337
return Task.CompletedTask;

src/Services/Catalog/Catalog.API.IntegrationTests/ApiFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
2020
service.AddMarten(options =>
2121
{
2222
options.Connection(postgresConnection);
23-
options.Schema.For<Product>().UseNumericRevisions(true);
23+
options.Schema.For<ProductDocument>().UseNumericRevisions(true);
2424
}).UseLightweightSessions();
2525

2626
service

src/Services/Catalog/Catalog.API.IntegrationTests/Database/DataSeeder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ public async Task SeedDataBaseAsync(Action<ProductConfiguration>? productConfigu
1212
var products = productConfiguration is null ? GetListOfProducts() : [product.ToDbProduct()];
1313

1414
await using var session = store.LightweightSession();
15-
session.Store<Product>(products);
15+
session.Store<ProductDocument>(products);
1616
await session.SaveChangesAsync();
1717
}
1818

19-
public async Task<IReadOnlyList<Product>> GetAllData()
19+
public async Task<IReadOnlyList<ProductDocument>> GetAllData()
2020
{
2121
await using var session = store.LightweightSession();
22-
var data = await session.Query<Product>().ToListAsync();
22+
var data = await session.Query<ProductDocument>().ToListAsync();
2323
return data;
2424
}
2525

26-
private static IEnumerable<Product> GetListOfProducts()
26+
private static IEnumerable<ProductDocument> GetListOfProducts()
2727
{
2828
return
2929
[

src/Services/Catalog/Catalog.API.IntegrationTests/Database/ProductConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public record ProductConfiguration
1515

1616
public static class CreateProductConfiguration
1717
{
18-
public static Product ToDbProduct(this ProductConfiguration configuration)
18+
public static ProductDocument ToDbProduct(this ProductConfiguration configuration)
1919
{
20-
return new Product
20+
return new ProductDocument
2121
{
2222
Id = configuration.Id,
2323
Name = configuration.Name,

src/Services/Catalog/Catalog.API.IntegrationTests/Features/CreateProducts/CreateProductsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public async Task CreateProduct_With_Valid_Object_Return_Created(CreateProductRe
152152

153153
result.StatusCode.ShouldBe(HttpStatusCode.Created);
154154
await using var session = apiSpecification.GetDocumentStore().LightweightSession();
155-
var valueInDb = session.Query<Product>().First(x => x.Id == createProductRequest.Id);
155+
var valueInDb = session.Query<ProductDocument>().First(x => x.Id == createProductRequest.Id);
156156
valueInDb.ShouldNotBeNull();
157157
valueInDb.Id.ShouldBe(productId);
158158
}

src/Services/Catalog/Catalog.API.IntegrationTests/Features/GetProductById/GetProductByIdTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ public async Task GetProductById_Product_NotFound_Returns_NotFound()
2323

2424
// Act
2525
var result = await _client.GetAsync($"api/v1/catalog/products/{productId}");
26-
var response = await result.Content.ReadFromJsonAsync<ProblemDetails>();
26+
var response = await result.Content.ReadFromJsonAsync<string>();
2727

2828
// Assert
29-
result.StatusCode.ShouldBe(System.Net.HttpStatusCode.NotFound);
29+
result.StatusCode.ShouldBe(HttpStatusCode.NotFound);
3030
response.ShouldNotBeNull();
31-
response.Detail.ShouldNotBeNull();
32-
response.Detail.ShouldBe($"Entity \"Product\" ({productId}) was not found.");
31+
response.ShouldBe(productId.ToString());
3332
}
3433

3534
[Fact]

src/Services/Catalog/Catalog.API.IntegrationTests/Features/GetProducts/GetProductsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task DisposeAsync()
9696
await Task.CompletedTask;
9797
}
9898

99-
private static List<ProductResponse> GetProductsModules(IReadOnlyList<Product> products)
99+
private static List<ProductResponse> GetProductsModules(IReadOnlyList<ProductDocument> products)
100100
{
101101
return products.Select(x => new ProductResponse(
102102
Id: Ulid.Parse(x.Id),

src/Services/Catalog/Catalog.API.IntegrationTests/Features/GetProductsByCategory/GetProductsByCategoryTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public async Task DisposeAsync()
5656
await Task.CompletedTask;
5757
}
5858

59-
private static ProductModule ToProductModule(Product product)
59+
private static ProductModule ToProductModule(ProductDocument productDocument)
6060
{
6161
return new ProductModule(
62-
Id: Ulid.Parse(product.Id),
63-
Name: product.Name,
64-
Category: product.Category,
65-
Description: product.Description,
66-
ImageFile: product.ImageFile,
67-
Price: product.Price);
62+
Id: Ulid.Parse(productDocument.Id),
63+
Name: productDocument.Name,
64+
Category: productDocument.Category,
65+
Description: productDocument.Description,
66+
ImageFile: productDocument.ImageFile,
67+
Price: productDocument.Price);
6868
}
6969
}
7070
}

src/Services/Catalog/Catalog.API.IntegrationTests/Features/UpdateProduct/UpdateProductTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,12 @@ public async Task UpdateProduct_ProductId_NotFound_Returns_NotFound(UpdateProduc
147147
var result = await _client
148148
.SetFakeBearerToken(FakePermission.GetPermissions([Policies.CatalogProductUpdatePermission]))
149149
.PutAsJsonAsync("api/v1/catalog/products", updateProductRequest);
150-
var response = await result.Content.ReadFromJsonAsync<ProblemDetails>();
150+
var response = await result.Content.ReadFromJsonAsync<string>();
151151

152152
// Assert
153153
result.StatusCode.ShouldBe(HttpStatusCode.NotFound);
154154
response.ShouldNotBeNull();
155-
response.Detail.ShouldNotBeNull();
156-
response.Detail.ShouldContain($"Entity \"Product\" ({updateProductRequest.Id}) was not found.");
155+
response.ShouldBe(updateProductRequest.Id);
157156
}
158157

159158
[Theory, CatalogRequestAutoData]
@@ -206,7 +205,7 @@ await _dataSeeder.SeedDataBaseAsync(x =>
206205
result.StatusCode.ShouldBe(HttpStatusCode.NoContent);
207206

208207
await using var session = apiSpecification.GetDocumentStore().LightweightSession();
209-
var valueInDb = session.Query<Product>().FirstOrDefault(x => x.Id == updateProductRequest.Id);
208+
var valueInDb = session.Query<ProductDocument>().FirstOrDefault(x => x.Id == updateProductRequest.Id);
210209

211210
valueInDb!.Name.ShouldBe(updateProductRequest.Name);
212211
valueInDb.Description.ShouldBe(updateProductRequest.Description);

src/Services/Catalog/Catalog.API/Data/CatalogInitialDataMigration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task Populate(IDocumentStore store, CancellationToken cancellation)
2828
await policy.ExecuteAsync(async () =>
2929
{
3030
await using var session = store.LightweightSession();
31-
if (await session.Query<Product>().AnyAsync(cancellation))
31+
if (await session.Query<ProductDocument>().AnyAsync(cancellation))
3232
return;
3333

3434
session.Store(GetPreConfiguredProducts());
@@ -41,7 +41,7 @@ await policy.ExecuteAsync(async () =>
4141
}
4242
}
4343

44-
private static IEnumerable<Product> GetPreConfiguredProducts()
44+
private static IEnumerable<ProductDocument> GetPreConfiguredProducts()
4545
{
4646
return
4747
[

0 commit comments

Comments
 (0)