Skip to content

Commit 396e280

Browse files
starovernetthiennn
authored andcommitted
Stock servivce changes (simplcommerce#695)
* rewrite AddAllProduct to use single query with group join * add StockService tests * remove unnesessary 'at' symbols
1 parent 3fc663a commit 396e280

16 files changed

Lines changed: 232 additions & 26 deletions

File tree

SimplCommerce.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Commen
119119
EndProject
120120
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.PaymentBraintree", "src\Modules\SimplCommerce.Module.PaymentBraintree\SimplCommerce.Module.PaymentBraintree.csproj", "{3BF9411D-4794-404D-9ADE-8DABD50853D1}"
121121
EndProject
122+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Inventory.Tests", "test\SimplCommerce.Module.Inventory.Tests\SimplCommerce.Module.Inventory.Tests.csproj", "{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}"
123+
EndProject
122124
Global
123125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
124126
Debug|Any CPU = Debug|Any CPU
@@ -633,6 +635,18 @@ Global
633635
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x64.Build.0 = Release|Any CPU
634636
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x86.ActiveCfg = Release|Any CPU
635637
{3BF9411D-4794-404D-9ADE-8DABD50853D1}.Release|x86.Build.0 = Release|Any CPU
638+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
639+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
640+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|x64.ActiveCfg = Debug|Any CPU
641+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|x64.Build.0 = Debug|Any CPU
642+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|x86.ActiveCfg = Debug|Any CPU
643+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Debug|x86.Build.0 = Debug|Any CPU
644+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
645+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|Any CPU.Build.0 = Release|Any CPU
646+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x64.ActiveCfg = Release|Any CPU
647+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x64.Build.0 = Release|Any CPU
648+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x86.ActiveCfg = Release|Any CPU
649+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5}.Release|x86.Build.0 = Release|Any CPU
636650
EndGlobalSection
637651
GlobalSection(SolutionProperties) = preSolution
638652
HideSolutionNode = FALSE
@@ -682,6 +696,7 @@ Global
682696
{67B14D0D-0BA7-4E6C-8F8E-151C92732D6D} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
683697
{4C78BFFD-3AD6-4C39-A44D-F7FC55A2B3A6} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
684698
{3BF9411D-4794-404D-9ADE-8DABD50853D1} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
699+
{19AF369C-71BB-44B3-9D02-8EF3A3E912E5} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
685700
EndGlobalSection
686701
GlobalSection(ExtensibilityGlobals) = postSolution
687702
SolutionGuid = {B9D0D8F0-1AB9-44DD-839F-ED8CEE7DDB10}

src/Modules/SimplCommerce.Module.Comments/Areas/Comments/Views/Shared/Components/Comment/Default.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div id="comments" class="comment-item-list">
2727
@if (Model.CommentsCount > 0)
2828
{
29-
@foreach (var comment in Model.Items.Data)
29+
foreach (var comment in Model.Items.Data)
3030
{
3131
<div>
3232
<div>

src/Modules/SimplCommerce.Module.Core/Data/RepositoryWithTypedId.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using Microsoft.EntityFrameworkCore;
45
using Microsoft.EntityFrameworkCore.Storage;
@@ -24,6 +25,11 @@ public void Add(T entity)
2425
DbSet.Add(entity);
2526
}
2627

28+
public void AddRange(IEnumerable<T> entity)
29+
{
30+
DbSet.AddRange(entity);
31+
}
32+
2733
public IDbContextTransaction BeginTransaction()
2834
{
2935
return Context.Database.BeginTransaction();

src/Modules/SimplCommerce.Module.Inventory/Services/StockService.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,18 @@ public StockService(IRepository<Stock> stockRepository, IRepository<Product> pro
2323

2424
public async Task AddAllProduct(Warehouse warehouse)
2525
{
26-
// TODO Need refactor for better performance
27-
var productIds = await _productRepository.Query().Where(x => !x.HasOptions && x.VendorId == warehouse.VendorId).Select(x => x.Id).ToListAsync();
28-
var inStockProductIds = await _stockRepository.Query().Where(x => x.WarehouseId== warehouse.Id).Select(x => x.ProductId).ToListAsync();
29-
foreach(var productId in productIds)
30-
{
31-
if (!inStockProductIds.Contains(productId))
26+
var stocks = await _productRepository.Query().Where(x => !x.HasOptions && x.VendorId == warehouse.VendorId)
27+
.GroupJoin(_stockRepository.Query().Where(x => x.WarehouseId == warehouse.Id),
28+
product => product.Id, stock => stock.ProductId,
29+
(product, stockCollection) => new {IsNew = !stockCollection.Any(), ProductId = product.Id})
30+
.Where(x => x.IsNew)
31+
.Select(x => new Stock
3232
{
33-
var stock = new Stock
34-
{
35-
ProductId = productId,
36-
WarehouseId = warehouse.Id,
37-
Quantity = 0
38-
};
39-
40-
_stockRepository.Add(stock);
41-
}
42-
}
43-
33+
ProductId = x.ProductId,
34+
WarehouseId = warehouse.Id,
35+
Quantity = 0
36+
}).ToArrayAsync();
37+
_stockRepository.AddRange(stocks);
4438
await _stockRepository.SaveChangesAsync();
4539
}
4640

src/Modules/SimplCommerce.Module.Reviews/Areas/Reviews/Views/Review/List.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<div id="reviews" class="review-item-list">
9494
@if (Model.ReviewsCount > 0)
9595
{
96-
@foreach (var review in Model.Items.Data)
96+
foreach (var review in Model.Items.Data)
9797
{
9898
<div>
9999
<ul class="list-inline product-ratings">

src/Modules/SimplCommerce.Module.Reviews/Areas/Reviews/Views/Shared/Components/Review/Default.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<div id="reviews" class="review-item-list">
6969
@if (Model.ReviewsCount > 0)
7070
{
71-
@foreach (var review in Model.Items.Data)
71+
foreach (var review in Model.Items.Data)
7272
{
7373
<div>
7474
<ul class="list-inline product-ratings">

src/Modules/SimplCommerce.Module.WishList/Areas/WishList/Views/WishList/PrivateList.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<hr />
1919
@if (Model.Items.TotalItems > 0)
2020
{
21-
@foreach (var item in Model.Items.Data)
21+
foreach (var item in Model.Items.Data)
2222
{
2323
<div class="row item">
2424
<div class="col-md-3">

src/Modules/SimplCommerce.Module.WishList/Areas/WishList/Views/WishList/PublicList.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<hr />
1616
@if (Model.Items.TotalItems > 0)
1717
{
18-
@foreach (var item in Model.Items.Data)
18+
foreach (var item in Model.Items.Data)
1919
{
2020
<div class="row item">
2121
<div class="col-md-4">

src/SimplCommerce.Infrastructure/Data/IRepositoryWithTypedId.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using Microsoft.EntityFrameworkCore.Storage;
45
using SimplCommerce.Infrastructure.Models;
@@ -11,6 +12,8 @@ public interface IRepositoryWithTypedId<T, TId> where T : IEntityWithTypedId<TId
1112

1213
void Add(T entity);
1314

15+
void AddRange(IEnumerable<T> entity);
16+
1417
IDbContextTransaction BeginTransaction();
1518

1619
void SaveChanges();

src/SimplCommerce.WebHost/Themes/CozaStore/Views/Shared/_LoginPartial.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@if (SignInManager.IsSignedIn(User))
88
{
99
<a class="flex-c-m trans-04 p-lr-25" asp-area="Core" asp-controller="Manage" asp-action="Index" title="@Localizer["Manage"]">@Localizer["Hello {0}!", (await WorkContext.GetCurrentUser()).FullName]</a>
10-
@if (User.IsInRole("admin") || User.IsInRole("vendor"))
10+
if (User.IsInRole("admin") || User.IsInRole("vendor"))
1111
{
1212
<a class="flex-c-m trans-04 p-lr-25" asp-area="Core" asp-controller="HomeAdmin" asp-action="Index">@Localizer["Dashboard"]</a>
1313
}

0 commit comments

Comments
 (0)