How to add new tables into database and configure into NopCommerce and List in menu

How to add new tables into database and configure into NopCommerce and List in menu

How to add new tables into database and configure into NopCommerce and List in menu

access_timeFriday, 24 March, 2017 chat_bubble_outline0 comments

Add a new table in nop NopCommerce is very tedious job but if we work properly step by step then we can add table easily without any problem.

Here I am describing how to add new table in NopCommerce. I need to send bulk message to our users so I have created a table Name BulkMessageMaster.

Open visual studio with nop commerce solutions and follow the below steps

table structure

Step 1

First we go to Nop.Core solution in Libraries Directory and Open this path \Libraries\Nop.Core\Domain and create an entity under Folder Messages.

We create the Entity class and give name " BulkMessageMaster.cs ".

Here I am creating entity an existing folder; you can create entity in a new folder also.

\Libraries\Nop.Data\Mapping

/// <summary>

    /// Represents BulkMessageMaster entity

    /// </summary>

    public partial class BulkMessageMaster : BaseEntity

    {            

        public string UserName { get; set; }

        public int SmsSendCount { get; set; }

        public string MobileNumber { get; set; }     

        public bool IsActive { get; set; }

 

Step 2

Open Nop.Data solution on path path \Libraries\Nop.Data\Mapping and create a class under folder Messages.

Create a Mapping class which bind class to Database table and give name "BulkMessageMasterMap.cs".

Presentation\Nop.Admin\Models

public partial class BulkMessageMasterMap : NopEntityTypeConfiguration<BulkMessageMaster>

    {

        public BulkMessageMasterMap()

        {

            this.ToTable("BulkMessageMaster");

            this.HasKey(nls => nls.Id);

            this.Property(nls => nls.MobileNumber).IsRequired().HasMaxLength(50);

            this.Property(nls => nls.UserName).IsRequired().HasMaxLength(500);

        }

    }

 

Here Core part is finish. Now we go to Presentation Nop.Admin

Step 3

Open Nop. Admin solution on path Presentation\Nop.Admin\Models and create one folder under this path or in existing folder

Create a Model class for MVC and give name " BulkMessageMasterListModel.cs.cs" and BulkMessageMasterModel.cs (Same for Nop.Web)  

public partial class BulkMessageMasterListModel : BaseNopModel

    {

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.UserName")]

        public string UserName { get; set; }

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.MobileNumber")]

        public string MobileNumber { get; set; }

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.Active")]

        public bool IsActiveUser { get; set; }       

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.SmsSendCount")]

        public int SmsSendCount { get; set; }

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.MobileMessage")]

        public string MobileMessage { get; set; }

    }

 

public partial class BulkMessageMasterModel : BaseNopEntityModel

    {

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.UserName")]

        [AllowHtml]     

        public string UserName { get; set; }

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.SmsSendCount")]

        public int SmsSendCount { get; set; }

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.MobileNumber")]

        [AllowHtml]

        public string MobileNumber { get; set; }

        [NopResourceDisplayName("Admin.Promotions.BulkMessageMaster.Fields.Active")]

        public bool IsActive { get; set; }

        public int ActiveId { get; set; }

    }

 

images 

Step 4

Open Validators folder in Nop. Admin solution on path ~\Nop.Admin\Validators and create one class under Messages Folder

Create a Validator class for MVC and give name "BulkMessageMasterValidator.cs". (Same for Nop.Web)

Validator class

 

public partial class BulkMessageMasterValidator : BaseNopValidator<BulkMessageMasterModel>

    {

        public BulkMessageMasterValidator(ILocalizationService localizationService, IDbContext dbContext)

        {

            RuleFor(x => x.UserName).NotEmpty().WithMessage(localizationService.GetResource( "Admin.Promotions.BulkMessageMaster.Fields.UserName.Required"));

            RuleFor(x => x.MobileNumber).NotEmpty().WithMessage(localizationService.GetResource( "Admin.Promotions.BulkMessageMaster.Fields.MobileNumber.Required"));

                      SetStringPropertiesMaxLength<NewsLetterSubscription>(dbContext);

        }

    }

 

Step 5

Open Infrastructure folder in Nop. Admin solution on path ~\Nop.Admin\ Infrastructure Open AutoMapperConfiguration.cs class and write below code for Mapping Model to Entity and Entity to Model. (Same for Nop.Web)

Infrastructure

 

  //Bulk Messages

                cfg.CreateMap<BulkMessageMaster, BulkMessageMasterModel>()

                .ForMember(dest => dest.CustomProperties, mo => mo.Ignore());

                cfg.CreateMap<BulkMessageMasterModel, BulkMessageMaster>();

 

Step 6

Open Infrastructure folder in Nop. Admin solution on path ~\Nop.Admin\ Extensions

Open MappingExtensions.cs class and write below code for Mapping Model to Entity and Entity to Model. (Same for Nop.Web)

#region Bulk Message Master

        public static BulkMessageMasterModel ToModel(this BulkMessageMaster entity)

        {            return entity.MapTo<BulkMessageMaster, BulkMessageMasterModel>();        }

        public static BulkMessageMaster ToEntity(this BulkMessageMasterModel model)

        {            return model.MapTo<BulkMessageMasterModel, BulkMessageMaster>();        }

        public static BulkMessageMaster ToEntity(this BulkMessageMasterModel model, BulkMessageMaster destination)

        {            return model.MapTo(destination);        }

        #endregion

 

Bulk Message Master

Step 7

Open Messages  folder in Nop. Services  solution on path ~\Nop.Admin\ Services /Messages

Create a interface and give name " IBulkMessageMasterService.cs". (Same for Nop.Web)

void InsertBulkMessageMaster(BulkMessageMaster bulkMessageMaster);

        void UpdateBulkMessageMaster(BulkMessageMaster bulkMessageMaster);

 

        void DeleteBulkMessageMaster(BulkMessageMaster bulkMessageMaster);

        BulkMessageMaster GetBulkMessageMasterById(int bulkMessageMasterId);

        BulkMessageMaster GetBulkMessageByUserName(string userName);

        BulkMessageMaster GetBulkMessageByMobileNumber(string mobilenumber);

        int GetSmsCountByMobileNumber(string mobileNumber);

        IPagedList<BulkMessageMaster> GetAllBulkMessageMasters(string username = null, string mobileNumber = null,       

            bool? isActive = null, int smsSendCount = 0,

            int pageIndex = 0, int pageSize = int.MaxValue);

 

 

Create a Service and give name "BulkMessageMasterService.cs". (Same for Nop.Web)

This method is only for insert new record in BulkMessageMasterService Table. Here you can create methods whatever you want.

public partial class BulkMessageMasterService: IBulkMessageMasterService

{

    #region Fields

    public BulkMessageMasterService(IDbContext context,IRepository<BulkMessageMaster> bulkMessageRepository )

        {

            this._context = context;

            this._bulkMessageRepository = bulkMessageRepository;

        }

    #endregion

 

Web link diary

Step 8

Open Nop. Web.Framework solution on path  Presentation\Nop.Web.Framework and open DependencyRegistrar.cs and register your service and interface like this

// BulkMessageMaster

builder.RegisterType<BulkMessageMasterService>().As<IBulkMessageMasterService>().InstancePerLifetimeScope();

 

Step 9

Create new controller and give name "UserMasterController.cs"

public partial class BulkMessageMasterController : BaseAdminController

    {

        #region Fields

private readonly IBulkMessageMasterService _bulkMessageService;

        private readonly IPermissionService _permissionService;

        private readonly ILocalizationService _localizationService;

        private readonly ISMSMessageService _smsMessageService;

        private readonly IWorkContext _workContext;

        #endregion    }

}

 

Create View for action method.

Step 10

Now I need to Show Manage Bulk Messages to Admin Menu. To add menu First we

Open Nop. Web.Framework solution on path Libraries\Nop.Web.Framework and open DependencyRegistrar.cs and add a PermissionRecord for ManageBulkProducts

public static readonly PermissionRecord ManageBulkProducts = new PermissionRecord { Name = "Admin area.Manage Manage Bulk Products", SystemName = "ManageBulkProducts", Category = "Configuration" };

 

PermissionRecord

Second we have to add an entry in which menu you want to add your item. In my case I want to add this under promotion menu. So Open XML File name sitemap.config in Nop.Admin Solution on path Presentation/ Nop.Admin and add below entry.

      <siteMapNode SystemName="Bulk Messages" nopResource="Admin.Promotions.ManageBulkMessages" PermissionNames="ManageBulkMessages" controller="BulkMessageMaster" action="List"  IconClass="fa-dot-circle-o"/>

 

Third add an entry in PermissionRecord table for new menu.

Last give permission this new menu to admin.

Final Screen

After Save you will see you are added item like show in below images.

Show link on menu


0 Comments

Leave a Comment: