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

KrzysztofPajak4b6c24fd5740
diff --git a/src/Business/Grand.Business.Cms/Services/BlogService.cs b/src/Business/Grand.Business.Cms/Services/BlogService.cs
index d9820a121..7b79e639e 100644
--- a/src/Business/Grand.Business.Cms/Services/BlogService.cs
+++ b/src/Business/Grand.Business.Cms/Services/BlogService.cs
@@ -5,8 +5,6 @@ using Grand.Domain.Data;
using Grand.Infrastructure.Extensions;
using Grand.SharedKernel.Extensions;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -75,7 +73,8 @@ namespace Grand.Business.Cms.Services
int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false, string tag = null, string blogPostName = "", string categoryId = "")
{
- var query = _blogPostRepository.Table;
+ var query = from p in _blogPostRepository.Table
+ select p;
if (!string.IsNullOrEmpty(categoryId))
{
@@ -242,7 +241,7 @@ namespace Grand.Business.Cms.Services
where (customerId == "" || c.CustomerId == customerId) && (storeId == "" || c.StoreId == storeId)
select c;
- return await query.ToListAsync();
+ return await query.ToListAsync2();
}
/// <summary>
@@ -262,7 +261,7 @@ namespace Grand.Business.Cms.Services
orderby c.CreatedOnUtc
select c;
- return await query.ToListAsync();
+ return await query.ToListAsync2();
}
/// Get blog comments by identifiers
@@ -277,7 +276,7 @@ namespace Grand.Business.Cms.Services
var query = from bc in _blogCommentRepository.Table
where commentIds.Contains(bc.Id)
select bc;
- var comments = await query.ToListAsync();
+ var comments = await query.ToListAsync2();
//sort by passed identifiers
var sortedComments = new List<BlogComment>();
foreach (string id in commentIds)
@@ -331,7 +330,7 @@ namespace Grand.Business.Cms.Services
/// <returns></returns>
public virtual async Task<IList<BlogCategory>> GetBlogCategoryByPostId(string blogPostId)
{
- return await _blogCategoryRepository.Table.Where(x => x.BlogPosts.Any(x => x.BlogPostId == blogPostId)).ToListAsync();
+ return await _blogCategoryRepository.Table.Where(x => x.BlogPosts.Any(x => x.BlogPostId == blogPostId)).ToListAsync2();
}
/// <summary>
@@ -344,7 +343,7 @@ namespace Grand.Business.Cms.Services
if (string.IsNullOrEmpty(blogCategorySeName))
throw new ArgumentNullException(nameof(blogCategorySeName));
- return await _blogCategoryRepository.Table.Where(x => x.SeName == blogCategorySeName.ToLowerInvariant()).FirstOrDefaultAsync();
+ return await _blogCategoryRepository.Table.Where(x => x.SeName == blogCategorySeName.ToLowerInvariant()).FirstOrDefaultAsync2();
}
/// <summary>
@@ -361,7 +360,7 @@ namespace Grand.Business.Cms.Services
query = query.Where(b => b.Stores.Contains(storeId) || !b.LimitedToStores);
}
- return await query.OrderBy(x => x.DisplayOrder).ToListAsync();
+ return await query.OrderBy(x => x.DisplayOrder).ToListAsync2();
}
/// <summary>
@@ -489,7 +488,7 @@ namespace Grand.Business.Cms.Services
orderby bp.DisplayOrder
select bp;
- return await query.ToListAsync();
+ return await query.ToListAsync2();
}
#endregion
diff --git a/src/Business/Grand.Business.Cms/Services/NewsService.cs b/src/Business/Grand.Business.Cms/Services/NewsService.cs
index 01f4215c2..6dab6942a 100644
--- a/src/Business/Grand.Business.Cms/Services/NewsService.cs
+++ b/src/Business/Grand.Business.Cms/Services/NewsService.cs
@@ -7,8 +7,6 @@ using Grand.Infrastructure;
using Grand.Infrastructure.Extensions;
using Grand.SharedKernel.Extensions;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -67,7 +65,8 @@ namespace Grand.Business.Cms.Services
public virtual async Task<IPagedList<NewsItem>> GetAllNews(string storeId = "",
int pageIndex = 0, int pageSize = int.MaxValue, bool ignorAcl = false, bool showHidden = false, string newsTitle = "")
{
- var query = _newsItemRepository.Table;
+ var query = from p in _newsItemRepository.Table
+ select p;
if (!String.IsNullOrWhiteSpace(newsTitle))
query = query.Where(n => n.Title != null && n.Title.ToLower().Contains(newsTitle.ToLower()));
@@ -162,7 +161,7 @@ namespace Grand.Business.Cms.Services
where (customerId == "" || c.CustomerId == customerId)
select c;
- return await query2.ToListAsync();
+ return await query2.ToListAsync2();
}
#endregion
diff --git a/src/Business/Grand.Business.Cms/Services/PageLayoutService.cs b/src/Business/Grand.Business.Cms/Services/PageLayoutService.cs
index 1ac5c4a6e..1e1ce3b6e 100644
--- a/src/Business/Grand.Business.Cms/Services/PageLayoutService.cs
+++ b/src/Business/Grand.Business.Cms/Services/PageLayoutService.cs
@@ -5,8 +5,6 @@ using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Extensions;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -61,7 +59,7 @@ namespace Grand.Business.Cms.Services
orderby pt.DisplayOrder
select pt;
- return query.ToListAsync();
+ return query.ToListAsync2();
});
}
diff --git a/src/Business/Grand.Business.Cms/Services/PageService.cs b/src/Business/Grand.Business.Cms/Services/PageService.cs
index 9337302a2..6dce630c4 100644
--- a/src/Business/Grand.Business.Cms/Services/PageService.cs
+++ b/src/Business/Grand.Business.Cms/Services/PageService.cs
@@ -9,8 +9,6 @@ using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Extensions;
using Grand.SharedKernel.Extensions;
using MediatR;
-using MongoDB.Driver;
-using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -78,10 +76,12 @@ namespace Grand.Business.Cms.Services
return await _cacheBase.GetAsync(key, async () =>
{
- var query = _pageRepository.Table;
+ var query = from p in _pageRepository.Table
+ select p;
+
query = query.Where(t => t.SystemName.ToLower() == systemName.ToLower());
query = query.OrderBy(t => t.Id);
- var pages = await query.ToListAsync();
+ var pages = await query.ToListAsync2();
if (!String.IsNullOrEmpty(storeId))
{
pages = pages.Where(x => _aclService.Authorize(x, storeId)).ToList();
@@ -100,7 +100,8 @@ namespace Grand.Business.Cms.Services
string key = string.Format(CacheKey.PAGES_ALL_KEY, storeId, ignorAcl);
return await _cacheBase.GetAsync(key, () =>
{
- var query = _pageRepository.Table;
+ var query = from p in _pageRepository.Table
+ select p;
query = query.OrderBy(t => t.DisplayOrder).ThenBy(t => t.SystemName);
@@ -124,7 +125,7 @@ namespace Grand.Business.Cms.Services
query = query.OrderBy(t => t.SystemName);
}
}
- return query.ToListAsync();
+ return query.ToListAsync2();
});
}
diff --git a/src/Core/Grand.Domain/Data/RepositoryExtensions.cs b/src/Core/Grand.Domain/Data/RepositoryExtensions.cs
index 441ee359c..72ea81f13 100644
--- a/src/Core/Grand.Domain/Data/RepositoryExtensions.cs
+++ b/src/Core/Grand.Domain/Data/RepositoryExtensions.cs
@@ -27,5 +27,9 @@ namespace Grand.Domain.Data
{
return ((IMongoQueryable<T>)query).FirstOrDefaultAsync();
}
+ public static Task<T> FirstOrDefaultAsync2<T>(this IQueryable<T> query)
+ {
+ return ((IMongoQueryable<T>)query).FirstOrDefaultAsync();
+ }
}
}