Add Reviews to product service and product details VM

This commit is contained in:
David Oliver 2021-11-19 15:18:55 -05:00
Родитель 8bc625da37
Коммит 84f5373dc7
7 изменённых файлов: 34 добавлений и 2 удалений

Просмотреть файл

@ -65,7 +65,7 @@ namespace Commerce
.ConfigureServices(services =>
{
services
.AddSingleton<IProductService>(sp => new ProductService("products.json"))
.AddSingleton<IProductService>(sp => new ProductService("products.json", "reviews.json"))
.AddSingleton<ICartService>(sp => new CartService("products.json"))
.AddSingleton<IProfileService>(sp => new ProfileService());
})

Просмотреть файл

@ -167,6 +167,7 @@
<Content Include="$(MSBuildThisFileDirectory)Assets\Products\isochill-armourvent-hat.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Products\profile-michael-scott.png" />
<Content Include="$(MSBuildThisFileDirectory)Assets\Products\the-absolute-freedom.png" />
<Content Include="$(MSBuildThisFileDirectory)reviews.json" />
<None Include="$(MSBuildThisFileDirectory)Assets\SharedAssets.md" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -8,4 +8,6 @@ namespace Commerce.Services;
public interface IProductService
{
Task<IEnumerable<Product>> GetProducts(string? term, CancellationToken ct);
Task<Review[]> GetReviews(int productId, CancellationToken ct);
}

Просмотреть файл

@ -9,8 +9,10 @@ namespace Commerce.Services;
public class ProductService : JsonDataService<Product>, IProductService
{
public ProductService(string dataFile) : base(dataFile)
private readonly JsonDataService<Review> _reviewDataService;
public ProductService(string productDataFile, string reviewDataFile) : base(productDataFile)
{
_reviewDataService = new JsonDataService<Review>(reviewDataFile);
}
public async Task<IEnumerable<Product>> GetProducts(string? term, CancellationToken ct)
@ -23,4 +25,10 @@ public class ProductService : JsonDataService<Product>, IProductService
return products;
}
public async Task<Review[]> GetReviews(int productId, CancellationToken ct)
{
var reviews = await _reviewDataService.GetEntities();
return reviews;
}
}

Просмотреть файл

@ -21,6 +21,8 @@ public partial class ProductDetailsViewModel
public IFeed<Product> Product => Feed.Async(Load);
public IFeed<Review[]> Reviews => Product.SelectAsync(async (p, ct) => await _productService.GetReviews(p.ProductId, ct));
private async ValueTask<Product> Load(CancellationToken ct)
=> _product;
}

Просмотреть файл

@ -25,11 +25,13 @@ partial class ProductDetailsViewModel : IAsyncDisposable
Model = vm;
Product = ctx.GetOrCreateState(vm.Product);
Reviews = ctx.GetOrCreateState(vm.Reviews);
}
public ProductDetailsViewModel Model { get; }
public IFeed<Product> Product { get; }
public IFeed<Review[]> Reviews { get; }
}
/// <inheritdoc />

Просмотреть файл

@ -0,0 +1,17 @@
[
{
"Name": "Jean-Ralphio",
"Message": "Really good shoes. Love them",
"Photo": "https://loremflickr.com/360/360/face?random=1"
},
{
"Name": "Eric",
"Message": "Instant buy, instant classic",
"Photo": "https://loremflickr.com/360/360/face?random=2"
},
{
"Name": "Lisa Kudrow",
"Message": "I feel like walking on clouds with these shoes. Never experienced somthing simliar",
"Photo": "https://loremflickr.com/360/360/face?random=3"
}
]