Skip to content

Commit

Permalink
fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed May 22, 2023
1 parent e20240a commit 482bff2
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"dotnet-ef": {
"version": "7.0.3",
"version": "7.0.5",
"commands": [
"dotnet-ef"
]
Expand Down
21 changes: 20 additions & 1 deletion src/Modules/Catalog/Core/Model/Entity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
using System.Collections;

namespace Catalog.Core.Model;

public readonly record struct ProductName(string Name);

public sealed record Tag(string Name);

public sealed record Tags(IReadOnlyCollection<Tag> Value) : IEnumerable<Tag>
{
public static readonly Tags Empty = new Tags(Array.Empty<Tag>());
public IEnumerator<Tag> GetEnumerator()
{
return Value.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

public sealed record ProductDescription(string Description);

public readonly record struct Price(decimal Value)
Expand Down Expand Up @@ -30,4 +49,4 @@ public sealed record ProductPrice(Price CurrentPrice, Price? PromotionalPrice =
public readonly record struct AvailableQuantity(int Value);

public sealed record Product(ProductId Id, ProductName ProductName, ProductDescription ProductDescription, ProductPrice Price,
AvailableQuantity AvailableQuantity);
AvailableQuantity AvailableQuantity, Tags? Tags);
1 change: 1 addition & 0 deletions src/Modules/Catalog/Core/Repository/IProductFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public sealed class Filter
public string? Query { get; init; }
public decimal? PriceFrom { get; init; }
public decimal? PriceTo { get; init; }
public string Tag { get; init; }
internal int Skip => (Page - 1) * PageSize;
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Catalog.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class Tags2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<List<string>>(
name: "tags",
table: "products",
type: "text[]",
nullable: true,
oldClrType: typeof(List<string>),
oldType: "text[]");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<List<string>>(
name: "tags",
table: "products",
type: "text[]",
nullable: false,
oldClrType: typeof(List<string>),
oldType: "text[]",
oldNullable: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasAnnotation("Npgsql:TsVectorProperties", new[] { "Name", "Description", "Tags" });

b.Property<List<string>>("Tags")
.IsRequired()
.HasColumnType("text[]")
.HasColumnName("tags");

Expand Down
6 changes: 4 additions & 2 deletions src/Modules/Catalog/Infrastructure/Model/EfProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class EfProduct

public int AvailableQuantity { get; set; }

public List<string> Tags { get; set; }
public List<string>? Tags { get; set; }

public NpgsqlTsVector SearchVector { get; set; } = null!;

Expand All @@ -41,11 +41,13 @@ public EfProduct(Product product)
AvailableQuantity = product.AvailableQuantity.Value;
Price = product.Price.CurrentPrice;
PromotionalPrice = product.Price.PromotionalPrice;
Tags = product.Tags?.Select(x => x.Name).ToList();
}

public Product ToProduct()
{
var tags = Tags?.Select(tag => new Tag(tag)).ToArray() ?? Array.Empty<Tag>();
return new Product(ProductId, new ProductName(Name), new ProductDescription(Description),
new ProductPrice(Price, PromotionalPrice), new AvailableQuantity(AvailableQuantity));
new ProductPrice(Price, PromotionalPrice), new AvailableQuantity(AvailableQuantity), new Tags(tags));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public async IAsyncEnumerable<Product> FilterProducts(Filter filter, [Enumerator
{
query = query.Where(x => x.SearchVector.Matches(filter.Query));
}

if (!string.IsNullOrEmpty(filter.Tag))
{
query = query.Where(x => x.SearchVector.Matches(filter.Tag));
}

if (filter.PriceFrom.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task ReadProductByIdWhenNoExistsShouldReturnNull()
await using var host = await _apiFixture.GetHost();
var productId = ProductId.New();
// Act
var resp = await host.Scenario(s =>
await host.Scenario(s =>
{
s.Get.Url($"/api/products/{productId.Value}");
s.StatusCodeShouldBe(404);
Expand All @@ -62,7 +62,7 @@ public async Task ReadProductByIdWhenExistsShouldReturnProduct()
cts.CancelAfter(TimeSpan.FromSeconds(30));
var productId = ProductId.New();
var product = new Product(productId, new ProductName("xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
// Act

await _apiFixture.ProductsWriter.AddProduct(product, cts.Token);
Expand Down Expand Up @@ -91,7 +91,7 @@ await host.Scenario(s =>
{
s.Get.Url($"/api/products");
s.StatusCodeShouldBe(204);
}); ;
});
}

[Fact]
Expand All @@ -104,11 +104,11 @@ public async Task SearchProductsWhenNiveaProductExistsShouldReturnProductsWithNa
await using var host = await _apiFixture.GetHost();

var product1 = new Product(ProductId.New(), new ProductName("not xDDD"), new ProductDescription("nivea"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product2 = new Product(ProductId.New(), new ProductName("Nivea xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product3 = new Product(ProductId.New(), new ProductName("xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
await _apiFixture.ProductsWriter.AddProducts(new[] { product1, product2, product3 }, cts.Token);
// Act

Expand All @@ -135,11 +135,11 @@ public async Task FilterProductWhenProductsInPriceConditionExistsShouldReturnPro

await using var host = await _apiFixture.GetHost();
var product1 = new Product(ProductId.New(), new ProductName("not xDDD"), new ProductDescription("nivea"),
new ProductPrice(new Price(5m), new Price(1m)), new AvailableQuantity(10));
new ProductPrice(new Price(5m), new Price(1m)), new AvailableQuantity(10), Tags.Empty);
var product2 = new Product(ProductId.New(), new ProductName("Nivea xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(11m)), new AvailableQuantity(10));
new ProductPrice(new Price(11m)), new AvailableQuantity(10), Tags.Empty);
var product3 = new Product(ProductId.New(), new ProductName("xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(20m), new Price(20m)), new AvailableQuantity(10));
new ProductPrice(new Price(20m), new Price(20m)), new AvailableQuantity(10), Tags.Empty);
await _apiFixture.ProductsWriter.AddProducts(new[] { product1, product2, product3 }, cts.Token);
// Act

Expand Down Expand Up @@ -168,11 +168,11 @@ public async Task FilterProductWhenNiveaProductExistsShouldReturnOneProductWithN

await using var host = await _apiFixture.GetHost();
var product1 = new Product(ProductId.New(), new ProductName("not xDDD"), new ProductDescription("nivea"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product2 = new Product(ProductId.New(), new ProductName("Nivea xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product3 = new Product(ProductId.New(), new ProductName("xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
await _apiFixture.ProductsWriter.AddProducts(new[] { product1, product2, product3 }, cts.Token);
// Act
var resp = await host.Scenario(s =>
Expand Down Expand Up @@ -200,11 +200,11 @@ public async Task FilterProductWhenNiveaProductExistsShouldReturnOneProductWithN
await using var host = await _apiFixture.GetHost();

var product1 = new Product(ProductId.New(), new ProductName("not xDDD"), new ProductDescription("nivea"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product2 = new Product(ProductId.New(), new ProductName("Nivea xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product3 = new Product(ProductId.New(), new ProductName("xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
await _apiFixture.ProductsWriter.AddProducts(new[] { product1, product2, product3 }, cts.Token);
// Act

Expand Down Expand Up @@ -234,15 +234,15 @@ public async Task SearchProductInNonExistentPage()
await using var host = await _apiFixture.GetHost();

var product1 = new Product(ProductId.New(), new ProductName("not xDDD"), new ProductDescription("nivea"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product2 = new Product(ProductId.New(), new ProductName("Nivea xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
var product3 = new Product(ProductId.New(), new ProductName("xDDD"), new ProductDescription("xDDD"),
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10));
new ProductPrice(new Price(10m), new Price(5m)), new AvailableQuantity(10), Tags.Empty);
await _apiFixture.ProductsWriter.AddProducts(new[] { product1, product2, product3 }, cts.Token);
// Act

var resp = await host.Scenario(s =>
await host.Scenario(s =>
{
s.Get.Url($"/api/products?query=nivea&pageSize=1&page=2137");
s.StatusCodeShouldBe(204);
Expand Down
Loading

0 comments on commit 482bff2

Please sign in to comment.