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.

Refactoring - use repository extensions

KrzysztofPajak6457905e50c8
diff --git a/src/Business/Grand.Business.Catalog/Events/Handlers/UpdateProductOnCartEventHandler.cs b/src/Business/Grand.Business.Catalog/Events/Handlers/UpdateProductOnCartEventHandler.cs
index db69a7d72..58aca5c8b 100644
--- a/src/Business/Grand.Business.Catalog/Events/Handlers/UpdateProductOnCartEventHandler.cs
+++ b/src/Business/Grand.Business.Catalog/Events/Handlers/UpdateProductOnCartEventHandler.cs
@@ -2,7 +2,6 @@
using Grand.Domain.Customers;
using Grand.Domain.Data;
using MediatR;
-using MongoDB.Driver;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -20,9 +19,8 @@ namespace Grand.Business.Catalog.Events.Handlers
public async Task Handle(UpdateProductOnCartEvent notification, CancellationToken cancellationToken)
{
- var builderCustomer = Builders<Customer>.Filter;
- var filterCustomer = builderCustomer.ElemMatch(x => x.ShoppingCartItems, y => y.ProductId == notification.Product.Id);
- await _customerRepository.Collection.Find(filterCustomer).ForEachAsync(async (cs) =>
+ var customers = await _customerRepository.Table.Where(x => x.ShoppingCartItems.Any(y => y.ProductId == notification.Product.Id)).ToListAsync2();
+ foreach (var cs in customers)
{
foreach (var item in cs.ShoppingCartItems.Where(x => x.ProductId == notification.Product.Id))
{
@@ -35,7 +33,6 @@ namespace Grand.Business.Catalog.Events.Handlers
await _customerRepository.UpdateToSet(cs.Id, x => x.ShoppingCartItems, z => z.Id, item.Id, item);
}
}
- );
}
}
}
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/ProductReviewService.cs b/src/Business/Grand.Business.Catalog/Services/Products/ProductReviewService.cs
index 042ee44fa..a5bef580d 100644
--- a/src/Business/Grand.Business.Catalog/Services/Products/ProductReviewService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Products/ProductReviewService.cs
@@ -4,8 +4,6 @@ using Grand.Domain;
using Grand.Domain.Catalog;
using Grand.Domain.Data;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
diff --git a/src/Business/Grand.Business.System/Commands/Handlers/Common/DeleteActivitylogCommandHandler.cs b/src/Business/Grand.Business.System/Commands/Handlers/Common/DeleteActivitylogCommandHandler.cs
index 97e73f6cf..bbcf97c0b 100644
--- a/src/Business/Grand.Business.System/Commands/Handlers/Common/DeleteActivitylogCommandHandler.cs
+++ b/src/Business/Grand.Business.System/Commands/Handlers/Common/DeleteActivitylogCommandHandler.cs
@@ -2,7 +2,6 @@
using Grand.Domain.Data;
using Grand.Domain.Logging;
using MediatR;
-using MongoDB.Bson;
using System.Threading;
using System.Threading.Tasks;
diff --git a/src/Business/Grand.Business.System/Services/Reports/CustomerReportService.cs b/src/Business/Grand.Business.System/Services/Reports/CustomerReportService.cs
index 3b2d84c92..67ac0a30a 100644
--- a/src/Business/Grand.Business.System/Services/Reports/CustomerReportService.cs
+++ b/src/Business/Grand.Business.System/Services/Reports/CustomerReportService.cs
@@ -9,7 +9,6 @@ using Grand.Domain.Orders;
using Grand.Domain.Payments;
using Grand.Domain.Shipping;
using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -76,8 +75,10 @@ namespace Grand.Business.System.Services.Reports
DateTime? createdToUtc, int? os, PaymentStatus? ps, ShippingStatus? ss, int orderBy,
int pageIndex = 0, int pageSize = 214748364)
{
-
- var query = _orderRepository.Table;
+
+ var query = from p in _orderRepository.Table
+ select p;
+
query = query.Where(o => !o.Deleted);
if (os.HasValue)
query = query.Where(o => o.OrderStatusId == os.Value);
@@ -174,10 +175,10 @@ namespace Grand.Business.System.Services.Reports
var customergroup = await _groupService.GetCustomerGroupBySystemName(SystemCustomerGroupNames.Registered);
var customerGroupRegister = customergroup.Id;
var filter = builder.Where(o => !o.Deleted);
- filter = filter & builder.Where(o => o.CreatedOnUtc >= startTimeUtc.Value && o.CreatedOnUtc <= endTime);
- filter = filter & builder.Where(o => o.Groups.Any(y => y == customerGroupRegister));
+ filter &= builder.Where(o => o.CreatedOnUtc >= startTimeUtc.Value && o.CreatedOnUtc <= endTime);
+ filter &= builder.Where(o => o.Groups.Any(y => y == customerGroupRegister));
if (!string.IsNullOrEmpty(storeId))
- filter = filter & builder.Where(o => o.StoreId == storeId);
+ filter &= builder.Where(o => o.StoreId == storeId);
var daydiff = (endTimeUtc.Value - startTimeUtc.Value).TotalDays;
if (daydiff > 31)
diff --git a/src/Business/Grand.Business.System/Services/Reports/OrderReportService.cs b/src/Business/Grand.Business.System/Services/Reports/OrderReportService.cs
index b1550c263..57a7f609d 100644
--- a/src/Business/Grand.Business.System/Services/Reports/OrderReportService.cs
+++ b/src/Business/Grand.Business.System/Services/Reports/OrderReportService.cs
@@ -9,7 +9,6 @@ using Grand.Domain.Payments;
using Grand.Domain.Shipping;
using MongoDB.Bson;
using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
@@ -69,7 +68,8 @@ namespace Grand.Business.System.Services.Reports
public virtual async Task<IList<OrderByCountryReportLine>> GetCountryReport(string storeId, int? os,
PaymentStatus? ps, ShippingStatus? ss, DateTime? startTimeUtc, DateTime? endTimeUtc)
{
- var query = _orderRepository.Table;
+ var query = from p in _orderRepository.Table
+ select p;
query = query.Where(o => !o.Deleted);
if (!String.IsNullOrEmpty(storeId))
@@ -102,7 +102,7 @@ namespace Grand.Business.System.Services.Reports
SumOrders = r.SumOrders
});
- return await report.ToListAsync();
+ return await report.ToListAsync2();
}
@@ -467,7 +467,7 @@ namespace Grand.Business.System.Services.Reports
&& (string.IsNullOrEmpty(salesEmployeeId) || o.SeId == salesEmployeeId)
group o by 1 into g
select new ReportPeriodOrder() { Amount = g.Sum(x => x.OrderTotal / x.CurrencyRate), Count = g.Count() };
- var report = (await query.ToListAsync())?.FirstOrDefault();
+ var report = (await query.ToListAsync2())?.FirstOrDefault();
if (report == null)
report = new ReportPeriodOrder();
report.Date = date;
@@ -499,7 +499,7 @@ namespace Grand.Business.System.Services.Reports
if (recordsToReturn > 0)
product = product.Take(recordsToReturn);
- var report = await product.ToListAsync();
+ var report = await product.ToListAsync2();
var ids = new List<string>();
foreach (var reportLine in report)
ids.Add(reportLine.ProductId);
@@ -533,7 +533,7 @@ namespace Grand.Business.System.Services.Reports
(createdToUtc.Value >= order.CreatedOnUtc) &&
(!order.Deleted)
from orderItem in order.OrderItems
- select new { orderItem.ProductId }).ToListAsync()).Distinct().Select(x => x.ProductId);
+ select new { orderItem.ProductId }).ToListAsync2()).Distinct().Select(x => x.ProductId);
var qproducts = from p in _productRepository.Table
orderby p.Name
@@ -572,7 +572,8 @@ namespace Grand.Business.System.Services.Reports
DateTime? startTimeUtc = null, DateTime? endTimeUtc = null,
string billingEmail = null, string billingLastName = "", string tagid = null)
{
- var query = _orderRepository.Table;
+ var query = from p in _orderRepository.Table
+ select p;
query = query.Where(o => !o.Deleted);
@@ -626,7 +627,7 @@ namespace Grand.Business.System.Services.Reports
select p;
- var productCost = await query2.SumAsync(orderItem => orderItem.OriginalProductCost * orderItem.Quantity);
+ var productCost = query2.Sum(orderItem => orderItem.OriginalProductCost * orderItem.Quantity);
var reportSummary = await GetOrderAverageReportLine(
storeId: storeId,
diff --git a/src/Business/Grand.Business.System/Services/Reports/ProductsReportService.cs b/src/Business/Grand.Business.System/Services/Reports/ProductsReportService.cs
index 4e9ae8642..ee6e24e61 100644
--- a/src/Business/Grand.Business.System/Services/Reports/ProductsReportService.cs
+++ b/src/Business/Grand.Business.System/Services/Reports/ProductsReportService.cs
@@ -2,10 +2,9 @@
using Grand.Business.System.Utilities;
using Grand.Domain.Catalog;
using Grand.Domain.Data;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
+using System.Linq;
namespace Grand.Business.System.Services.Reports
{
@@ -51,7 +50,7 @@ namespace Grand.Business.System.Services.Reports
if (!string.IsNullOrEmpty(storeId))
query_simple_products = query_simple_products.Where(x => x.Stores.Contains(storeId));
- var products = await query_simple_products.ToListAsync();
+ var products = await query_simple_products.ToListAsync2();
//Track inventory for product by product attributes
var query2_1 = from p in _productRepository.Table
@@ -78,7 +77,7 @@ namespace Grand.Business.System.Services.Reports
where c.StockQuantity <= 0
select c;
- var combinations = await query2_2.ToListAsync();
+ var combinations = await query2_2.ToListAsync2();
return (products, combinations);
}