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 (customer service)

KrzysztofPajak3a5df5e4a6ab
diff --git a/src/Business/Grand.Business.Customers/Queries/Handlers/GetCustomerQueryHandler.cs b/src/Business/Grand.Business.Customers/Queries/Handlers/GetCustomerQueryHandler.cs
index c7e633500..f0464a521 100644
--- a/src/Business/Grand.Business.Customers/Queries/Handlers/GetCustomerQueryHandler.cs
+++ b/src/Business/Grand.Business.Customers/Queries/Handlers/GetCustomerQueryHandler.cs
@@ -2,7 +2,6 @@
using Grand.Domain.Customers;
using Grand.Domain.Data;
using MediatR;
-using MongoDB.Driver.Linq;
using System;
using System.Linq;
using System.Threading;
@@ -10,7 +9,7 @@ using System.Threading.Tasks;
namespace Grand.Business.Customers.Queries.Handlers
{
- public class GetCustomerQueryHandler : IRequestHandler<GetCustomerQuery, IMongoQueryable<Customer>>
+ public class GetCustomerQueryHandler : IRequestHandler<GetCustomerQuery, IQueryable<Customer>>
{
private readonly IRepository<Customer> _customerRepository;
@@ -18,9 +17,10 @@ namespace Grand.Business.Customers.Queries.Handlers
{
_customerRepository = customerRepository;
}
- public Task<IMongoQueryable<Customer>> Handle(GetCustomerQuery request, CancellationToken cancellationToken)
+ public Task<IQueryable<Customer>> Handle(GetCustomerQuery request, CancellationToken cancellationToken)
{
- var query = _customerRepository.Table;
+ var query = from p in _customerRepository.Table
+ select p;
if (request.CreatedFromUtc.HasValue)
query = query.Where(c => request.CreatedFromUtc.Value <= c.CreatedOnUtc);
diff --git a/src/Business/Grand.Business.Customers/Queries/Models/GetCustomerQuery.cs b/src/Business/Grand.Business.Customers/Queries/Models/GetCustomerQuery.cs
index 0a671a6ae..94279beaf 100644
--- a/src/Business/Grand.Business.Customers/Queries/Models/GetCustomerQuery.cs
+++ b/src/Business/Grand.Business.Customers/Queries/Models/GetCustomerQuery.cs
@@ -1,13 +1,13 @@
using Grand.Domain.Customers;
using Grand.Domain.Orders;
using MediatR;
-using MongoDB.Driver.Linq;
using System;
+using System.Linq;
using System.Linq.Expressions;
namespace Grand.Business.Customers.Queries.Models
{
- public class GetCustomerQuery : IRequest<IMongoQueryable<Customer>>
+ public class GetCustomerQuery : IRequest<IQueryable<Customer>>
{
public DateTime? CreatedFromUtc { get; set; } = null;
diff --git a/src/Business/Grand.Business.Customers/Services/AffiliateService.cs b/src/Business/Grand.Business.Customers/Services/AffiliateService.cs
index e502d32ca..846d09318 100644
--- a/src/Business/Grand.Business.Customers/Services/AffiliateService.cs
+++ b/src/Business/Grand.Business.Customers/Services/AffiliateService.cs
@@ -5,7 +5,6 @@ using Grand.Domain.Affiliates;
using Grand.Domain.Data;
using Grand.Domain.Orders;
using MediatR;
-using MongoDB.Driver.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
@@ -67,7 +66,7 @@ namespace Grand.Business.Customers.Services
orderby a.Id
where a.FriendlyUrlName.Contains(friendlyUrlName.ToLowerInvariant())
select a;
- var affiliate = query.FirstOrDefaultAsync();
+ var affiliate = query.FirstOrDefaultAsync2();
return affiliate;
}
@@ -82,7 +81,8 @@ namespace Grand.Business.Customers.Services
int pageIndex = 0, int pageSize = int.MaxValue,
bool showHidden = false)
{
- var query = _affiliateRepository.Table;
+ var query = from p in _affiliateRepository.Table
+ select p;
if (!String.IsNullOrWhiteSpace(friendlyUrlName))
query = query.Where(a => a.FriendlyUrlName != null && a.FriendlyUrlName.Contains(friendlyUrlName.ToLowerInvariant()));
@@ -95,7 +95,9 @@ namespace Grand.Business.Customers.Services
if (loadOnlyWithOrders)
{
- var ordersQuery = _orderRepository.Table;
+ var ordersQuery = from p in _orderRepository.Table
+ select p;
+
if (ordersCreatedFromUtc.HasValue)
ordersQuery = ordersQuery.Where(o => ordersCreatedFromUtc.Value <= o.CreatedOnUtc);
if (ordersCreatedToUtc.HasValue)
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerAttributeService.cs b/src/Business/Grand.Business.Customers/Services/CustomerAttributeService.cs
index c51dfb6fd..482dba433 100644
--- a/src/Business/Grand.Business.Customers/Services/CustomerAttributeService.cs
+++ b/src/Business/Grand.Business.Customers/Services/CustomerAttributeService.cs
@@ -5,9 +5,6 @@ using Grand.Infrastructure.Extensions;
using Grand.Domain.Customers;
using Grand.Domain.Data;
using MediatR;
-using MongoDB.Bson;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -61,7 +58,7 @@ namespace Grand.Business.Customers.Services
var query = from ca in _customerAttributeRepository.Table
orderby ca.DisplayOrder
select ca;
- return query.ToListAsync();
+ return query.ToListAsync2();
});
}
@@ -140,9 +137,10 @@ namespace Grand.Business.Customers.Services
if (customerAttributeValue == null)
throw new ArgumentNullException(nameof(customerAttributeValue));
- var updatebuilder = Builders<CustomerAttribute>.Update;
- var update = updatebuilder.AddToSet(p => p.CustomerAttributeValues, customerAttributeValue);
- await _customerAttributeRepository.Collection.UpdateOneAsync(new BsonDocument("_id", customerAttributeValue.CustomerAttributeId), update);
+ var ca = await _customerAttributeRepository.GetByIdAsync(customerAttributeValue.CustomerAttributeId);
+ ca.CustomerAttributeValues.Add(customerAttributeValue);
+
+ await _customerAttributeRepository.UpdateAsync(ca);
await _cacheBase.RemoveByPrefix(CacheKey.CUSTOMERATTRIBUTES_PATTERN_KEY);
await _cacheBase.RemoveByPrefix(CacheKey.CUSTOMERATTRIBUTEVALUES_PATTERN_KEY);
@@ -160,16 +158,11 @@ namespace Grand.Business.Customers.Services
if (customerAttributeValue == null)
throw new ArgumentNullException(nameof(customerAttributeValue));
- var builder = Builders<CustomerAttribute>.Filter;
- var filter = builder.Eq(x => x.Id, customerAttributeValue.CustomerAttributeId);
- filter = filter & builder.ElemMatch(x => x.CustomerAttributeValues, y => y.Id == customerAttributeValue.Id);
- var update = Builders<CustomerAttribute>.Update
- .Set(x => x.CustomerAttributeValues.ElementAt(-1).DisplayOrder, customerAttributeValue.DisplayOrder)
- .Set(x => x.CustomerAttributeValues.ElementAt(-1).IsPreSelected, customerAttributeValue.IsPreSelected)
- .Set(x => x.CustomerAttributeValues.ElementAt(-1).Locales, customerAttributeValue.Locales)
- .Set(x => x.CustomerAttributeValues.ElementAt(-1).Name, customerAttributeValue.Name);
+ var ca = await _customerAttributeRepository.GetByIdAsync(customerAttributeValue.CustomerAttributeId);
+ ca.CustomerAttributeValues.Remove(ca.CustomerAttributeValues.FirstOrDefault(c => c.Id == customerAttributeValue.Id));
+ ca.CustomerAttributeValues.Add(customerAttributeValue);
- await _customerAttributeRepository.Collection.UpdateManyAsync(filter, update);
+ await _customerAttributeRepository.UpdateAsync(ca);
await _cacheBase.RemoveByPrefix(CacheKey.CUSTOMERATTRIBUTES_PATTERN_KEY);
await _cacheBase.RemoveByPrefix(CacheKey.CUSTOMERATTRIBUTEVALUES_PATTERN_KEY);
@@ -187,9 +180,9 @@ namespace Grand.Business.Customers.Services
if (customerAttributeValue == null)
throw new ArgumentNullException(nameof(customerAttributeValue));
- var updatebuilder = Builders<CustomerAttribute>.Update;
- var update = updatebuilder.Pull(p => p.CustomerAttributeValues, customerAttributeValue);
- await _customerAttributeRepository.Collection.UpdateOneAsync(new BsonDocument("_id", customerAttributeValue.CustomerAttributeId), update);
+ var ca = await _customerAttributeRepository.GetByIdAsync(customerAttributeValue.CustomerAttributeId);
+ ca.CustomerAttributeValues.Remove(ca.CustomerAttributeValues.FirstOrDefault(c => c.Id == customerAttributeValue.Id));
+ await _customerAttributeRepository.UpdateAsync(ca);
await _cacheBase.RemoveByPrefix(CacheKey.CUSTOMERATTRIBUTES_PATTERN_KEY);
await _cacheBase.RemoveByPrefix(CacheKey.CUSTOMERATTRIBUTEVALUES_PATTERN_KEY);
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerHistoryPasswordService.cs b/src/Business/Grand.Business.Customers/Services/CustomerHistoryPasswordService.cs
index d4d470420..8a2743077 100644
--- a/src/Business/Grand.Business.Customers/Services/CustomerHistoryPasswordService.cs
+++ b/src/Business/Grand.Business.Customers/Services/CustomerHistoryPasswordService.cs
@@ -3,10 +3,10 @@ using Grand.Domain.Customers;
using Grand.Domain.Data;
using Grand.Infrastructure.Extensions;
using MediatR;
-using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
+using System.Linq;
namespace Grand.Business.Customers.Services
{
@@ -54,11 +54,10 @@ namespace Grand.Business.Customers.Services
/// <returns>List of customer passwords</returns>
public virtual async Task<IList<CustomerHistoryPassword>> GetPasswords(string customerId, int passwordsToReturn)
{
- var filter = Builders<CustomerHistoryPassword>.Filter.Eq(x => x.CustomerId, customerId);
- return await _customerHistoryPasswordProductRepository.Collection.Find(filter)
- .SortByDescending(password => password.CreatedOnUtc)
- .Limit(passwordsToReturn)
- .ToListAsync();
+ return await _customerHistoryPasswordProductRepository.Table.Where(x=>x.CustomerId == customerId)
+ .OrderByDescending(password => password.CreatedOnUtc)
+ .Take(passwordsToReturn)
+ .ToListAsync2();
}
}
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerNoteService.cs b/src/Business/Grand.Business.Customers/Services/CustomerNoteService.cs
index ba776d3e8..0f68122cb 100644
--- a/src/Business/Grand.Business.Customers/Services/CustomerNoteService.cs
+++ b/src/Business/Grand.Business.Customers/Services/CustomerNoteService.cs
@@ -3,8 +3,6 @@ using Grand.Domain.Customers;
using Grand.Domain.Data;
using Grand.Infrastructure.Extensions;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -36,7 +34,7 @@ namespace Grand.Business.Customers.Services
if (string.IsNullOrWhiteSpace(id))
return Task.FromResult<CustomerNote>(null);
- return _customerNoteRepository.Table.Where(x => x.Id == id).FirstOrDefaultAsync();
+ return _customerNoteRepository.Table.Where(x => x.Id == id).FirstOrDefaultAsync2();
}
/// <summary>
@@ -86,7 +84,7 @@ namespace Grand.Business.Customers.Services
query = query.OrderByDescending(x => x.CreatedOnUtc);
- return await query.ToListAsync();
+ return await query.ToListAsync2();
}
#endregion
diff --git a/src/Business/Grand.Business.Customers/Services/CustomerService.cs b/src/Business/Grand.Business.Customers/Services/CustomerService.cs
index 9d23febfa..ffd984ce5 100644
--- a/src/Business/Grand.Business.Customers/Services/CustomerService.cs
+++ b/src/Business/Grand.Business.Customers/Services/CustomerService.cs
@@ -11,14 +11,13 @@ using Grand.Domain.Stores;
using Grand.Infrastructure.Extensions;
using Grand.SharedKernel;
using MediatR;
-using MongoDB.Bson;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
+using MongoDB.Driver;
+using MongoDB.Bson;
namespace Grand.Business.Customers.Services
{
@@ -121,10 +120,12 @@ namespace Grand.Business.Customers.Services
/// <returns>Customers</returns>
public virtual async Task<IList<Customer>> GetAllCustomersByPasswordFormat(PasswordFormat passwordFormat)
{
- var query = _customerRepository.Table;
+ var query = from p in _customerRepository.Table
+ select p;
+
query = query.Where(c => c.PasswordFormatId == passwordFormat);
query = query.OrderByDescending(c => c.CreatedOnUtc);
- return await query.ToListAsync();
+ return await query.ToListAsync2();
}
/// <summary>
@@ -141,7 +142,9 @@ namespace Grand.Business.Customers.Services
public virtual async Task<IPagedList<Customer>> GetOnlineCustomers(DateTime lastActivityFromUtc,
string[] customerGroupIds, string storeId = "", string salesEmployeeId = "", int pageIndex = 0, int pageSize = int.MaxValue)
{
- var query = _customerRepository.Table;
+ var query = from p in _customerRepository.Table
+ select p;
+
query = query.Where(c => lastActivityFromUtc <= c.LastActivityDateUtc);
query = query.Where(c => !c.Deleted);
@@ -167,7 +170,9 @@ namespace Grand.Business.Customers.Services
/// <returns>Int</returns>
public virtual Task<int> GetCountOnlineShoppingCart(DateTime lastActivityFromUtc, string storeId = "", string salesEmployeeId = "")
{
- var query = _customerRepository.Table;
+ var query = from p in _customerRepository.Table
+ select p;
+
query = query.Where(c => c.Active);
query = query.Where(c => lastActivityFromUtc <= c.LastUpdateCartDateUtc);
query = query.Where(c => c.ShoppingCartItems.Any(y => y.ShoppingCartTypeId == ShoppingCartType.ShoppingCart));
@@ -178,7 +183,7 @@ namespace Grand.Business.Customers.Services
if (!string.IsNullOrEmpty(salesEmployeeId))
query = query.Where(c => c.SeId == salesEmployeeId);
- return query.CountAsync();
+ return query.CountAsync2();
}
/// <summary>
@@ -207,7 +212,7 @@ namespace Grand.Business.Customers.Services
var query = from c in _customerRepository.Table
where customerIds.Contains(c.Id)
select c;
- var customers = await query.ToListAsync();
+ var customers = await query.ToListAsync2();
//sort by passed identifiers
var sortedCustomers = new List<Customer>();
foreach (var id in customerIds)
@@ -226,8 +231,7 @@ namespace Grand.Business.Customers.Services
/// <returns>A customer</returns>
public virtual Task<Customer> GetCustomerByGuid(Guid customerGuid)
{
- var filter = Builders<Customer>.Filter.Eq(x => x.CustomerGuid, customerGuid);
- return _customerRepository.Collection.Find(filter).FirstOrDefaultAsync();
+ return _customerRepository.Table.Where(x => x.CustomerGuid == customerGuid).FirstOrDefaultAsync2();
}
/// <summary>
@@ -240,8 +244,7 @@ namespace Grand.Business.Customers.Services
if (string.IsNullOrWhiteSpace(email))
return Task.FromResult<Customer>(null);
- var filter = Builders<Customer>.Filter.Eq(x => x.Email, email.ToLowerInvariant());
- return _customerRepository.Collection.Find(filter).FirstOrDefaultAsync();
+ return _customerRepository.Table.Where(x => x.Email == email.ToLowerInvariant()).FirstOrDefaultAsync2();
}
/// <summary>
@@ -254,8 +257,7 @@ namespace Grand.Business.Customers.Services
if (string.IsNullOrWhiteSpace(systemName))
return Task.FromResult<Customer>(null);
- var filter = Builders<Customer>.Filter.Eq(x => x.SystemName, systemName);
- return _customerRepository.Collection.Find(filter).FirstOrDefaultAsync();
+ return _customerRepository.Table.Where(x => x.SystemName == systemName).FirstOrDefaultAsync2();
}
/// <summary>
@@ -268,8 +270,7 @@ namespace Grand.Business.Customers.Services
if (string.IsNullOrWhiteSpace(username))
return Task.FromResult<Customer>(null);
- var filter = Builders<Customer>.Filter.Eq(x => x.Username, username.ToLowerInvariant());
- return _customerRepository.Collection.Find(filter).FirstOrDefaultAsync();
+ return _customerRepository.Table.Where(x => x.Username == username.ToLowerInvariant()).FirstOrDefaultAsync2();
}
/// <summary>
diff --git a/src/Business/Grand.Business.Customers/Services/SalesEmployeeService.cs b/src/Business/Grand.Business.Customers/Services/SalesEmployeeService.cs
index 9126ac2fe..4bb89e949 100644
--- a/src/Business/Grand.Business.Customers/Services/SalesEmployeeService.cs
+++ b/src/Business/Grand.Business.Customers/Services/SalesEmployeeService.cs
@@ -5,8 +5,6 @@ using Grand.Infrastructure.Extensions;
using Grand.Domain.Customers;
using Grand.Domain.Data;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -56,7 +54,7 @@ namespace Grand.Business.Customers.Services
var query = from se in _salesEmployeeRepository.Table
orderby se.DisplayOrder
select se;
- return query.ToListAsync();
+ return query.ToListAsync2();
});
}
diff --git a/src/Business/Grand.Business.Customers/Services/UserApiService.cs b/src/Business/Grand.Business.Customers/Services/UserApiService.cs
index 70ce3163a..17f1791a4 100644
--- a/src/Business/Grand.Business.Customers/Services/UserApiService.cs
+++ b/src/Business/Grand.Business.Customers/Services/UserApiService.cs
@@ -4,7 +4,6 @@ using Grand.Domain;
using Grand.Domain.Customers;
using Grand.Domain.Data;
using MediatR;
-using MongoDB.Driver.Linq;
using System.Linq;
using System.Threading.Tasks;
@@ -39,7 +38,7 @@ namespace Grand.Business.Customers.Services
/// <param name="id">id</param>
public virtual Task<UserApi> GetUserByEmail(string email)
{
- return _userRepository.Table.Where(x => x.Email == email.ToLowerInvariant()).FirstOrDefaultAsync();
+ return _userRepository.Table.Where(x => x.Email == email.ToLowerInvariant()).FirstOrDefaultAsync2();
}
/// <summary>
@@ -88,7 +87,9 @@ namespace Grand.Business.Customers.Services
/// <returns>PagedList<UserApi></returns>
public virtual async Task<IPagedList<UserApi>> GetUsers(string email = "", int pageIndex = 0, int pageSize = 2147483647)
{
- var query = _userRepository.Table;
+ var query = from p in _userRepository.Table
+ select p;
+
if (!string.IsNullOrEmpty(email))
query = query.Where(x => x.Email.Contains(email.ToLowerInvariant()));
diff --git a/src/Business/Grand.Business.Customers/Services/VendorService.cs b/src/Business/Grand.Business.Customers/Services/VendorService.cs
index bc1eeeafa..1be9070b3 100644
--- a/src/Business/Grand.Business.Customers/Services/VendorService.cs
+++ b/src/Business/Grand.Business.Customers/Services/VendorService.cs
@@ -4,13 +4,12 @@ using Grand.Domain;
using Grand.Domain.Data;
using Grand.Domain.Vendors;
using MediatR;
-using MongoDB.Bson;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
+using MongoDB.Driver;
+using MongoDB.Bson;
namespace Grand.Business.Customers.Services
{
@@ -67,7 +66,8 @@ namespace Grand.Business.Customers.Services
public virtual async Task<IPagedList<Vendor>> GetAllVendors(string name = "",
int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false)
{
- var query = _vendorRepository.Table;
+ var query = from p in _vendorRepository.Table
+ select p;
if (!String.IsNullOrWhiteSpace(name))
query = query.Where(v => v.Name.ToLower().Contains(name.ToLower()));
@@ -184,7 +184,7 @@ namespace Grand.Business.Customers.Services
var query = from c in _vendorRepository.Table
where c.AppliedDiscounts.Any(x => x == discountId)
select c;
- return await query.ToListAsync();
+ return await query.ToListAsync2();
}
#region Vendor reviews
diff --git a/src/Core/Grand.Domain/Data/RepositoryExtensions.cs b/src/Core/Grand.Domain/Data/RepositoryExtensions.cs
index 96889191e..063e40717 100644
--- a/src/Core/Grand.Domain/Data/RepositoryExtensions.cs
+++ b/src/Core/Grand.Domain/Data/RepositoryExtensions.cs
@@ -32,6 +32,9 @@ namespace Grand.Domain.Data
return ((IMongoQueryable<T>)query).FirstOrDefaultAsync();
}
-
+ public static Task<int> CountAsync2<T>(this IQueryable<T> query)
+ {
+ return ((IMongoQueryable<T>)query).CountAsync();
+ }
}
}