Hello guys! I am very begineer with NOP-Commerce. I want to create a method which returns all the categories?

May 25, 2016 at 6:42 AM
Guys please guide me how can i create a method to return all the categories. This is what i have done until now but stuck at a point where need to implement Json object conversion.

I have created private object of ICategoryService at class level and then initialize that object in a constructor like this below.
public class ApiController : BaseController
        private ICustomerService _customerService;
        private IOrderService _orderService;
        private IWorkContext _workContext;
        private IVendorService _vendorService;
        private IProductAttributeParser _productAttributeParser;
        private RestServiceSettings _settings;
        private CustomerSettings _customerSettings;

        private ICategoryService _categoryService;

     public ApiController(
            ICustomerService customerService,
            IOrderService orderService,
            IWorkContext workContext,
            IProductAttributeParser productAttributeParser,
            IVendorService vendorService,
            IProductService productService,
            ICategoryService categoryService,
            RestServiceSettings settings,
            CustomerSettings customerSettings)
            _customerService = customerService;
            _orderService = orderService;
            _workContext = workContext;
            _productAttributeParser = productAttributeParser;
            _vendorService = vendorService;

            _categoryService = categoryService;

            _settings = settings;
            _customerSettings = customerSettings;
Below is my ActionMethod seems to be very simple. Every step is simple until i have to create a method GetCategoryJson().
Please guide me what is going on in the same method for Customer i.e. GetCustomerJson()
so i can write GetCategoryJson()...
        public ActionResult GetAllCategories(string apiToken)
            if (!IsApiTokenValid(apiToken))
                return InvalidApiToken(apiToken);

            var categories = _categoryService.GetAllCategories();

            if (categories == null)
                return ErrorOccured("Categories not found.");

            return Successful(GetCategoryJson(customer));
Below it is GetCustomerJson(). I want to create similar method for GetCatgoryJson().
private object GetCustomerJson(Customer customer)
            // TODO: refactor into own method for reuse
            var customerRoles = customer.CustomerRoles
                .Select(c =>
                        Id = c.Id,
                        Name = c.Name,
                        SystemName = c.SystemName

            // TODO: refactor into own method for reuse
            var externalAuthenticationRecords = customer.ExternalAuthenticationRecords
                .Select(e =>
                        Id = e.Id,
                        CustomerId = e.CustomerId,
                        Email = e.Email,
                        ExternalIdentifier = e.ExternalIdentifier,
                        ExternalDisplayIdentifier = e.ExternalDisplayIdentifier,
                        OAuthToken = e.OAuthToken,
                        OAuthAccessToken = e.OAuthAccessToken,
                        ProviderSystemName = e.ProviderSystemName

            // TODO: refactor into own method for reuse
            var shoppingCartItem = customer.ShoppingCartItems
                .Select(c =>
                        Id = c.Id,
                        StoreId = c.StoreId,
                        ShoppingCartTypeId = c.ShoppingCartTypeId,
                        CustomerId = c.CustomerId,
                        ProductId = c.ProductId,
                        AttributesXml = c.AttributesXml,
                        CustomerEnteredPrice = c.CustomerEnteredPrice,
                        Quantity = c.Quantity,
                        CreatedOnUtc = c.CreatedOnUtc,
                        UpdatedOnUtc = c.UpdatedOnUtc,
                        IsFreeShipping = c.IsFreeShipping,
                        IsShipEnabled = c.IsShipEnabled,
                        AdditionalShippingCharge = c.AdditionalShippingCharge,
                        IsTaxExempt = c.IsTaxExempt

            var customerJson = new
                Id = customer.Id,
                CustomerGuid = customer.CustomerGuid,
                UserName = customer.Username,
                Email = customer.Email,
                CustomerRoles = customerRoles,
                AdminComment = customer.AdminComment,
                IsTaxExempt = customer.IsTaxExempt,
                AffiliateId = customer.AffiliateId,
                VendorId = customer.VendorId,
                HasShoppingCartItems = customer.HasShoppingCartItems,
                Active = customer.Active,
                Deleted = customer.Deleted,
                IsSystemAccount = customer.IsSystemAccount,
                SystemName = customer.SystemName,
                LastIpAddress = customer.LastIpAddress,
                CreatedOnUtc = customer.CreatedOnUtc,
                LastLoginDateUtc = customer.LastLoginDateUtc,
                LastActivityDateUtc = customer.LastActivityDateUtc,
                ExternalAuthenticationRecords = externalAuthenticationRecords,
                ShoppingCartItems = shoppingCartItem

            return customerJson;