Skip to content

Commit

Permalink
Further changes with remove IServiceProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
support committed Dec 12, 2023
1 parent a411e60 commit 27568c1
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 108 deletions.
63 changes: 26 additions & 37 deletions src/Web/Grand.Web.Admin/Services/CustomerViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using Grand.Web.Admin.Models.ShoppingCart;
using Grand.Web.Common.Extensions;
using Grand.Web.Common.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.DependencyInjection;
using System.Net;
Expand Down Expand Up @@ -62,7 +63,7 @@ public class CustomerViewModelService : ICustomerViewModelService
private readonly ISalesEmployeeService _salesEmployeeService;
private readonly ICustomerNoteService _customerNoteService;
private readonly IDownloadService _downloadService;
private readonly IServiceProvider _serviceProvider;
private readonly IHttpContextAccessor _httpContextAccessor;

private readonly TaxSettings _taxSettings;
private readonly LoyaltyPointsSettings _loyaltyPointsSettings;
Expand Down Expand Up @@ -93,7 +94,7 @@ public CustomerViewModelService(
ISalesEmployeeService salesEmployeeService,
ICustomerNoteService customerNoteService,
IDownloadService downloadService,
IServiceProvider serviceProvider,
IHttpContextAccessor httpContextAccessor,
CustomerSettings customerSettings,
TaxSettings taxSettings,
LoyaltyPointsSettings loyaltyPointsSettings,
Expand Down Expand Up @@ -127,7 +128,7 @@ public CustomerViewModelService(
_salesEmployeeService = salesEmployeeService;
_customerNoteService = customerNoteService;
_downloadService = downloadService;
_serviceProvider = serviceProvider;
_httpContextAccessor = httpContextAccessor;
}

#region Utilities
Expand Down Expand Up @@ -213,7 +214,7 @@ protected virtual string GetCustomerGroupsNames(IList<CustomerGroup> customerGro
{
if (customer == null)
throw new ArgumentNullException(nameof(customer));
var openAuthenticationService = _serviceProvider.GetRequiredService<IExternalAuthenticationService>();
var openAuthenticationService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IExternalAuthenticationService>();
var result = new List<CustomerModel.AssociatedExternalAuthModel>();
foreach (var record in await openAuthenticationService.GetExternalIdentifiers(customer))
{
Expand Down Expand Up @@ -249,7 +250,7 @@ protected virtual async Task<CustomerModel> PrepareCustomerModelForList(Customer
};
}

protected virtual async Task PrepareSelesEmployeeModel(CustomerModel model)
protected virtual async Task PrepareSalesEmployeeModel(CustomerModel model)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
Expand Down Expand Up @@ -504,20 +505,13 @@ public virtual async Task PrepareCustomerModel(CustomerModel model, Customer cus

model.UsernamesEnabled = _customerSettings.UsernamesEnabled;
model.AllowUsersToChangeUsernames = _customerSettings.AllowUsersToChangeUsernames;
if (customer != null)
{
model.DisplayVatNumber = _taxSettings.EuVatEnabled;
}
else
{
model.DisplayVatNumber = false;
}
model.DisplayVatNumber = customer != null && _taxSettings.EuVatEnabled;

//stores
await PrepareStoresModel(model);

//employees
await PrepareSelesEmployeeModel(model);
await PrepareSalesEmployeeModel(model);

//customer attributes
await PrepareCustomerAttributeModel(model, customer);
Expand Down Expand Up @@ -763,7 +757,7 @@ public virtual async Task<Customer> UpdateCustomerModel(Customer customer, Custo
{
if (!model.VatNumber.Equals(prevVatNumber, StringComparison.OrdinalIgnoreCase))
{
var checkVatService = _serviceProvider.GetRequiredService<IVatService>();
var checkVatService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IVatService>();
await _userFieldService.SaveField(customer,
SystemCustomerFieldNames.VatNumberStatusId,
(int)(await checkVatService.GetVatNumberStatus(model.VatNumber)).status);
Expand Down Expand Up @@ -892,21 +886,17 @@ public virtual async Task DeleteSelected(IEnumerable<string> selectedIds)
{
var customers = new List<Customer>();
customers.AddRange(await _customerService.GetCustomersByIds(selectedIds.ToArray()));
for (var i = 0; i < customers.Count; i++)
foreach (var customer in customers.Where(customer => customer.Id != _workContext.CurrentCustomer.Id))
{
var customer = customers[i];
if (customer.Id != _workContext.CurrentCustomer.Id)
{
await _customerService.DeleteCustomer(customer);
}
await _customerService.DeleteCustomer(customer);
}
}

public async Task SendEmail(Customer customer, CustomerModel.SendEmailModel model)
{
var emailAccountService = _serviceProvider.GetRequiredService<IEmailAccountService>();
var emailAccountSettings = _serviceProvider.GetRequiredService<EmailAccountSettings>();
var queuedEmailService = _serviceProvider.GetRequiredService<IQueuedEmailService>();
var emailAccountService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IEmailAccountService>();
var emailAccountSettings = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<EmailAccountSettings>();
var queuedEmailService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IQueuedEmailService>();

var emailAccount = await emailAccountService.GetEmailAccountById(emailAccountSettings.DefaultEmailAccountId) ??
(await emailAccountService.GetAllEmailAccounts()).FirstOrDefault();
Expand Down Expand Up @@ -1015,8 +1005,7 @@ public virtual async Task PrepareAddressModel(CustomerAddressModel model, Addres
}
}

if (model.Address == null)
model.Address = new AddressModel();
model.Address ??= new AddressModel();

model.Address.NameEnabled = _addressSettings.NameEnabled;
model.Address.FirstNameEnabled = true;
Expand Down Expand Up @@ -1077,10 +1066,10 @@ public virtual async Task<IList<ShoppingCartItemModel>> PrepareShoppingCartItemM
var items = new List<ShoppingCartItemModel>();
if (cart.Any())
{
var taxService = _serviceProvider.GetRequiredService<ITaxService>();
var priceCalculationService = _serviceProvider.GetRequiredService<IPricingService>();
var priceFormatter = _serviceProvider.GetRequiredService<IPriceFormatter>();
var productAttributeFormatter = _serviceProvider.GetRequiredService<IProductAttributeFormatter>();
var taxService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<ITaxService>();
var priceCalculationService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IPricingService>();
var priceFormatter = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IPriceFormatter>();
var productAttributeFormatter = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IProductAttributeFormatter>();
foreach (var sci in cart)
{
var store = await _storeService.GetStoreById(sci.StoreId);
Expand Down Expand Up @@ -1111,7 +1100,7 @@ public virtual async Task DeleteCart(Customer customer, string id)
var cart = customer.ShoppingCartItems.FirstOrDefault(a => a.Id == id);
if (cart != null)
{
await _serviceProvider.GetRequiredService<IShoppingCartService>()
await _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IShoppingCartService>()
.DeleteShoppingCartItem(customer, cart, ensureOnlyActiveCheckoutAttributes: true);
await _customerService.UpdateCustomerInAdminPanel(customer);
}
Expand All @@ -1122,7 +1111,7 @@ public virtual async Task<IList<string>> UpdateCart(Customer customer, string sh
var cart = customer.ShoppingCartItems.FirstOrDefault(a => a.Id == shoppingCartId);
if (cart != null)
{
return await _serviceProvider.GetRequiredService<IShoppingCartService>()
return await _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IShoppingCartService>()
.UpdateShoppingCartItem(
customer,
shoppingCartId,
Expand Down Expand Up @@ -1258,7 +1247,7 @@ public virtual async Task DeletePersonalizedProduct(string id)
}
public virtual async Task<(IEnumerable<ContactFormModel> contactFormModels, int totalCount)> PrepareContactFormModel(string customerId, int pageIndex, int pageSize)
{
var contactUsService = _serviceProvider.GetRequiredService<IContactUsService>();
var contactUsService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IContactUsService>();
var contactform = await contactUsService.GetAllContactUs(storeId: "", vendorId: "", customerId: customerId, pageIndex: pageIndex - 1, pageSize: pageSize);
var items = new List<ContactFormModel>();
foreach (var x in contactform)
Expand All @@ -1275,12 +1264,12 @@ public virtual async Task DeletePersonalizedProduct(string id)
}
public virtual async Task<(IEnumerable<CustomerModel.OutOfStockSubscriptionModel> outOfStockSubscriptionModels, int totalCount)> PrepareOutOfStockSubscriptionModel(string customerId, int pageIndex, int pageSize)
{
var outOfStockSubscriptionService = _serviceProvider.GetRequiredService<IOutOfStockSubscriptionService>();
var outOfStockSubscriptionService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IOutOfStockSubscriptionService>();
var subscriptions = await outOfStockSubscriptionService.GetAllSubscriptionsByCustomerId(customerId, "", pageIndex - 1, pageSize);
var items = new List<CustomerModel.OutOfStockSubscriptionModel>();
if (subscriptions.Any())
{
var productAttributeFormatter = _serviceProvider.GetRequiredService<IProductAttributeFormatter>();
var productAttributeFormatter = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IProductAttributeFormatter>();
foreach (var x in subscriptions)
{
var store = await _storeService.GetStoreById(x.StoreId);
Expand All @@ -1300,7 +1289,7 @@ public virtual async Task DeletePersonalizedProduct(string id)
}
public virtual async Task<IList<CustomerModel.CustomerNote>> PrepareCustomerNoteList(string customerId)
{
var downloadService = _serviceProvider.GetRequiredService<IDownloadService>();
var downloadService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IDownloadService>();
var customerNoteModels = new List<CustomerModel.CustomerNote>();
foreach (var customerNote in (await _customerNoteService.GetCustomerNotes(customerId))
.OrderByDescending(on => on.CreatedOnUtc))
Expand Down Expand Up @@ -1335,7 +1324,7 @@ public virtual async Task<CustomerNote> InsertCustomerNote(string customerId, st
if (displayToCustomer)
{
//email
var messageProviderService = _serviceProvider.GetRequiredService<IMessageProviderService>();
var messageProviderService = _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IMessageProviderService>();
await messageProviderService.SendNewCustomerNoteMessage(customerNote,
await _customerService.GetCustomerById(customerId), _workContext.CurrentStore, _workContext.WorkingLanguage.Id);

Expand Down
27 changes: 16 additions & 11 deletions src/Web/Grand.Web.Admin/Services/NewsViewModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Grand.Web.Admin.Extensions.Mapping;
using Grand.Web.Admin.Interfaces;
using Grand.Web.Admin.Models.News;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace Grand.Web.Admin.Services
Expand All @@ -26,7 +27,9 @@ public class NewsViewModelService : INewsViewModelService
private readonly ITranslationService _translationService;
private readonly ISlugService _slugService;
private readonly IPictureService _pictureService;
private readonly IServiceProvider _serviceProvider;
private readonly ILanguageService _languageService;
private readonly ICustomerService _customerService;
private readonly SeoSettings _seoSettings;
#endregion

#region Constructors
Expand All @@ -36,14 +39,18 @@ public NewsViewModelService(INewsService newsService,
ITranslationService translationService,
ISlugService slugService,
IPictureService pictureService,
IServiceProvider serviceProvider)
ILanguageService languageService,
ICustomerService customerService,
SeoSettings seoSettings)
{
_newsService = newsService;
_dateTimeService = dateTimeService;
_translationService = translationService;
_slugService = slugService;
_pictureService = pictureService;
_serviceProvider = serviceProvider;
_languageService = languageService;
_customerService = customerService;
_seoSettings = seoSettings;
}

#endregion
Expand All @@ -62,14 +69,13 @@ public NewsViewModelService(INewsService newsService,
}
public virtual async Task<NewsItem> InsertNewsItemModel(NewsItemModel model)
{
var datetimeService = _serviceProvider.GetRequiredService<IDateTimeService>();
var newsItem = model.ToEntity(datetimeService);
var newsItem = model.ToEntity(_dateTimeService);
newsItem.CreatedOnUtc = DateTime.UtcNow;
await _newsService.InsertNews(newsItem);

var seName = await newsItem.ValidateSeName(model.SeName, model.Title, true, _serviceProvider.GetRequiredService<SeoSettings>(), _slugService, _serviceProvider.GetRequiredService<ILanguageService>());
var seName = await newsItem.ValidateSeName(model.SeName, model.Title, true, _seoSettings, _slugService, _languageService);
newsItem.SeName = seName;
newsItem.Locales = await model.Locales.ToTranslationProperty(newsItem, x => x.Title, _serviceProvider.GetRequiredService<SeoSettings>(), _slugService, _serviceProvider.GetRequiredService<ILanguageService>());
newsItem.Locales = await model.Locales.ToTranslationProperty(newsItem, x => x.Title, _seoSettings, _slugService, _languageService);
await _newsService.UpdateNews(newsItem);
//search engine name
await _slugService.SaveSlug(newsItem, seName, "");
Expand All @@ -82,9 +88,9 @@ public virtual async Task<NewsItem> UpdateNewsItemModel(NewsItem newsItem, NewsI
{
var prevPictureId = newsItem.PictureId;
newsItem = model.ToEntity(newsItem, _dateTimeService);
var seName = await newsItem.ValidateSeName(model.SeName, model.Title, true, _serviceProvider.GetRequiredService<SeoSettings>(), _slugService, _serviceProvider.GetRequiredService<ILanguageService>());
var seName = await newsItem.ValidateSeName(model.SeName, model.Title, true, _seoSettings, _slugService, _languageService);
newsItem.SeName = seName;
newsItem.Locales = await model.Locales.ToTranslationProperty(newsItem, x => x.Title, _serviceProvider.GetRequiredService<SeoSettings>(), _slugService, _serviceProvider.GetRequiredService<ILanguageService>());
newsItem.Locales = await model.Locales.ToTranslationProperty(newsItem, x => x.Title, _seoSettings, _slugService, _languageService);
await _newsService.UpdateNews(newsItem);

//search engine name
Expand Down Expand Up @@ -116,7 +122,6 @@ public virtual async Task<NewsItem> UpdateNewsItemModel(NewsItem newsItem, NewsI
//load all news comments
comments = await _newsService.GetAllComments("");
}
var customerService = _serviceProvider.GetRequiredService<ICustomerService>();
var items = new List<NewsCommentModel>();
foreach (var newsComment in comments.PagedForCommand(pageIndex, pageSize))
{
Expand All @@ -127,7 +132,7 @@ public virtual async Task<NewsItem> UpdateNewsItemModel(NewsItem newsItem, NewsI
NewsItemTitle = (await _newsService.GetNewsById(newsComment.NewsItemId))?.Title,
CustomerId = newsComment.CustomerId
};
var customer = await customerService.GetCustomerById(newsComment.CustomerId);
var customer = await _customerService.GetCustomerById(newsComment.CustomerId);
commentModel.CustomerInfo = !string.IsNullOrEmpty(customer.Email) ? customer.Email : _translationService.GetResource("Admin.Customers.Guest");
commentModel.CreatedOn = _dateTimeService.ConvertToUserTime(newsComment.CreatedOnUtc, DateTimeKind.Utc);
commentModel.CommentTitle = newsComment.CommentTitle;
Expand Down
Loading

0 comments on commit 27568c1

Please sign in to comment.