예제 #1
0
        public async Task <ContentStorageItem> Save(string id, string category, IContent content,
                                                    CancellationToken cancellationToken)
        {
            var dbItem = await FindWithoutContent(id, category, cancellationToken);

            if (dbItem == null)
            {
                dbItem = new ContentStorageDbItem
                {
                    Id       = id,
                    Category = category
                };
                context.Items.Add(dbItem);
            }
            else
            {
                try
                {
                    context.Items.Update(dbItem);
                }
                catch (InvalidOperationException)
                {
                    dbItem = context.Items.Find(id, category);
                    context.Items.Update(dbItem);
                }
            }

            dbItem.Timestamp = DateTimeOffset.UtcNow;
            dbItem.Content   = await content.ReadAsByteArray(cancellationToken);

            dbItem.Size = content.Size;
            await context.SaveChangesAsync(cancellationToken);

            return(new ContentStorageItem(id, category, content, dbItem.Timestamp));
        }
예제 #2
0
 public ContentStorageItemContent(ContentStorageDbItem dbItem, IServiceScopeFactory serviceScopeFactory)
 {
     this.dbItem = dbItem ?? throw new ArgumentNullException(nameof(dbItem));
     this.serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory));
 }
예제 #3
0
 private ContentStorageItemContent GetItemContent(ContentStorageDbItem dbItem) =>
 new ContentStorageItemContent(dbItem, serviceScopeFactory);