Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Further changes with export data

support5556e6a1321a
diff --git a/src/Business/Grand.Business.Common/Services/ExportImport/ExcelExportProvider.cs b/src/Business/Grand.Business.Common/Services/ExportImport/ExcelExportProvider.cs
index dde72f014..9b9d2fac8 100644
--- a/src/Business/Grand.Business.Common/Services/ExportImport/ExcelExportProvider.cs
+++ b/src/Business/Grand.Business.Common/Services/ExportImport/ExcelExportProvider.cs
@@ -29,6 +29,33 @@ namespace Grand.Business.Common.Services.ExportImport
}
}
+ private MemoryStream stream;
+ private IWorkbook xlPackage;
+ public virtual IExportProvider BuilderExportToByte<T>(PropertyByName<T>[] properties, IEnumerable<T> itemsToExport)
+ {
+ if (stream == null)
+ stream = new MemoryStream();
+
+ if (xlPackage == null)
+ xlPackage = new XSSFWorkbook();
+
+ ISheet worksheet = xlPackage.CreateSheet(typeof(T).Name);
+ var manager = new PropertyManager<T>(properties);
+ WriteCaption(worksheet, properties);
+ var row = 1;
+ foreach (var items in itemsToExport)
+ {
+ WriteToXlsx(properties, worksheet, items, row++);
+ }
+
+ return this;
+ }
+ public virtual byte[] BuilderExportToByte()
+ {
+ xlPackage.Write(stream);
+ return stream.ToArray();
+ }
+
/// <summary>
/// Write caption (first row) to XLSX worksheet
/// </summary>
diff --git a/src/Business/Grand.Business.Core/Interfaces/ExportImport/IExportProvider.cs b/src/Business/Grand.Business.Core/Interfaces/ExportImport/IExportProvider.cs
index 7e282581d..0cf73b320 100644
--- a/src/Business/Grand.Business.Core/Interfaces/ExportImport/IExportProvider.cs
+++ b/src/Business/Grand.Business.Core/Interfaces/ExportImport/IExportProvider.cs
@@ -5,5 +5,7 @@ namespace Grand.Business.Core.Interfaces.ExportImport
public interface IExportProvider
{
byte[] ExportToByte<T>(PropertyByName<T>[] properties, IEnumerable<T> itemsToExport);
+ IExportProvider BuilderExportToByte<T>(PropertyByName<T>[] properties, IEnumerable<T> itemsToExport);
+ byte[] BuilderExportToByte();
}
}
diff --git a/src/Business/Grand.Business.Customers/Services/AddressSchemaProperty.cs b/src/Business/Grand.Business.Customers/Services/AddressSchemaProperty.cs
new file mode 100644
index 000000000..b5e23106b
--- /dev/null
+++ b/src/Business/Grand.Business.Customers/Services/AddressSchemaProperty.cs
@@ -0,0 +1,34 @@
+using Grand.Business.Core.Interfaces.Common.Directory;
+using Grand.Business.Core.Interfaces.ExportImport;
+using Grand.Business.Core.Utilities.ExportImport;
+using Grand.Domain.Common;
+
+namespace Grand.Business.Customers.Services
+{
+ public class AddressSchemaProperty : ISchemaProperty<Address>
+ {
+ private readonly ICountryService _countryService;
+
+ public AddressSchemaProperty(ICountryService countryService)
+ {
+ _countryService = countryService;
+ }
+
+ public virtual PropertyByName<Address>[] GetProperties()
+ {
+ var properties = new[]
+ {
+ new PropertyByName<Address>("Email", p=>p.Email),
+ new PropertyByName<Address>("FirstName", p=>p.FirstName),
+ new PropertyByName<Address>("LastName", p=>p.LastName),
+ new PropertyByName<Address>("PhoneNumber", p=>p.PhoneNumber),
+ new PropertyByName<Address>("FaxNumber", p=>p.FaxNumber),
+ new PropertyByName<Address>("Address1", p=>p.Address1),
+ new PropertyByName<Address>("Address2", p=>p.Address2),
+ new PropertyByName<Address>("City", p=>p.City),
+ new PropertyByName<Address>("Country", p=> !string.IsNullOrEmpty(p.CountryId) ? _countryService.GetCountryById(p.CountryId).Result?.Name : ""),
+ };
+ return properties;
+ }
+ }
+}
diff --git a/src/Business/Grand.Business.Customers/Startup/StartupApplication.cs b/src/Business/Grand.Business.Customers/Startup/StartupApplication.cs
index 42535533d..f05eecd3d 100644
--- a/src/Business/Grand.Business.Customers/Startup/StartupApplication.cs
+++ b/src/Business/Grand.Business.Customers/Startup/StartupApplication.cs
@@ -1,6 +1,7 @@
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Interfaces.ExportImport;
using Grand.Business.Customers.Services;
+using Grand.Domain.Common;
using Grand.Domain.Customers;
using Grand.Infrastructure;
using Microsoft.AspNetCore.Builder;
@@ -37,6 +38,7 @@ namespace Grand.Business.Customers.Startup
serviceCollection.AddScoped<IUserApiService, UserApiService>();
serviceCollection.AddScoped<IAffiliateService, AffiliateService>();
serviceCollection.AddScoped<ISchemaProperty<Customer>, CustomerSchemaProperty>();
+ serviceCollection.AddScoped<ISchemaProperty<Address>, AddressSchemaProperty>();
}
}
}
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index d6960cb3a..bc7539fd9 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -23,6 +23,7 @@ using Grand.Web.Models.Customer;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
+using Grand.Business.Core.Interfaces.ExportImport;
namespace Grand.Web.Controllers
{
@@ -694,7 +695,7 @@ namespace Grand.Web.Controllers
}
//TODO
- public virtual async Task<IActionResult> Export(/*[FromServices] IExportManager exportManager*/)
+ public virtual async Task<IActionResult> Export()
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
return Challenge();
@@ -702,9 +703,9 @@ namespace Grand.Web.Controllers
if (!_customerSettings.AllowUsersToExportData)
return Challenge();
- var customer = _workContext.CurrentCustomer;
- byte[] bytes = null; // await exportManager.ExportCustomerToXlsx(customer, _workContext.CurrentStore.Id);
- return File(bytes, "text/xls", "PersonalInfo.xlsx");
+ var model = await _mediator.Send(new GetCustomerData(_workContext.CurrentCustomer));
+
+ return File(model, "text/xls", "PersonalInfo.xlsx");
}
#endregion
diff --git a/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomerDataHandler.cs b/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomerDataHandler.cs
new file mode 100644
index 000000000..4c6b59222
--- /dev/null
+++ b/src/Web/Grand.Web/Features/Handlers/Customers/GetCustomerDataHandler.cs
@@ -0,0 +1,47 @@
+using Grand.Business.Core.Interfaces.Checkout.Orders;
+using Grand.Business.Core.Interfaces.ExportImport;
+using Grand.Domain.Common;
+using Grand.Domain.Customers;
+using Grand.Domain.Orders;
+using Grand.Web.Features.Models.Customers;
+using MediatR;
+
+namespace Grand.Web.Features.Handlers.Customers
+{
+ public class GetCustomerDataHandler : IRequestHandler<GetCustomerData, byte[]>
+ {
+ private readonly IExportProvider _exportProvider;
+ private readonly ISchemaProperty<Customer> _customerSchemaProperty;
+ private readonly ISchemaProperty<Address> _addresSchemaProperty;
+ private readonly ISchemaProperty<Order> _orderSchemaProperty;
+ private readonly IOrderService _orderService;
+
+ public GetCustomerDataHandler(
+ IExportProvider exportProvider,
+ ISchemaProperty<Customer> customerSchemaProperty,
+ ISchemaProperty<Address> addresSchemaProperty,
+ ISchemaProperty<Order> orderSchemaProperty,
+ IOrderService orderService)
+ {
+ _exportProvider = exportProvider;
+ _customerSchemaProperty = customerSchemaProperty;
+ _addresSchemaProperty = addresSchemaProperty;
+ _orderSchemaProperty = orderSchemaProperty;
+ _orderService = orderService;
+ }
+
+ public async Task<byte[]> Handle(GetCustomerData request, CancellationToken cancellationToken)
+ {
+ var orders = await _orderService.SearchOrders(
+ customerId: request.Customer.Id);
+
+ _exportProvider
+ .BuilderExportToByte(_customerSchemaProperty.GetProperties(), new List<Customer> { request.Customer })
+ .BuilderExportToByte(_addresSchemaProperty.GetProperties(), request.Customer.Addresses)
+ .BuilderExportToByte(_orderSchemaProperty.GetProperties(), orders);
+
+ return _exportProvider.BuilderExportToByte();
+ }
+
+ }
+}
diff --git a/src/Web/Grand.Web/Features/Models/Customers/GetCustomerData.cs b/src/Web/Grand.Web/Features/Models/Customers/GetCustomerData.cs
new file mode 100644
index 000000000..3cbb757d1
--- /dev/null
+++ b/src/Web/Grand.Web/Features/Models/Customers/GetCustomerData.cs
@@ -0,0 +1,7 @@
+using Grand.Domain.Customers;
+using MediatR;
+
+namespace Grand.Web.Features.Models.Customers
+{
+ public record GetCustomerData(Customer Customer) : IRequest<byte[]>;
+}