From d031ceb4361cf0f91af555dba1c0d01ab6a919d6 Mon Sep 17 00:00:00 2001 From: neozhu Date: Fri, 11 Feb 2022 14:24:38 +0800 Subject: [PATCH] finished product features --- .../Commands/Import/ImportProductsCommand.cs | 24 ++- .../Import/ImportProductsCommandValidator.cs | 12 +- .../Queries/Export/ExportProductsQuery.cs | 22 +-- .../Pagination/ProductsPaginationQuery.cs | 2 +- .../Pages/Products/Products.razor | 139 +++++++++++------- src/Blazor.Server.UI/Pages/_Layout.cshtml | 21 ++- 6 files changed, 146 insertions(+), 74 deletions(-) diff --git a/src/Application/Features/Products/Commands/Import/ImportProductsCommand.cs b/src/Application/Features/Products/Commands/Import/ImportProductsCommand.cs index cad3203d5..11a1906b8 100644 --- a/src/Application/Features/Products/Commands/Import/ImportProductsCommand.cs +++ b/src/Application/Features/Products/Commands/Import/ImportProductsCommand.cs @@ -40,13 +40,29 @@ IMapper mapper } public async Task Handle(ImportProductsCommand request, CancellationToken cancellationToken) { - //TODO:Implementing ImportProductsCommandHandler method + var result = await _excelService.ImportAsync(request.Data, mappers: new Dictionary> { - //ex. { _localizer["Name"], (row,item) => item.Name = row[_localizer["Name"]]?.ToString() }, - + { _localizer["Product Name"], (row,item) => item.Name = row[_localizer["Product Name"]]?.ToString() }, + { _localizer["Description"], (row,item) => item.Description = row[_localizer["Description"]]?.ToString() }, + { _localizer["Unit"], (row,item) => item.Unit = row[_localizer["Unit"]]?.ToString() }, + { _localizer["Price of unit"], (row,item) => item.Price =row.IsNull(_localizer["Price of unit"])? 0m:Convert.ToDecimal(row[_localizer["Price of unit"]]) }, + { _localizer["Pictures"], (row,item) => item.Pictures =row.IsNull(_localizer["Pictures"])? null:row[_localizer["Pictures"]].ToString().Split(",").ToList() }, }, _localizer["Products"]); - throw new System.NotImplementedException(); + if (result.Succeeded) + { + foreach(var dto in result.Data) + { + var item = _mapper.Map(dto); + await _context.Products.AddAsync(item,cancellationToken); + } + await _context.SaveChangesAsync(cancellationToken); + return Result.Success(); + } + else + { + return Result.Failure(result.Errors); + } } public async Task Handle(CreateProductsTemplateCommand request, CancellationToken cancellationToken) { diff --git a/src/Application/Features/Products/Commands/Import/ImportProductsCommandValidator.cs b/src/Application/Features/Products/Commands/Import/ImportProductsCommandValidator.cs index 4e9b5b7ef..d175c6c12 100644 --- a/src/Application/Features/Products/Commands/Import/ImportProductsCommandValidator.cs +++ b/src/Application/Features/Products/Commands/Import/ImportProductsCommandValidator.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. namespace CleanArchitecture.Blazor.Application.Features.Products.Commands.Import; @@ -7,11 +7,11 @@ public class ImportProductsCommandValidator : AbstractValidator v.Data) - // .NotNull() - // .NotEmpty(); - throw new System.NotImplementedException(); + + RuleFor(v => v.Data) + .NotNull() + .NotEmpty(); + } } diff --git a/src/Application/Features/Products/Queries/Export/ExportProductsQuery.cs b/src/Application/Features/Products/Queries/Export/ExportProductsQuery.cs index c9bae9011..ec74762ec 100644 --- a/src/Application/Features/Products/Queries/Export/ExportProductsQuery.cs +++ b/src/Application/Features/Products/Queries/Export/ExportProductsQuery.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. @@ -8,10 +8,10 @@ namespace CleanArchitecture.Blazor.Application.Features.Products.Queries.Export; public class ExportProductsQuery : IRequest { - public string FilterRules { get; set; } - public string Sort { get; set; } = "Id"; - public string Order { get; set; } = "desc"; - } + public string OrderBy { get; set; } = "Id"; + public string SortDirection { get; set; } = "Desc"; + public string Keyword { get; set; } = String.Empty; +} public class ExportProductsQueryHandler : IRequestHandler @@ -36,16 +36,18 @@ IStringLocalizer localizer public async Task Handle(ExportProductsQuery request, CancellationToken cancellationToken) { - //TODO:Implementing ExportProductsQueryHandler method - var filters = PredicateBuilder.FromFilter(request.FilterRules); - var data = await _context.Products.Where(filters) - .OrderBy("{request.Sort} {request.Order}") + var data = await _context.Products.Where(x=>x.Name.Contains(request.Keyword) || x.Description.Contains(request.Keyword)) + .OrderBy($"{request.OrderBy} {request.SortDirection}") .ProjectTo(_mapper.ConfigurationProvider) .ToListAsync(cancellationToken); var result = await _excelService.ExportAsync(data, new Dictionary>() { - //{ _localizer["Id"], item => item.Id }, + { _localizer["Product Name"], item => item.Name }, + { _localizer["Description"], item => item.Description }, + { _localizer["Price of unit"], item => item.Price }, + { _localizer["Unit"], item => item.Unit }, + { _localizer["Pictures"], item => item.Pictures!=null?string.Join(",",item.Pictures):null }, } , _localizer["Products"]); return result; diff --git a/src/Application/Features/Products/Queries/Pagination/ProductsPaginationQuery.cs b/src/Application/Features/Products/Queries/Pagination/ProductsPaginationQuery.cs index a471186eb..3bf897a9a 100644 --- a/src/Application/Features/Products/Queries/Pagination/ProductsPaginationQuery.cs +++ b/src/Application/Features/Products/Queries/Pagination/ProductsPaginationQuery.cs @@ -30,7 +30,7 @@ IStringLocalizer localizer public async Task> Handle(ProductsWithPaginationQuery request, CancellationToken cancellationToken) { - var data = await _context.Products.Where(x=>x.Name.Contains(request.Keyword)) + var data = await _context.Products.Where(x=>x.Name.Contains(request.Keyword) || x.Description.Contains(request.Keyword)) .OrderBy($"{request.OrderBy} {request.SortDirection}") .ProjectTo(_mapper.ConfigurationProvider) .PaginatedDataAsync(request.PageNumber, request.PageSize); diff --git a/src/Blazor.Server.UI/Pages/Products/Products.razor b/src/Blazor.Server.UI/Pages/Products/Products.razor index bf30f880c..4684e2f3f 100644 --- a/src/Blazor.Server.UI/Pages/Products/Products.razor +++ b/src/Blazor.Server.UI/Pages/Products/Products.razor @@ -1,9 +1,13 @@ @page "/pages/products" @using Blazor.Server.UI.Components.Dialogs @using CleanArchitecture.Blazor.Application.Features.Products.Commands.Delete +@using CleanArchitecture.Blazor.Application.Features.Products.Commands.Import @using CleanArchitecture.Blazor.Application.Features.Products.DTOs +@using CleanArchitecture.Blazor.Application.Features.Products.Queries.Export @using CleanArchitecture.Blazor.Application.Features.Products.Queries.Pagination @using CleanArchitecture.Blazor.Application.Features.Products.Commands.AddEdit + +@inject IJSRuntime JS @inject IStringLocalizer L @Title +
- Product - @L["Refresh"] - @if (_canCreate) - { - @L["Create"] - } - @if (_canDelete) - { - @L["Delete"] - } - @if (_canImport) - { - @L["Import Data"] - } - @if (_canExport) - { - @L["Export Data"] - } -
+ Product + @L["Refresh"] + @if (_canCreate) + { + @L["Create"] + } + @if (_canDelete) + { + @L["Delete"] + } + @if (_canImport) + { +