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.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Refactoring - use repository extensions
KrzysztofPajakbb913862885c
diff --git a/src/Business/Grand.Business.Catalog/Services/Products/ProductTagService.cs b/src/Business/Grand.Business.Catalog/Services/Products/ProductTagService.csindex 3aab2ced6..81ee9391f 100644--- a/src/Business/Grand.Business.Catalog/Services/Products/ProductTagService.cs+++ b/src/Business/Grand.Business.Catalog/Services/Products/ProductTagService.cs@@ -166,14 +166,8 @@ namespace Grand.Business.Catalog.Services.Productsawait _productTagRepository.UpdateAsync(productTag);- //update name on products- var filter = new MongoDB.Bson.BsonDocument- {- new MongoDB.Bson.BsonElement("ProductTags", previouse.Name)- };- var update = MongoDB.Driver.Builders<Product>.Update- .Set(x => x.ProductTags.ElementAt(-1), productTag.Name);- await _productRepository.Collection.UpdateManyAsync(filter, update);+ //update on products+ await _productRepository.UpdateToSet(x => x.ProductTags, previouse.Name, productTag.Name, true);//cacheawait _cacheBase.RemoveByPrefix(CacheKey.PRODUCTTAG_PATTERN_KEY);diff --git a/src/Business/Grand.Business.Checkout/Events/Orders/OrderDeletedEventHandler.cs b/src/Business/Grand.Business.Checkout/Events/Orders/OrderDeletedEventHandler.csindex e21f8411a..c8fd65d2f 100644--- a/src/Business/Grand.Business.Checkout/Events/Orders/OrderDeletedEventHandler.cs+++ b/src/Business/Grand.Business.Checkout/Events/Orders/OrderDeletedEventHandler.cs@@ -18,9 +18,7 @@ namespace Grand.Business.Checkout.Events.Orderspublic Task Handle(OrderDeletedEvent notification, CancellationToken cancellationToken){//delete product also purchased- var filters = MongoDB.Driver.Builders<ProductAlsoPurchased>.Filter;- var filter = filters.Where(x => x.OrderId == notification.Order.Id);- return _productAlsoPurchasedRepository.Collection.DeleteManyAsync(filter);+ return _productAlsoPurchasedRepository.DeleteManyAsync(x=>x.OrderId == notification.Order.Id);}}}diff --git a/src/Business/Grand.Business.Checkout/Services/Orders/OrderService.cs b/src/Business/Grand.Business.Checkout/Services/Orders/OrderService.csindex 71f7d670a..9d52e9ee2 100644--- a/src/Business/Grand.Business.Checkout/Services/Orders/OrderService.cs+++ b/src/Business/Grand.Business.Checkout/Services/Orders/OrderService.cs@@ -286,9 +286,7 @@ namespace Grand.Business.Checkout.Services.Ordersvar order = await GetOrderByOrderItemId(orderItem.Id);- var updatebuilder = MongoDB.Driver.Builders<Order>.Update;- var updatefilter = updatebuilder.PullFilter(x => x.OrderItems, y => y.Id == orderItem.Id);- await _orderRepository.Collection.UpdateOneAsync(new MongoDB.Bson.BsonDocument("_id", order.Id), updatefilter);+ await _orderRepository.PullFilter(order.Id, x => x.OrderItems, x => x.Id == orderItem.Id);//event notificationawait _mediator.EntityDeleted(orderItem);diff --git a/src/Business/Grand.Business.Customers/Services/CustomerService.cs b/src/Business/Grand.Business.Customers/Services/CustomerService.csindex e3134bc88..b186ded64 100644--- a/src/Business/Grand.Business.Customers/Services/CustomerService.cs+++ b/src/Business/Grand.Business.Customers/Services/CustomerService.cs@@ -572,24 +572,25 @@ namespace Grand.Business.Customers.Servicesif (guestGroup == null)throw new GrandException("Guests group could not be loaded");- var userGroups = new List<string> { guestGroup.Id };- var builder = MongoDB.Driver.Builders<Customer>.Filter;- var filter = builder.AnyIn(x => x.Groups, userGroups);+ var query = from p in _customerRepository.Table+ select p;++ query = query.Where(x => x.Groups.Contains(guestGroup.Id));if (createdFromUtc.HasValue)- filter &= builder.Gte(x => x.LastActivityDateUtc, createdFromUtc.Value);+ query = query.Where(x => x.LastActivityDateUtc > createdFromUtc.Value);if (createdToUtc.HasValue)- filter &= builder.Lte(x => x.LastActivityDateUtc, createdToUtc.Value);+ query = query.Where(x => x.LastActivityDateUtc < createdToUtc.Value);if (onlyWithoutShoppingCart)- filter &= builder.Size(x => x.ShoppingCartItems, 0);+ query = query.Where(x => !x.ShoppingCartItems.Any());- filter &= builder.Eq(x => x.HasContributions, false);+ query = query.Where(x => !x.HasContributions);- filter &= builder.Eq(x => x.IsSystemAccount, false);+ query = query.Where(x => !x.IsSystemAccount);- var customers = await _customerRepository.Collection.DeleteManyAsync(filter);+ var customers = await _customerRepository.DeleteAsync(query);- return (int)customers.DeletedCount;+ return customers.Count();}diff --git a/src/Business/Grand.Business.Marketing/Services/Customers/CustomerCoordinatesService.cs b/src/Business/Grand.Business.Marketing/Services/Customers/CustomerCoordinatesService.csindex bd32263f6..a87e5d8fd 100644--- a/src/Business/Grand.Business.Marketing/Services/Customers/CustomerCoordinatesService.cs+++ b/src/Business/Grand.Business.Marketing/Services/Customers/CustomerCoordinatesService.cs@@ -48,8 +48,7 @@ namespace Grand.Business.Marketing.Services.Customerspublic async Task SaveGeoCoordinate(Customer customer, double longitude, double latitude){- var coordinates = new MongoDB.Driver.GeoJsonObjectModel.GeoJson2DCoordinates(longitude, latitude);- customer.Coordinates = new Domain.Common.GeoCoordinates(coordinates.Y, coordinates.Y);+ customer.Coordinates = new Domain.Common.GeoCoordinates(longitude, latitude);//update customerawait _customerRepository.UpdateField(customer.Id, x => x.Coordinates, customer.Coordinates);diff --git a/src/Business/Grand.Business.Marketing/Services/Customers/CustomerTagService.cs b/src/Business/Grand.Business.Marketing/Services/Customers/CustomerTagService.csindex f94fb2da2..5e6551377 100644--- a/src/Business/Grand.Business.Marketing/Services/Customers/CustomerTagService.cs+++ b/src/Business/Grand.Business.Marketing/Services/Customers/CustomerTagService.cs@@ -147,9 +147,7 @@ namespace Grand.Business.Marketing.Services.Customers/// </summary>public virtual async Task InsertTagToCustomer(string customerTagId, string customerId){- var updatebuilder = MongoDB.Driver.Builders<Customer>.Update;- var update = updatebuilder.AddToSet(p => p.CustomerTags, customerTagId);- await _customerRepository.Collection.UpdateOneAsync(new MongoDB.Bson.BsonDocument("_id", customerId), update);+ await _customerRepository.AddToSet(customerId, x => x.CustomerTags, customerTagId);}/// <summary>@@ -157,9 +155,7 @@ namespace Grand.Business.Marketing.Services.Customers/// </summary>public virtual async Task DeleteTagFromCustomer(string customerTagId, string customerId){- var updatebuilder = MongoDB.Driver.Builders<Customer>.Update;- var update = updatebuilder.Pull(p => p.CustomerTags, customerTagId);- await _customerRepository.Collection.UpdateOneAsync(new MongoDB.Bson.BsonDocument("_id", customerId), update);+ await _customerRepository.Pull(customerId, x => x.CustomerTags, customerTagId);}/// <summary>diff --git a/src/Core/Grand.Domain/Data/IRepository.cs b/src/Core/Grand.Domain/Data/IRepository.csindex 6d2b214fc..653f7cd56 100644--- a/src/Core/Grand.Domain/Data/IRepository.cs+++ b/src/Core/Grand.Domain/Data/IRepository.cs@@ -168,8 +168,23 @@ namespace Grand.Domain.Data/// <param name="elemFieldMatch">Subdocument field to match</param>/// <param name="elemMatch">Subdocument ident value</param>/// <param name="value">Subdocument - to update (all values)</param>+ /// <param name="updateMany">Update many records</param>/// <returns></returns>- Task UpdateToSet<U>(string id, Expression<Func<T, IEnumerable<U>>> field, Expression<Func<U, bool>> elemFieldMatch, U value);+ Task UpdateToSet<U>(string id, Expression<Func<T, IEnumerable<U>>> field, Expression<Func<U, bool>> elemFieldMatch, U value, bool updateMany = false);++ // <summary>+ /// Update subdocuments+ /// </summary>+ /// <typeparam name="T">Document</typeparam>+ /// <typeparam name="Z">Subdocuments</typeparam>+ /// <param name="id">Ident of entitie</param>+ /// <param name="field"></param>+ /// <param name="elemFieldMatch">Subdocument field to match</param>+ /// <param name="value">Subdocument - to update (all values)</param>+ /// <param name="updateMany">Update many records</param>+ /// <returns></returns>+ Task UpdateToSet<U>(Expression<Func<T, IEnumerable<U>>> field, U elemFieldMatch, U value, bool updateMany = false);+/// <summary>/// Delete subdocumentdiff --git a/src/Core/Grand.Domain/Data/Repository.cs b/src/Core/Grand.Domain/Data/Repository.csindex 54a8a49ad..8084e89d8 100644--- a/src/Core/Grand.Domain/Data/Repository.cs+++ b/src/Core/Grand.Domain/Data/Repository.cs@@ -1,3 +1,4 @@+using MongoDB.Bson;using MongoDB.Driver;using MongoDB.Driver.Linq;using System;@@ -305,18 +306,50 @@ namespace Grand.Domain.Data/// <param name="elemFieldMatch">Subdocument field to match</param>/// <param name="elemMatch">Subdocument ident value</param>/// <param name="value">Subdocument - to update (all values)</param>- public virtual async Task UpdateToSet<U>(string id, Expression<Func<T, IEnumerable<U>>> field, Expression<Func<U, bool>> elemFieldMatch, U value)+ public virtual async Task UpdateToSet<U>(string id, Expression<Func<T, IEnumerable<U>>> field, Expression<Func<U, bool>> elemFieldMatch, U value, bool updateMany = false){- var filter = Builders<T>.Filter.Eq(x => x.Id, id)+ var filter = string.IsNullOrEmpty(id) ? Builders<T>.Filter.Where(x => true) : Builders<T>.Filter.Eq(x => x.Id, id)& Builders<T>.Filter.ElemMatch(field, elemFieldMatch);MemberExpression me = field.Body as MemberExpression;MemberInfo minfo = me.Member;var update = Builders<T>.Update.Set($"{minfo.Name}.$", value);- var result = await _collection.UpdateOneAsync(filter, update);+ if (updateMany)+ {+ var result = await _collection.UpdateManyAsync(filter, update);+ }+ else+ {+ var result = await _collection.UpdateOneAsync(filter, update);+ }+}+ // <summary>+ /// Update subdocuments+ /// </summary>+ /// <typeparam name="T">Document</typeparam>+ /// <typeparam name="Z">Subdocuments</typeparam>+ /// <param name="id">Ident of entitie</param>+ /// <param name="field"></param>+ /// <param name="elemFieldMatch">Subdocument field to match</param>+ /// <param name="value">Subdocument - to update (all values)</param>+ /// <param name="updateMany">Update many records</param>+ /// <returns></returns>+ public virtual async Task UpdateToSet<U>(Expression<Func<T, IEnumerable<U>>> field, U elemFieldMatch, U value, bool updateMany = false)+ {+ MemberExpression me = field.Body as MemberExpression;+ MemberInfo minfo = me.Member;++ var filter = new BsonDocument+ {+ new BsonElement(minfo.Name, elemFieldMatch.ToString())+ };+ var update = Builders<T>.Update.Set($"{minfo.Name}.$", value);+ await _collection.UpdateManyAsync(filter, update);++ }/// <summary>/// Delete subdocument/// </summary>