-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorker.cs
178 lines (163 loc) · 9.15 KB
/
Worker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using TrendyolOrderService.DBContext;
using TrendyolOrderService.Context;
using TrendyolOrderService.Models;
using TrendyolOrderService.Utils;
namespace TrendyolOrderService
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
string apiKey = "";
string apiSecret= "";
string supplierId = "";
string token = TokenManager.GetToken($"{apiKey}:{apiSecret}");
var client = new RestClient("https://api.trendyol.com/sapigw/suppliers/" + supplierId + "/orders");
var request = new RestRequest
{
Method = RestSharp.Method.GET,
Timeout = -1
};
request.AddHeader("Authorization", $"Basic {token}");
request.AddParameter("text/plain", "", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
TrendyolOrderContext.TrendyolRest deserializedTrendyolResponse = JsonConvert.DeserializeObject<TrendyolOrderContext.TrendyolRest>(response.Content);
//try
//{ }
var order = new TrendyolOrders();
using (var dbContext = new TrendyolDBContext())
{
foreach (var content in deserializedTrendyolResponse.content)
{
if (!dbContext.TrendyolOrders.Any(a => a.OrderNumber == content.orderNumber))
{
order = new TrendyolOrders();
_ = dbContext.Add(new TrendyolOrders_ShipmentAddresses
{
FirstName = content.shipmentAddress.firstName,
LastName = content.shipmentAddress.lastName,
Id = content.shipmentAddress.id,
Address1 = content.shipmentAddress.address1,
Address2 = content.shipmentAddress.address2,
City = content.shipmentAddress.city,
CityCode = content.shipmentAddress.cityCode,
CountryCode = content.shipmentAddress.countryCode,
District = content.shipmentAddress.district,
DistrictId = content.shipmentAddress.districtId,
PostalCode = content.shipmentAddress.postalCode,
Neighborhood = content.shipmentAddress.neighborhood,
NeighborhoodId = content.shipmentAddress.neighborhoodId,
FullAddress = content.shipmentAddress.fullAddress,
FullName = content.shipmentAddress.fullName
});
dbContext.SaveChanges();
order.ShipmentAddressId = content.shipmentAddress.id;
order.OrderNumber = content.orderNumber;
_ = dbContext.Add(new TrendyolOrders_InvoiceAddresses
{
firstName = content.invoiceAddress.firstName,
lastName = content.invoiceAddress.lastName,
id = content.invoiceAddress.id,
address1 = content.invoiceAddress.address1,
address2 = content.invoiceAddress.address2,
city = content.invoiceAddress.city,
company = content.invoiceAddress.company,
cityCode = content.invoiceAddress.cityCode,
countryCode = content.invoiceAddress.countryCode,
district = content.invoiceAddress.district,
districtId = content.invoiceAddress.districtId,
neighborhood = content.invoiceAddress.neighborhood,
neighborhoodId = content.invoiceAddress.neighborhoodId,
fullAddress = content.invoiceAddress.fullAddress,
fullName = content.invoiceAddress.fullName
});
dbContext.SaveChanges();
order.InvoiceAddressId = content.invoiceAddress.id;
foreach (var pack in content.lines)
{
_ = dbContext.Add(new TrendyolOrders_Lines
{
id = pack.id,
quantity = pack.quantity,
salesCampaignId = pack.salesCampaignId,
productSize = pack.productSize,
merchantSku = pack.merchantSku,
productName = pack.productName,
productCode = pack.productCode,
merchantId = pack.merchantId,
amount = pack.amount,
discount = pack.discount,
lineItemPrice = pack.discountDetails[0].lineItemPrice,
lineItemDiscount = pack.discountDetails[0].lineItemDiscount,
currencyCode = pack.currencyCode,
productColor = pack.productColor,
sku = pack.sku,
vatBaseAmount = pack.vatBaseAmount,
barcode = pack.barcode,
orderLineItemStatusName = pack.orderLineItemStatusName,
price = pack.price,
orderNumber = order.OrderNumber
});
}
foreach (var pack in content.packageHistories)
{
_ = dbContext.Add(new TrendyolOrders_PackageHistories
{
orderNumber = order.OrderNumber,
createdDate = pack.createdDate,
status = pack.status
});
}
order.TaxNumber = content.taxNumber?.ToString();
order.TotalDiscount = content.totalDiscount;
order.GrossAmount = content.grossAmount;
order.customerFirstName = content.customerFirstName;
order.customerEmail = content.customerEmail;
order.customerId = content.customerId;
order.customerLastName = content.customerLastName;
order.cargoTrackingNumber = content.cargoTrackingNumber;
order.cargoTrackingLink = content.cargoTrackingLink;
order.cargoSenderNumber = content.cargoSenderNumber;
order.cargoProviderName = content.cargoProviderName;
order.orderDate = content.orderDate;
order.tcIdentityNumber = content.tcIdentityNumber;
order.shipmentPackageStatus = content.shipmentPackageStatus;
order.deliveryType = content.deliveryType;
order.timeSlotId = content.timeSlotId;
order.scheduledDeliveryStoreId = content.scheduledDeliveryStoreId;
order.estimatedDeliveryStartDate = content.estimatedDeliveryStartDate;
order.estimatedDeliveryEndDate = content.estimatedDeliveryEndDate;
order.totalPrice = content.totalPrice;
_ = dbContext.Add(order);
_ = dbContext.SaveChanges();
}
}
}
//catch (Exception ex)
//{
// throw;
//}
_logger.LogInformation(response.Content);
Console.WriteLine(response.Content);
await Task.Delay(10 * 1000, stoppingToken);
}
}
}
}