Skip to content

Commit efdad80

Browse files
Improvements/code (#589)
* Remove redundant self-assignments * Mark entity as non-nullable in LiteDBRepository.cs * Improve null safety and readability in various files * Refactor file reading to be asynchronous * Make RedisMessageCacheManager.Clear method asynchronous * Refactor CultureInfo validation logic in validators * Improve URL validation logic in DomainHostValidator and StoreValidator
1 parent 72c6318 commit efdad80

File tree

14 files changed

+23
-37
lines changed

14 files changed

+23
-37
lines changed

src/Core/Grand.Data/LiteDb/LiteDBRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ private Task Update(T entity, UpdateBuilder<T> updateBuilder)
226226
propertyInfo?.SetValue(entity, item.Value);
227227
}
228228

229-
entity.UpdatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
230-
entity.UpdatedBy = _auditInfoProvider.GetCurrentUser();
229+
entity!.UpdatedOnUtc = _auditInfoProvider.GetCurrentDateTime();
230+
entity!.UpdatedBy = _auditInfoProvider.GetCurrentUser();
231231

232232
Collection.Update(entity);
233233
return Task.CompletedTask;

src/Core/Grand.Infrastructure/Caching/Redis/RedisMessageCacheManager.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ public override Task RemoveByPrefix(string prefix, bool publisher = true)
5353
/// Clear cache
5454
/// </summary>
5555
/// <param name="publisher">publisher</param>
56-
public override Task Clear(bool publisher = true)
56+
public override async Task Clear(bool publisher = true)
5757
{
58-
base.Clear();
58+
await base.Clear(publisher);
5959
if (publisher)
60-
_messageBus.PublishAsync(new MessageEvent { Key = "", MessageType = (int)MessageEventType.ClearCache });
60+
await _messageBus.PublishAsync(new MessageEvent { Key = "", MessageType = (int)MessageEventType.ClearCache });
6161

62-
return Task.CompletedTask;
6362
}
6463
}

src/Modules/Grand.Module.Installer/Services/InstallDataLocaleResources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected virtual async Task InstallLocaleResources()
1212

1313
//save resources
1414
var filePath = Path.Combine(_hostingEnvironment.ContentRootPath, "App_Data/Resources/DefaultLanguage.xml");
15-
var localesXml = File.ReadAllText(filePath);
15+
var localesXml = await File.ReadAllTextAsync(filePath);
1616

1717
var xmlDoc = XmlExtensions.LanguageXmlDocument(localesXml);
1818

src/Web/Grand.Web.Admin/Areas/Admin/Views/Shared/_AdminLayout.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
@if (await groupService.IsAdmin(currentCustomer))
177177
{
178178
<li>
179-
<a href="@Url.Action("Edit", "Customer", new { currentCustomer.Id, area = Constants.AreaAdmin })">
179+
<a href="@Url.Action("Edit", "Customer", new { currentCustomer!.Id, area = Constants.AreaAdmin })">
180180
<i class="icon-user"></i> @Loc["Admin.Header.Profile"]
181181
</a>
182182
</li>

src/Web/Grand.Web.Admin/Controllers/OrderController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,6 @@ public async Task<IActionResult> AddressEdit(OrderAddressModel model,
945945

946946
//If we got this far, something failed, redisplay form
947947
model = await orderViewModelService.PrepareOrderAddressModel(order, address);
948-
model.BillingAddress = model.BillingAddress;
949948
return View(model);
950949
}
951950

src/Web/Grand.Web.Admin/Controllers/ProductController.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@ public async Task<IActionResult> RelatedProductAddPopup(ProductModel.AddRelatedP
631631

632632
Error(ModelState);
633633
model = await _productViewModelService.PrepareRelatedProductModel();
634-
model.ProductId = model.ProductId;
635634
return View(model);
636635
}
637636

@@ -729,7 +728,6 @@ public async Task<IActionResult> SimilarProductAddPopup(ProductModel.AddSimilarP
729728

730729
Error(ModelState);
731730
model = await _productViewModelService.PrepareSimilarProductModel();
732-
model.ProductId = model.ProductId;
733731
return View(model);
734732
}
735733

@@ -827,7 +825,6 @@ public async Task<IActionResult> BundleProductAddPopup(ProductModel.AddBundlePro
827825

828826
Error(ModelState);
829827
model = await _productViewModelService.PrepareBundleProductModel();
830-
model.ProductId = model.ProductId;
831828
return View(model);
832829
}
833830

@@ -915,7 +912,6 @@ public async Task<IActionResult> CrossSellProductAddPopup(ProductModel.AddCrossS
915912

916913
Error(ModelState);
917914
model = await _productViewModelService.PrepareCrossSellProductModel();
918-
model.ProductId = model.ProductId;
919915
return View(model);
920916
}
921917

@@ -1002,7 +998,6 @@ public async Task<IActionResult> RecommendedProductAddPopup(ProductModel.AddReco
1002998

1003999
Error(ModelState);
10041000
model = await _productViewModelService.PrepareRecommendedProductModel();
1005-
model.ProductId = model.ProductId;
10061001
return View(model);
10071002
}
10081003

@@ -1108,7 +1103,6 @@ public async Task<IActionResult> AssociatedProductAddPopup(ProductModel.AddAssoc
11081103

11091104
Error(ModelState);
11101105
model = await _productViewModelService.PrepareAssociatedProductModel();
1111-
model.ProductId = model.ProductId;
11121106
return View(model);
11131107
}
11141108

src/Web/Grand.Web.Admin/Services/ShipmentViewModelService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public virtual async Task<ShipmentModel> PrepareShipmentModel(Shipment shipment,
104104
if (prepareProducts)
105105
foreach (var shipmentItem in shipment.ShipmentItems)
106106
{
107-
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == shipmentItem.OrderItemId);
107+
var orderItem = order?.OrderItems.FirstOrDefault(x => x.Id == shipmentItem.OrderItemId);
108108
if (orderItem == null)
109109
continue;
110110

@@ -143,8 +143,7 @@ public virtual async Task<ShipmentModel> PrepareShipmentModel(Shipment shipment,
143143

144144
if (prepareShipmentEvent && !string.IsNullOrEmpty(shipment.TrackingNumber))
145145
{
146-
var srcm = _shippingService.LoadShippingRateCalculationProviderBySystemName(
147-
order.ShippingRateProviderSystemName);
146+
var srcm = _shippingService.LoadShippingRateCalculationProviderBySystemName(order?.ShippingRateProviderSystemName);
148147
if (srcm != null &&
149148
srcm.IsShippingRateMethodActive(_shippingProviderSettings))
150149
{

src/Web/Grand.Web.Admin/Validators/Directory/CurrencyValidator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ public CurrencyValidator(
3737
{
3838
if (string.IsNullOrEmpty(x))
3939
return true;
40-
//create a CultureInfo object
41-
//if "DisplayLocale" is wrong, then exception will be thrown
42-
new CultureInfo(x);
43-
return true;
40+
41+
var culture = new CultureInfo(x);
42+
return culture != null;
4443
}
4544
catch
4645
{

src/Web/Grand.Web.Admin/Validators/Localization/LanguageValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public LanguageValidator(
2121
try
2222
{
2323
//create a CultureInfo object
24-
new CultureInfo(x);
25-
return true;
24+
var culture = new CultureInfo(x);
25+
return culture != null;
2626
}
2727
catch
2828
{

src/Web/Grand.Web.Admin/Validators/Stores/DomainHostValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public DomainHostValidator(
1818
{
1919
try
2020
{
21-
new Uri(x.Url);
22-
return true;
21+
var uri = new Uri(x.Url);
22+
return uri != null;
2323
}
2424
catch
2525
{

0 commit comments

Comments
 (0)