-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathOrderCreatedCreateDataRecomendationHandler.cs
More file actions
61 lines (58 loc) · 2.38 KB
/
OrderCreatedCreateDataRecomendationHandler.cs
File metadata and controls
61 lines (58 loc) · 2.38 KB
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
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Orders.Models;
namespace SimplCommerce.Module.Orders.Events
{
public class OrderCreatedCreateDataRecomendationHandler : INotificationHandler<OrderCreated>
{
private readonly ILogger _logger;
private readonly IRepository<OrderHistory> _orderHistoryRepository;
private static string dataPath = Path.Combine(Environment.CurrentDirectory, "MLData/data.txt");
public OrderCreatedCreateDataRecomendationHandler(
IRepository<OrderHistory> orderHistoryRepository,
ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<OrderCreatedCreateDataRecomendationHandler>();
_orderHistoryRepository = orderHistoryRepository;
}
public async Task Handle(OrderCreated notification, CancellationToken cancellationToken)
{
try
{
if (notification.Order != null
&& notification.Order.OrderItems != null && notification.Order.OrderItems.Count > 1)
{
StringBuilder sb = new StringBuilder();
for (var i = 0; i < notification.Order.OrderItems.Count - 1; i++)
{
var item = notification.Order.OrderItems[i];
for (var i2 = i + 1; i2 <= i + 2; i2++)
{
if (i2 < notification.Order.OrderItems.Count)
{
var item2 = notification.Order.OrderItems[i2];
if (item.ProductId != item2.ProductId)
{
sb.AppendLine(string.Format("{0}\t{1}", item.ProductId, item2.ProductId));
}
}
}
}
await File.AppendAllTextAsync(dataPath, sb.ToString());
}
}
catch (Exception ex)
{
_logger.LogError(ex, $"Failed executing event handler {GetType().Name}: {ex.Message}");
}
}
}
}