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.

Extend Repository implementation - add new methods GetAllAsync/FirstOrDefaultAsync

KrzysztofPajakb2f214e37586
diff --git a/src/Core/Grand.Domain/Data/IRepository.cs b/src/Core/Grand.Domain/Data/IRepository.cs
index a0b339e94..d13bbd770 100644
--- a/src/Core/Grand.Domain/Data/IRepository.cs
+++ b/src/Core/Grand.Domain/Data/IRepository.cs
@@ -35,6 +35,19 @@ namespace Grand.Domain.Data
/// <returns>Entity</returns>
Task<T> GetByIdAsync(string id);
+ /// <summary>
+ /// Get all entities in collection
+ /// </summary>
+ /// <returns>collection of entities</returns>
+ Task<List<T>> GetAllAsync();
+
+ /// <summary>
+ /// Get first entity in query as async with filterdefinition
+ /// </summary>
+ /// <param name="filter">filter definition</param>
+ /// <returns>entity of <typeparamref name="T"/></returns>
+ Task<T> FirstOrDefaultAsync(FilterDefinition<T> filter = null);
+
/// <summary>
/// Insert entity
/// </summary>
diff --git a/src/Core/Grand.Domain/Data/Repository.cs b/src/Core/Grand.Domain/Data/Repository.cs
index c1ecca01f..5de12d884 100644
--- a/src/Core/Grand.Domain/Data/Repository.cs
+++ b/src/Core/Grand.Domain/Data/Repository.cs
@@ -77,9 +77,8 @@ namespace Grand.Domain.Data
{
return _collection.Find(e => e.Id == id).FirstOrDefault();
}
-
/// <summary>
- /// Get entity by identifier async
+ /// Get async entity by identifier
/// </summary>
/// <param name="id">Identifier</param>
/// <returns>Entity</returns>
@@ -87,6 +86,29 @@ namespace Grand.Domain.Data
{
return _collection.Find(e => e.Id == id).FirstOrDefaultAsync();
}
+
+ /// <summary>
+ /// Get all entities in collection
+ /// </summary>
+ /// <returns>collection of entities</returns>
+ public virtual Task<List<T>> GetAllAsync()
+ {
+ return _collection.AsQueryable().ToListAsync();
+ }
+
+ /// <summary>
+ /// get first item in query as async with filterdefinition
+ /// </summary>
+ /// <param name="filter"></param>
+ /// <returns>entity of <typeparamref name="T"/></returns>
+ public virtual Task<T> FirstOrDefaultAsync(FilterDefinition<T> filter = null)
+ {
+ if (filter == null)
+ return _collection.AsQueryable().FirstOrDefaultAsync();
+
+ return _collection.Find(filter).FirstOrDefaultAsync();
+ }
+
/// <summary>
/// Insert entity
/// </summary>
diff --git a/src/Web/Grand.Web.Common/Middleware/DbVersionCheckMiddleware.cs b/src/Web/Grand.Web.Common/Middleware/DbVersionCheckMiddleware.cs
index d666e80ec..ea8fe326b 100644
--- a/src/Web/Grand.Web.Common/Middleware/DbVersionCheckMiddleware.cs
+++ b/src/Web/Grand.Web.Common/Middleware/DbVersionCheckMiddleware.cs
@@ -4,8 +4,6 @@ using Grand.Infrastructure;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Microsoft.AspNetCore.Http;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System.Threading.Tasks;
namespace Grand.Web.Common.Middleware
@@ -27,7 +25,7 @@ namespace Grand.Web.Common.Middleware
return;
}
- var version = await cacheBase.GetAsync(CacheKey.GRAND_NODE_VERSION, () => repository.Table.FirstOrDefaultAsync());
+ var version = await cacheBase.GetAsync(CacheKey.GRAND_NODE_VERSION, async () => await repository.FirstOrDefaultAsync());
if (version == null)
{
await context.Response.WriteAsync($"The database does not exist.");