public CheckNotification ToBusiness(Entities.CheckNotification checkNotification) { return(new CheckNotification { CheckNotificationId = checkNotification.CheckNotificationId, CheckId = checkNotification.CheckId, CheckResultId = checkNotification.CheckResultId, Version = checkNotification.Version, IsHealthy = checkNotification.IsHealthy, Time = TimeUtilities.LongToDateTimeOffset(checkNotification.Time), CheckResult = ToBusiness(checkNotification.CheckResult) }); }
public async Task <CheckNotification> CheckForNotificationAsync(string checkName, CancellationToken token) { var checkResultAndHealth = await GetCheckResultAndHealthAsync(checkName, token); if (checkResultAndHealth == null) { // Don't notify when there is no check result to act on. return(null); } var notification = await GetNotificationAsync(checkName, token); if (notification == null) { if (checkResultAndHealth.IsHealthy) { // Don't notify when the check is healthy and there is no previous notification. return(null); } notification = new Entities.CheckNotification { CheckId = checkResultAndHealth.CheckResult.CheckId, Version = 0 }; _context.CheckNotifications.Add(notification); } else if (notification.IsHealthy == checkResultAndHealth.IsHealthy) { // Don't notify the health status is not changing. return(null); } notification.CheckResultId = checkResultAndHealth.CheckResult.CheckResultId; notification.CheckResult = checkResultAndHealth.CheckResult; notification.Time = TimeUtilities.DateTimeOffsetToLong(_systemClock.UtcNow); notification.IsHealthy = checkResultAndHealth.IsHealthy; notification.Version++; // Add the notification record var record = new Entities.CheckNotificationRecord { CheckId = notification.CheckId, CheckResultId = notification.CheckResultId, Time = notification.Time, IsHealthy = notification.IsHealthy, Version = notification.Version, CheckNotification = notification }; _context.CheckNotificationRecords.Add(record); try { await _context.SaveChangesAsync(); return(_entityMapper.ToBusiness(notification)); } catch (DbUpdateConcurrencyException) { // This means another caller updated the notification already. return(null); } }