| | | 1 | | using Microsoft.AspNetCore.SignalR; |
| | | 2 | | using Services.DataServices; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | |
| | | 5 | | namespace Services.SignalRServices |
| | | 6 | | { |
| | | 7 | | public class OrderHistoryHub : Hub |
| | | 8 | | { |
| | | 9 | | private readonly IOrderHistoryDataService _orderHistoryService; |
| | | 10 | | |
| | 0 | 11 | | public OrderHistoryHub(IOrderHistoryDataService orderHistoryService) |
| | | 12 | | { |
| | 0 | 13 | | _orderHistoryService = orderHistoryService; |
| | 0 | 14 | | } |
| | | 15 | | |
| | | 16 | | // Этот метод будет вызываться из JavaScript |
| | | 17 | | public async Task RequestRecentChanges() |
| | | 18 | | { |
| | | 19 | | try |
| | | 20 | | { |
| | 0 | 21 | | var changes = await _orderHistoryService.GetRecentChangesAsync(50); |
| | | 22 | | |
| | | 23 | | // Преобразуем для безопасной передачи |
| | 0 | 24 | | var safeChanges = changes.Select(c => new |
| | 0 | 25 | | { |
| | 0 | 26 | | c.Id, |
| | 0 | 27 | | c.OrderId, |
| | 0 | 28 | | ColumnName = c.ColumnName ?? "", |
| | 0 | 29 | | ColumnDisplayName = c.ColumnDisplayName ?? "", |
| | 0 | 30 | | OldValue = c.OldValue, |
| | 0 | 31 | | NewValue = c.NewValue, |
| | 0 | 32 | | ChangedAt = c.ChangedAt, |
| | 0 | 33 | | ChangedBy = c.ChangedBy ?? "" |
| | 0 | 34 | | }).ToList(); |
| | | 35 | | |
| | | 36 | | // Отправляем данные обратно caller'у |
| | 0 | 37 | | await Clients.Caller.SendAsync("ReceiveRecentChanges", safeChanges); |
| | 0 | 38 | | } |
| | 0 | 39 | | catch (Exception ex) |
| | | 40 | | { |
| | 0 | 41 | | Console.WriteLine($"Error in RequestRecentChanges: {ex.Message}"); |
| | | 42 | | // Можно отправить ошибку клиенту |
| | 0 | 43 | | await Clients.Caller.SendAsync("ReceiveError", ex.Message); |
| | | 44 | | } |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | // Метод для отправки уведомлений о новых изменениях |
| | | 48 | | public async Task SendChangeNotification(object change) |
| | | 49 | | { |
| | 0 | 50 | | await Clients.All.SendAsync("ReceiveChange", change); |
| | 0 | 51 | | } |
| | | 52 | | } |
| | | 53 | | } |