Exemplo n.º 1
0
        private async Task <UserSettings> FindSettingsAsync(string userId)
        {
            var settings = await _store.FindAsync(userId);

            Ensure.That(settings, nameof(settings),
                        options => options.WithException(new NotFoundException(new ErrorDto(ErrorCode.NotFound, "User settings do not exist."))))
            .IsNotNull();

            return(settings);
        }
        public async Task <UserSettings> FindAsync(string userId)
        {
            var key = GetKey(userId);

            if (_cache.TryGetValue(key, out UserSettings settings))
            {
                return(settings);
            }

            await sync.WaitAsync();

            try
            {
                if (_cache.TryGetValue(key, out settings))
                {
                    return(settings);
                }

                settings = await _originalDataStore.FindAsync(userId);

                if (settings == null)
                {
                    return(null);
                }

                var options = new MemoryCacheEntryOptions
                {
                    AbsoluteExpiration = DateTimeOffset.UtcNow.AddHours(_options.UserSettingsExpiresInHours),
                    Priority           = CacheItemPriority.Normal
                };

                _cache.Set(key, settings, options);
                return(settings);
            }
            finally
            {
                sync.Release();
            }
        }
Exemplo n.º 3
0
        public async Task <SendNotificationResponse> PostAsync(SendNotificationRequest request)
        {
            Ensure.That(request, nameof(request)).IsNotNull();

            var settings = await _settingsStore.FindAsync(request.RecipientUserId);

            if (settings == null)
            {
                throw new NotFoundException(new ErrorDto(ErrorCode.NotFound, "User settings do not exist."));
            }

            var message = Mapper.Map <NotificationMessage>(request);

            var response = new SendNotificationResponse();

            foreach (var sender in _senders)
            {
                var senderResult = await sender.SendAsync(message, settings);

                if (senderResult.Status != NotificationSendingStatus.Skipped)
                {
                    response.Results.Add(senderResult);
                }
            }

            if (response.Results.Any(x => x.Status == NotificationSendingStatus.Success))
            {
                var newRecord = Mapper.Map <NotificationRecord>(request);
                newRecord.UserSettingsId = settings.Id;

                var record = await _historyStore.SaveAsync(newRecord);

                response.NotificationRecordId = record.Id;
            }

            return(response);
        }