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
Moved specific methods from IWorkContextSetter to controllers
support9b9338a046c6
diff --git a/src/Core/Grand.Infrastructure/IWorkContext.cs b/src/Core/Grand.Infrastructure/IWorkContext.csindex 89c542c69..5160fec25 100644--- a/src/Core/Grand.Infrastructure/IWorkContext.cs+++ b/src/Core/Grand.Infrastructure/IWorkContext.cs@@ -77,30 +77,16 @@ namespace Grand.Infrastructure/// Set current user working language by Middleware/// </summary>Task<Language> SetWorkingLanguage(Customer customer);-- /// <summary>- /// Set current user working language- /// </summary>- Task<Language> SetWorkingLanguage(Language language);/// <summary>/// Set current user working currency by Middleware/// </summary>Task<Currency> SetWorkingCurrency(Customer customer);-- /// <summary>- /// Set user working currency- /// </summary>- Task<Currency> SetWorkingCurrency(Currency currency);/// <summary>/// Set current tax display type by Middleware/// </summary>Task<TaxDisplayType> SetTaxDisplayType(Customer customer);-- /// <summary>- /// Set tax display type- /// </summary>- Task<TaxDisplayType> SetTaxDisplayType(TaxDisplayType taxDisplayType);+}}diff --git a/src/Web/Grand.Web.Admin/Controllers/HomeController.cs b/src/Web/Grand.Web.Admin/Controllers/HomeController.csindex c7b9b87ad..83bde38c1 100644--- a/src/Web/Grand.Web.Admin/Controllers/HomeController.cs+++ b/src/Web/Grand.Web.Admin/Controllers/HomeController.cs@@ -109,12 +109,14 @@ namespace Grand.Web.Admin.Controllersreturn PartialView(model);}- public async Task<IActionResult> SetLanguage(string langid, [FromServices] IWorkContextSetter workContextSetter, [FromServices] ILanguageService languageService, string returnUrl = "")+ public async Task<IActionResult> SetLanguage(string langid,+ [FromServices] ILanguageService languageService,+ string returnUrl = ""){var language = await languageService.GetLanguageById(langid);if (language != null){- await workContextSetter.SetWorkingLanguage(language);+ await _userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.LanguageId, language.Id, _workContext.CurrentStore.Id);}//home pagediff --git a/src/Web/Grand.Web.Common/WorkContext.cs b/src/Web/Grand.Web.Common/WorkContext.csindex 6f10c597f..edfafa2d4 100644--- a/src/Web/Grand.Web.Common/WorkContext.cs+++ b/src/Web/Grand.Web.Common/WorkContext.cs@@ -15,6 +15,7 @@ using Grand.Infrastructure.Configuration;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Localization;+using Microsoft.Extensions.DependencyInjection;using Wangkanai.Detection.Services;namespace Grand.Web.Common@@ -28,16 +29,13 @@ namespace Grand.Web.Commonprivate readonly IHttpContextAccessor _httpContextAccessor;private readonly IGrandAuthenticationService _authenticationService;- private readonly IApiAuthenticationService _apiAuthenticationService;private readonly ICurrencyService _currencyService;private readonly ICustomerService _customerService;- private readonly IGroupService _groupService;- private readonly IUserFieldService _userFieldService;+ private readonly IGroupService _groupService;private readonly ILanguageService _languageService;private readonly IStoreHelper _storeHelper;private readonly IAclService _aclService;private readonly IVendorService _vendorService;- private readonly IDetectionService _detectionService;private readonly LanguageSettings _languageSettings;private readonly TaxSettings _taxSettings;@@ -58,32 +56,26 @@ namespace Grand.Web.Commonpublic WorkContext(IHttpContextAccessor httpContextAccessor,IGrandAuthenticationService authenticationService,- IApiAuthenticationService apiAuthenticationService,ICurrencyService currencyService,ICustomerService customerService,IGroupService groupService,- IUserFieldService userFieldService,ILanguageService languageService,IStoreHelper storeHelper,IAclService aclService,IVendorService vendorService,- IDetectionService detectionService,LanguageSettings languageSettings,TaxSettings taxSettings,AppConfig config){_httpContextAccessor = httpContextAccessor;_authenticationService = authenticationService;- _apiAuthenticationService = apiAuthenticationService;_currencyService = currencyService;_customerService = customerService;_groupService = groupService;- _userFieldService = userFieldService;_languageService = languageService;_storeHelper = storeHelper;_aclService = aclService;_vendorService = vendorService;- _detectionService = detectionService;_languageSettings = languageSettings;_taxSettings = taxSettings;_config = config;@@ -228,12 +220,14 @@ namespace Grand.Web.Commonprivate async Task<Customer> GetApiUserCustomer(){- return await _apiAuthenticationService.GetAuthenticatedCustomer();+ var apiAuthenticationService = _httpContextAccessor.HttpContext.RequestServices.GetService<IApiAuthenticationService>();+ return await apiAuthenticationService.GetAuthenticatedCustomer();}private async Task<Customer> GetSearchEngineCustomer(){- var isCrawler = _detectionService.Crawler?.IsCrawler;+ var detectionService = _httpContextAccessor.HttpContext.RequestServices.GetService<IDetectionService>();+ var isCrawler = detectionService.Crawler?.IsCrawler;if (!isCrawler.GetValueOrDefault()) return null;return await _customerService.GetCustomerBySystemName(SystemCustomerNames.SearchEngine);@@ -331,22 +325,7 @@ namespace Grand.Web.Common/// Gets or sets current user working language/// </summary>public virtual Language WorkingLanguage => _cachedLanguage;-- /// <summary>- /// Set current user working language- /// </summary>- public virtual async Task<Language> SetWorkingLanguage(Language language)- {- if (language != null)- await _userFieldService.SaveField(CurrentCustomer, SystemCustomerFieldNames.LanguageId, language.Id,- CurrentStore.Id);-- //then reset the cache value- _cachedLanguage = null;-- return language;- }-+/// <summary>/// Set current user working language by Middleware/// </summary>@@ -383,18 +362,8 @@ namespace Grand.Web.Common: allStoreLanguages.FirstOrDefault(lang =>//if the language for the current store not exist, then use the firstlang.Id == CurrentStore.DefaultLanguageId))) ?? allStoreLanguages.FirstOrDefault();-- //cache the language- _cachedLanguage = customerLanguage;-- //save the language identifier- if (customerLanguage != null && customerLanguage.Id != customerLanguageId)- {- await _userFieldService.SaveField(customer,- SystemCustomerFieldNames.LanguageId, customerLanguage.Id, CurrentStore.Id);- }-- return _cachedLanguage ?? throw new Exception("No language could be loaded");++ return _cachedLanguage = customerLanguage ?? throw new Exception("No language could be loaded");}/// <summary>@@ -431,32 +400,8 @@ namespace Grand.Web.Commonvar customerCurrency = (allStoreCurrencies.FirstOrDefault(currency => currency.Id == customerCurrencyId) ?? (!string.IsNullOrEmpty(CurrentStore?.DefaultCurrencyId) ? allStoreCurrencies.FirstOrDefault(currency => currency.Id == CurrentStore.DefaultCurrencyId) : allStoreCurrencies.FirstOrDefault(currency => currency.Id == WorkingLanguage.DefaultCurrencyId))) ??allStoreCurrencies.FirstOrDefault();- //cache the currency- _cachedCurrency = customerCurrency;- //save the currency identifier- if (customerCurrency?.Id != customerCurrencyId)- {- await _userFieldService.SaveField(customer,- SystemCustomerFieldNames.CurrencyId, customerCurrency?.Id, CurrentStore?.Id);- }-- return _cachedCurrency ?? throw new Exception("No currency could be loaded");- }-- /// <summary>- /// Set user working currency- /// </summary>- public virtual async Task<Currency> SetWorkingCurrency(Currency currency)- {- //and save it- await _userFieldService.SaveField(CurrentCustomer,- SystemCustomerFieldNames.CurrencyId, currency.Id, CurrentStore.Id);-- //then reset the cache value- _cachedCurrency = null;-- return currency;+ return _cachedCurrency = customerCurrency ?? throw new Exception("No currency could be loaded");}/// <summary>@@ -486,22 +431,6 @@ namespace Grand.Web.Commonreturn await Task.FromResult(_cachedTaxDisplayType);}-- public virtual async Task<TaxDisplayType> SetTaxDisplayType(TaxDisplayType taxDisplayType)- {- //whether customers are allowed to select tax display type- if (!_taxSettings.AllowCustomersToSelectTaxDisplayType)- return await Task.FromResult(taxDisplayType);-- //save passed value- await _userFieldService.SaveField(CurrentCustomer,- SystemCustomerFieldNames.TaxDisplayTypeId, (int)taxDisplayType, CurrentStore.Id);-- //then reset the cache value- _cachedTaxDisplayType = taxDisplayType;- return taxDisplayType;- }-/// <summary>/// Gets the current store/// </summary>diff --git a/src/Web/Grand.Web/Controllers/CommonController.cs b/src/Web/Grand.Web/Controllers/CommonController.csindex dc9b19357..c049f5b86 100644--- a/src/Web/Grand.Web/Controllers/CommonController.cs+++ b/src/Web/Grand.Web/Controllers/CommonController.cs@@ -144,7 +144,7 @@ namespace Grand.Web.Controllers[DenySystemAccount]public virtual async Task<IActionResult> SetLanguage([FromServices] AppConfig config,- [FromServices] IWorkContextSetter workContextSetter,+ [FromServices] IUserFieldService userFieldService,string langCode, string returnUrl = default){var language = await _languageService.GetLanguageByCode(langCode);@@ -168,7 +168,7 @@ namespace Grand.Web.ControllersreturnUrl = AddLanguageSeo(returnUrl, language);}- await workContextSetter.SetWorkingLanguage(language);+ await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.LanguageId, language.Id, _workContext.CurrentStore.Id);//notificationawait _mediator.Publish(new ChangeLanguageEvent(_workContext.CurrentCustomer, language));@@ -213,21 +213,20 @@ namespace Grand.Web.Controllers[HttpGet]public virtual async Task<IActionResult> SetCurrency([FromServices] ICurrencyService currencyService,- [FromServices] IWorkContextSetter workContextSetter,[FromServices] IUserFieldService userFieldService,string currencyCode, string returnUrl = ""){var currency = await currencyService.GetCurrencyByCode(currencyCode);if (currency != null)- await workContextSetter.SetWorkingCurrency(currency);+ {+ await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.CurrencyId, currency.Id, _workContext.CurrentStore.Id);+ }//clear coupon code- await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.DiscountCoupons,- "");+ await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.DiscountCoupons, "");//clear gift card- await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.GiftVoucherCoupons,- "");+ await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.GiftVoucherCoupons, "");//notificationawait _mediator.Publish(new ChangeCurrencyEvent(_workContext.CurrentCustomer, currency));@@ -291,15 +290,12 @@ namespace Grand.Web.Controllers[PublicStore(true)][HttpGet]public virtual async Task<IActionResult> SetTaxType(- [FromServices] IWorkContextSetter workContextSetter,+ [FromServices] TaxSettings taxSettings,+ [FromServices] IUserFieldService userFieldService,int customerTaxType, string returnUrl = default){var taxDisplayType = (TaxDisplayType)Enum.ToObject(typeof(TaxDisplayType), customerTaxType);- await workContextSetter.SetTaxDisplayType(taxDisplayType);-- //notification- await _mediator.Publish(new ChangeTaxTypeEvent(_workContext.CurrentCustomer, taxDisplayType));-+//home pageif (string.IsNullOrEmpty(returnUrl))returnUrl = Url.RouteUrl("HomePage");@@ -308,6 +304,17 @@ namespace Grand.Web.Controllersif (!Url.IsLocalUrl(returnUrl))returnUrl = Url.RouteUrl("HomePage");+ //whether customers are allowed to select tax display type+ if (!taxSettings.AllowCustomersToSelectTaxDisplayType)+ return Redirect(returnUrl);++ //save passed value+ await userFieldService.SaveField(_workContext.CurrentCustomer,+ SystemCustomerFieldNames.TaxDisplayTypeId, (int)taxDisplayType, _workContext.CurrentStore.Id);++ //notification+ await _mediator.Publish(new ChangeTaxTypeEvent(_workContext.CurrentCustomer, taxDisplayType));+return Redirect(returnUrl);}