public async Task <Unit> Handle(RefreshGarageStatusCommand request, CancellationToken cancellationToken) { var garage = await _garegeRepository.GetGarageByIdAsync(request.UserId); var door = garage?.Doors?.FirstOrDefault(x => x.IPAddress == request.IPAddress); var doorCurrentStatus = false; if (door is not null) { doorCurrentStatus = door.IsActive; door.UpdateStatus(request.Status); await _garegeRepository.UpdateDoorStatusAsync(door); } else { int countOfTry = 0; while (2 > countOfTry) { door = await _garegeRepository.GetDoorByIPAddressAsync(request.IPAddress); if (door is not null) { doorCurrentStatus = door.IsActive; door.UpdateStatus(request.Status); break; } door.UpdateStatus(false); countOfTry++; } } if (doorCurrentStatus != request.Status) { await _garegeRepository.AddHistoricalDoorStatusLogAsync( DoorsStatusHistory.CreateDoorsStatusHistory(door.Id, doorCurrentStatus, request.Status, DateTime.UtcNow)); } return(Unit.Value); }
public async Task AddHistoricalDoorStatusLogAsync(DoorsStatusHistory doorsStatusHistory) { await _context.Set <DoorsStatusHistory>().AddAsync(doorsStatusHistory); await _context.SaveChangesAsync(CancellationToken.None); }