Exemple #1
0
        public async Task <Record <T>?> Get(string id, string?partitionKey = null, CancellationToken token = default)
        {
            id = id
                 .VerifyNotEmpty(nameof(id))
                 .ToLowerInvariant();

            try
            {
                ItemResponse <T> itemResponse = await _container.ReadItemAsync <T>(id, new PartitionKey(partitionKey ?? id), cancellationToken : token);

                itemResponse.VerifyAssert(x => IsValid(x.StatusCode), x =>
                {
                    string msg = $"{nameof(Get)}: Failed to {nameof(_container.ReadItemAsync)} in getting id={id}, StatusCode={x.StatusCode}";
                    _logger.LogError(msg);
                    return(msg);
                });

                return(itemResponse.StatusCode == HttpStatusCode.OK ? new Record <T>(itemResponse.Resource, (ETag)itemResponse.ETag) : default);
Exemple #2
0
        public async Task <ETag> Set(Record <T> record, CancellationToken token = default)
        {
            record.VerifyNotNull(nameof(record));
            record.Prepare();

            _logger.LogTrace($"{nameof(Set)}: {nameof(_container.UpsertItemAsync)} item, {Json.Default.Serialize(record.Value)}");

            var option = new ItemRequestOptions
            {
                IfMatchEtag = record.ETag !,
            };

            ItemResponse <T> itemResponse = await _container.UpsertItemAsync(record.Value, requestOptions : option, cancellationToken : token);

            itemResponse.VerifyAssert(x => IsValid(x.StatusCode), x =>
            {
                string msg = $"{nameof(Set)}: Failed to {nameof(_container.UpsertItemAsync)}, StatusCode={x.StatusCode}";
                _logger.LogError(msg);
                return(msg);
            });

            return((ETag)itemResponse.ETag);
        }