Skip to content

Instantly share code, notes, and snippets.

@dicksonkimeu
Created October 21, 2019 18:48
Show Gist options
  • Save dicksonkimeu/81f731522a64ba69cc357f40c571311a to your computer and use it in GitHub Desktop.
Save dicksonkimeu/81f731522a64ba69cc357f40c571311a to your computer and use it in GitHub Desktop.
Products Sync
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json;
using SalesLifePOS.Services;
using SalesLifePOS.Data.Services;
using System.Linq;
using System.Net.Http;
using SalesLife.SDK;
using System.Collections.Generic;
using SalesLifePOS.Data;
using System.IO;
namespace SalesLife.HUB
{
public class SyncProducts
{
public string LogV = @"C:\\INTEGRATION\\LogVv.txt";
public async System.Threading.Tasks.Task<string> Products(string conn,
string xBaseURL, string xToken, string xTenantId)
{
string resp = "";
try
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer" + " " + xToken);
httpClient.DefaultRequestHeaders.Add("Abp.TenantId", xTenantId);
SalesLife.SDK.Client client = new SDK.Client(xBaseURL, httpClient);
SalesLife.SDK.PagedResultDtoOfGetProductForViewDtoResp pagedResultDto =
new PagedResultDtoOfGetProductForViewDtoResp();
pagedResultDto = await client.ApiServicesAppProductsGetallGetAsync(null,null,null,null,null
, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null
, null, null, null, null, null, null, null, null, null, null, null, null, null, 1);
int? TotalCount = pagedResultDto.result.TotalCount;
int x = 0;
while (TotalCount > x)
{
pagedResultDto = await client.ApiServicesAppProductsGetallGetAsync(null, null, null, null, null , null, null, null, null, null, null, null, null, null, null, null, null, null, null, null , null, null, null, null, null, null, null, null, null, null, null, null
, x, 1000);
List<GetProductForViewDto> pagedResults = new List<GetProductForViewDto>();
pagedResults = pagedResultDto.result.Items.ToList();
foreach (GetProductForViewDto prod in pagedResults)
{
using (var sourceDb = new SQLServerDb(conn))
{
Product y = sourceDb.Products.AsQueryable().Where(p => p.ProductID == prod.Product.ProductID).FirstOrDefault();
if (y != null)
{
y.CategoryID = 40;
y.SearchTerms = prod.Product.Name;
y.Name = prod.Product.Name;
y.StockUnits = int.Parse(prod.Product.StockUnits.ToString());
y.SafetyStockLevel = int.Parse(prod.Product.SafetyStockLevel.ToString());
y.ListPrice = decimal.Parse(prod.Product.ListPrice.ToString());
y.DealerPrice = decimal.Parse(prod.Product.DealerPrice.ToString());
y.TaxType = int.Parse(prod.Product.TaxType.ToString());
y.ProductCategoryId = "70624455100";
y.ProductSubCategoryId = "70624518201";
y.Integrated = true;
y.Id = Int32.Parse(prod.Product.Id.ToString());
sourceDb.Products.Update(y);
}
else
{
Product product = new Product();
product.ProductID = prod.Product.ProductID;
product.CategoryID = 40;
product.SearchTerms = prod.Product.Name;
product.Name = prod.Product.Name;
product.StockUnits = int.Parse( prod.Product.StockUnits.ToString());
product.SafetyStockLevel =int.Parse( prod.Product.SafetyStockLevel.ToString());
product.ListPrice =decimal.Parse( prod.Product.ListPrice.ToString());
product.DealerPrice =decimal.Parse( prod.Product.DealerPrice.ToString());
product.TaxType =int.Parse( prod.Product.TaxType.ToString());
product.Integrated = true;
product.ProductCategoryId = "70624455100";
product.ProductSubCategoryId = "70624518201";
product.Id= Int32.Parse(prod.Product.Id.ToString());
sourceDb.Products.Add(product);
}
await sourceDb.SaveChangesAsync();
sourceDb.Dispose();
}
}
x = x + 1000;
}
}
catch (Exception ex)
{
string error = "";
error = ex.Message + ex.Source + ex.StackTrace;
File.AppendAllText(LogV, DateTime.Now.ToShortDateString() + "" + "/" + "" + DateTime.Now.ToShortTimeString() + "" + "/Send ToAPI/Error/" + "" + ex.Message + "" + "/" + "" + ex.Source + "" + "/" + "" + ex.StackTrace);
resp = error;
}
return resp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment