Skip to content

Commit

Permalink
Refactor: Replace custom mappings with AutoMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
support committed Aug 9, 2024
1 parent e524c87 commit 7577b23
Show file tree
Hide file tree
Showing 40 changed files with 200 additions and 607 deletions.
10 changes: 7 additions & 3 deletions src/Web/Grand.Web.Admin/Controllers/MenuController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Interfaces.Common.Localization;
using AutoMapper;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Web.Admin.Extensions.Mapping;
using Grand.Web.Admin.Interfaces;
Expand All @@ -17,10 +18,12 @@ public class MenuController : BaseAdminController

public MenuController(
IMenuViewModelService menuViewModelService,
ITranslationService translationService)
ITranslationService translationService,
IMapper mapper)
{
_menuViewModelService = menuViewModelService;
_translationService = translationService;
_mapper = mapper;
}

#endregion
Expand All @@ -29,6 +32,7 @@ public MenuController(

private readonly IMenuViewModelService _menuViewModelService;
private readonly ITranslationService _translationService;
private readonly IMapper _mapper;

#endregion

Expand Down Expand Up @@ -97,7 +101,7 @@ public async Task<IActionResult> Edit(string id)
//No menu found with the specified id
return RedirectToAction("Index");

var model = menu.ToModel();
var model = _mapper.Map<MenuModel>(menu);

return View(model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public async Task<IActionResult> List(DataSourceRequest command, MessageTemplate
var items = new List<MessageTemplateModel>();
foreach (var x in messageTemplates)
{
var templateModel = x.ToModel();
var templateModel = _mapper.Map<MessageTemplateModel>(x);
var stores = (await _storeService
.GetAllStores())
.Where(s => !x.LimitedToStores || templateModel.Stores.Contains(s.Id))
Expand Down Expand Up @@ -135,7 +135,7 @@ public async Task<IActionResult> Create(MessageTemplateModel model, bool continu
{
if (ModelState.IsValid)
{
var messageTemplate = model.ToEntity();
var messageTemplate = _mapper.Map<MessageTemplate>(model);
//attached file
if (!model.HasAttachedDownload)
messageTemplate.AttachedDownloadId = "";
Expand Down Expand Up @@ -176,7 +176,7 @@ public async Task<IActionResult> Edit(string id)
//No message template found with the specified id
return RedirectToAction("List");

var model = messageTemplate.ToModel();
var model = _mapper.Map<MessageTemplateModel>(messageTemplate);
model.SendImmediately = !model.DelayBeforeSend.HasValue;
model.HasAttachedDownload = !string.IsNullOrEmpty(model.AttachedDownloadId);
model.AllowedTokens = _messageTokenProvider.GetListOfAllowedTokens();
Expand Down Expand Up @@ -214,7 +214,7 @@ public async Task<IActionResult> Edit(MessageTemplateModel model, bool continueE

if (ModelState.IsValid)
{
messageTemplate = model.ToEntity(messageTemplate);
messageTemplate = _mapper.Map(model, messageTemplate);
//attached file
if (!model.HasAttachedDownload)
messageTemplate.AttachedDownloadId = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Grand.Business.Core.Extensions;
using AutoMapper;
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Marketing.Newsletters;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Domain.Messages;
using Grand.Web.Admin.Extensions.Mapping;
using Grand.Web.Admin.Models.Messages;
using Grand.Web.Common.DataSource;
Expand All @@ -19,11 +21,13 @@ public class NewsletterCategoryController : BaseAdminController
public NewsletterCategoryController(
INewsletterCategoryService newsletterCategoryService,
ILanguageService languageService,
ITranslationService translationService)
ITranslationService translationService,
IMapper mapper)
{
_newsletterCategoryService = newsletterCategoryService;
_languageService = languageService;
_translationService = translationService;
_mapper = mapper;
}

#endregion
Expand All @@ -33,6 +37,7 @@ public NewsletterCategoryController(
private readonly INewsletterCategoryService _newsletterCategoryService;
private readonly ILanguageService _languageService;
private readonly ITranslationService _translationService;
private readonly IMapper _mapper;

#endregion

Expand Down Expand Up @@ -81,7 +86,7 @@ public async Task<IActionResult> Create(NewsletterCategoryModel model, bool cont
{
if (ModelState.IsValid)
{
var newsletterCategory = model.ToEntity();
var newsletterCategory = _mapper.Map<NewsletterCategory>(model);
await _newsletterCategoryService.InsertNewsletterCategory(newsletterCategory);
Success(_translationService.GetResource("admin.marketing.NewsletterCategory.Added"));
return continueEditing
Expand All @@ -99,7 +104,7 @@ public async Task<IActionResult> Edit(string id)
if (newsletterCategory == null)
return RedirectToAction("List");

var model = newsletterCategory.ToModel();
var model = _mapper.Map<NewsletterCategoryModel>(newsletterCategory);

//locales
await AddLocales(_languageService, model.Locales, (locale, languageId) =>
Expand All @@ -122,7 +127,7 @@ public async Task<IActionResult> Edit(NewsletterCategoryModel model, bool contin

if (ModelState.IsValid)
{
newsletterCategory = model.ToEntity(newsletterCategory);
newsletterCategory = _mapper.Map(model, newsletterCategory);
await _newsletterCategoryService.UpdateNewsletterCategory(newsletterCategory);

Success(_translationService.GetResource("admin.marketing.NewsletterCategory.Updated"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Interfaces.Common.Directory;
using AutoMapper;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Business.Core.Interfaces.Marketing.Newsletters;
Expand Down Expand Up @@ -27,14 +28,15 @@ public class NewsLetterSubscriptionController : BaseAdminController
private readonly IStoreService _storeService;
private readonly ITranslationService _translationService;
private readonly IWorkContext _workContext;

private readonly IMapper _mapper;
public NewsLetterSubscriptionController(INewsLetterSubscriptionService newsLetterSubscriptionService,
INewsletterCategoryService newsletterCategoryService,
IDateTimeService dateTimeService,
ITranslationService translationService,
IStoreService storeService,
IGroupService groupService,
IWorkContext workContext)
IWorkContext workContext,
IMapper mapper)
{
_newsLetterSubscriptionService = newsLetterSubscriptionService;
_newsletterCategoryService = newsletterCategoryService;
Expand All @@ -43,6 +45,7 @@ public NewsLetterSubscriptionController(INewsLetterSubscriptionService newsLette
_storeService = storeService;
_groupService = groupService;
_workContext = workContext;
_mapper = mapper;
}

[NonAction]
Expand Down Expand Up @@ -132,7 +135,7 @@ public async Task<IActionResult> SubscriptionList(DataSourceRequest command, New
var items = new List<NewsLetterSubscriptionModel>();
foreach (var x in newsletterSubscriptions)
{
var m = x.ToModel();
var m = _mapper.Map<NewsLetterSubscriptionModel>(x);
var store = await _storeService.GetStoreById(x.StoreId);
m.StoreName = store != null ? store.Shortcut : "Unknown store";
m.CreatedOn = _dateTimeService.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc)
Expand Down
11 changes: 7 additions & 4 deletions src/Web/Grand.Web.Admin/Controllers/OrderStatusController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Interfaces.Checkout.Orders;
using AutoMapper;
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Domain.Orders;
using Grand.Web.Admin.Extensions.Mapping;
Expand All @@ -16,10 +17,11 @@ public class OrderStatusController : BaseAdminController
#region Constructors

public OrderStatusController(IOrderStatusService orderStatusService,
IOrderService orderService)
IOrderService orderService, IMapper mapper)
{
_orderStatusService = orderStatusService;
_orderService = orderService;
_mapper = mapper;
}

#endregion
Expand All @@ -28,6 +30,7 @@ public OrderStatusController(IOrderStatusService orderStatusService,

private readonly IOrderStatusService _orderStatusService;
private readonly IOrderService _orderService;
private readonly IMapper _mapper;

#endregion

Expand Down Expand Up @@ -56,7 +59,7 @@ public async Task<IActionResult> Update(OrderStatusModel model)
if (!ModelState.IsValid) return Json(new DataSourceResult { Errors = ModelState.SerializeErrors() });

var status = await _orderStatusService.GetById(model.Id);
status = model.ToEntity(status);
status = _mapper.Map(model, status);
await _orderStatusService.Update(status);

return new JsonResult("");
Expand All @@ -68,7 +71,7 @@ public async Task<IActionResult> Add(OrderStatusModel model)
if (!ModelState.IsValid) return Json(new DataSourceResult { Errors = ModelState.SerializeErrors() });

var status = new OrderStatus();
status = model.ToEntity(status);
status = _mapper.Map(model, status);
status.StatusId = (await _orderStatusService.GetAll()).Max(x => x.StatusId) + 10;
await _orderStatusService.Insert(status);

Expand Down
14 changes: 9 additions & 5 deletions src/Web/Grand.Web.Admin/Controllers/PermissionController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Extensions;
using AutoMapper;
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Security;
Expand All @@ -21,12 +22,14 @@ public class PermissionController : BaseAdminController
public PermissionController(IWorkContext workContext,
IPermissionService permissionService,
IGroupService groupService,
ITranslationService translationService)
ITranslationService translationService,
IMapper mapper)
{
_workContext = workContext;
_permissionService = permissionService;
_groupService = groupService;
_translationService = translationService;
_mapper = mapper;
}

#endregion
Expand All @@ -37,6 +40,7 @@ public PermissionController(IWorkContext workContext,
private readonly IPermissionService _permissionService;
private readonly IGroupService _groupService;
private readonly ITranslationService _translationService;
private readonly IMapper _mapper;

#endregion

Expand Down Expand Up @@ -82,7 +86,7 @@ public async Task<IActionResult> Create(PermissionCreateModel model)
{
if (!ModelState.IsValid) return View(model);

var permission = model.ToEntity();
var permission = _mapper.Map<Permission>(model);
await _permissionService.InsertPermission(permission);
return Content("");
}
Expand All @@ -93,7 +97,7 @@ public async Task<IActionResult> Update(string systemName)

var permission = await _permissionService.GetPermissionBySystemName(systemName);
if (permission == null) return Content("Permission not found");
var model = permission.ToModel();
var model = _mapper.Map<PermissionUpdateModel>(permission);
return View(model);
}

Expand All @@ -103,7 +107,7 @@ public async Task<IActionResult> Update(PermissionUpdateModel model)
if (!ModelState.IsValid) return View(model);

var permission = await _permissionService.GetPermissionById(model.Id);
permission = model.ToEntity(permission);
permission = _mapper.Map(model, permission);
await _permissionService.UpdatePermission(permission);
return Content("");
}
Expand Down
16 changes: 9 additions & 7 deletions src/Web/Grand.Web.Admin/Controllers/PluginController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Interfaces.Common.Localization;
using AutoMapper;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Utilities.Common.Security;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
Expand Down Expand Up @@ -26,7 +27,8 @@ public class PluginController(
IHostApplicationLifetime applicationLifetime,
IWorkContext workContext,
IServiceProvider serviceProvider,
ExtensionsConfig extConfig)
ExtensionsConfig extConfig,
IMapper mapper)
: BaseAdminController
{
#region Fields
Expand All @@ -40,16 +42,16 @@ public class PluginController(
#region Utilities

[NonAction]
protected virtual PluginModel PreparePluginModel(PluginInfo PluginInfo)
protected virtual PluginModel PreparePluginModel(PluginInfo pluginInfo)
{
var pluginModel = PluginInfo.ToModel();
var pluginModel = mapper.Map<PluginModel>(pluginInfo);
//logo
pluginModel.LogoUrl = PluginInfo.GetLogoUrl(workContext);
pluginModel.LogoUrl = pluginInfo.GetLogoUrl(workContext);

//configuration URLs
if (PluginInfo.Installed)
if (pluginInfo.Installed)
{
var pluginInstance = PluginInfo.Instance(serviceProvider);
var pluginInstance = pluginInfo.Instance(serviceProvider);
pluginModel.ConfigurationUrl = pluginInstance.ConfigurationUrl();
}

Expand Down
Loading

0 comments on commit 7577b23

Please sign in to comment.