Add Reviews to product service and product details VM
This commit is contained in:
Родитель
8bc625da37
Коммит
84f5373dc7
|
@ -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"
|
||||
}
|
||||
]
|
Загрузка…
Ссылка в новой задаче