DEV Community

Cover image for Creating a NopCommerce plugin with .Net Core and Angular - Part I
Diógenes Polanco
Diógenes Polanco

Posted on • Edited on

7 2

Creating a NopCommerce plugin with .Net Core and Angular - Part I

Objective:

We will create a plugin for nopcommerce taking advantage of the power of aspnet core and the angular fluids for the frontend. We will create a plugin that will import the products from an external service.


You have to understand how to:

Create a plugin for nopCommerce?

The add-ons are specialized modules which maximize the functionality of nopcommerce such as payment processors, shipping provider, etc. Currently you can take advantage of their official documentation by entering this link.

Develop a webapp with AspNet Core?

The applications with asp net core are very easy to develop these days and when I say easy it is because I remember how it was a bit complex to develop with aspnet in the previous versions. but in another article I will talk about the differences between .Net vs .Net Core. So for the purposes you must also have basic knowledge of aspnet core and can start with this link.

Project Architecture

The product importer plugin is responsible for extracting the products from the ODataProducts service and rules configured in the plugin to upload them to the nopcommerce catalog.

Source code

This plugin only has one component that is responsible for importing the products of the external service so we will only have to develop a class that has the functionality to synchronize the products of the service against the nopcommerce catalog for that we will use C#.

You have to create a console test project in which we will do the service.

Let's execute the following commands:

~/ProductsImport$ dotnet  new console 
~/ProductsImport$ dotnet add package Newtonsoft.Json

We will create the following classes:

using System;
namespace ProductsImport
{
public class Product
{
public int ProductID { get; set; }
public string QuantityPerUnit { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public int ReorderLevel { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", ProductName, QuantityPerUnit);
}
}
}
view raw Product.cs hosted with ❤ by GitHub

using System;
using System.Collections.Generic;
namespace ProductsImport
{
public class ResponseProduct
{
public List<Product> Value { get; set; }
}
}

using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using Newtonsoft.Json;
namespace ProductsImport
{
public class ProductsImportHttpClient
{
HttpClient client = new HttpClient();
public ProductsImportHttpClient()
{
var urlodata = "https://services.odata.org/V3/Northwind/Northwind.svc/Products?$format=json";
client.BaseAddress = new Uri(urlodata);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<ResponseProduct> GetAsync(CancellationToken cancellationToken)
{
await Task.Delay(3000);
cancellationToken.ThrowIfCancellationRequested();
HttpResponseMessage response = await client.GetAsync("", cancellationToken);
if (response.IsSuccessStatusCode)
{
var stringResult = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<ResponseProduct>(stringResult);
}
return new ResponseProduct();
}
}
}

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ProductsImport
{
class Program
{
private static CancellationTokenSource tokenSource;
static void Main(string[] args)
{
tokenSource = new CancellationTokenSource();
var client = new ProductsImportHttpClient();
Task<ResponseProduct> productsTask = client.GetAsync(tokenSource.Token);
productsTask.ContinueWith(task =>
{
List<Product> products = task.Result.Value;
foreach (var product in products)
Console.WriteLine(product.ToString());
Environment.Exit(0);
},
TaskContinuationOptions.OnlyOnRanToCompletion);
productsTask.ContinueWith(
HandleError,
TaskContinuationOptions.OnlyOnFaulted);
productsTask.ContinueWith(
HandleCancellation,
TaskContinuationOptions.OnlyOnCanceled);
HandleExit();
}
private static void HandleError(Task<ResponseProduct> task)
{
Console.WriteLine("\nThere was a problem retrieving data");
Environment.Exit(1);
}
private static void HandleCancellation(Task<ResponseProduct> task)
{
Console.WriteLine("\nThe operation was canceled");
Environment.Exit(0);
}
private static void HandleExit()
{
while (true)
switch (Console.ReadKey().Key)
{
case ConsoleKey.X:
tokenSource.Cancel();
break;
case ConsoleKey.Q:
Console.WriteLine();
Environment.Exit(0);
break;
default:
Console.WriteLine("Waiting...");
break;
}
}
}
}
view raw Program.cs hosted with ❤ by GitHub

The result of the console:

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
le0maxx profile image
Leomax

Is there a part 2 of this thanks?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs