using System;
using System.Threading.Tasks;
using zero.Core.Collections;
using zero.Core.Entities;
using zero.Core.Extensions;
using Rc = Raven.Client;
namespace zero.Core.Api
{
public class PreviewApi : BackofficeApi, IPreviewApi
{
Preview Blueprint;
public PreviewApi(ICollectionContext store, Preview blueprint) : base(store)
{
Blueprint = blueprint;
}
///
public async Task> Add(TEntity model) where TEntity : ZeroEntity
{
return await Update(null, model);
}
///
public async Task> Update(string id, TEntity model) where TEntity : ZeroEntity
{
Preview entity = id == null ? await GetById(id) : Blueprint.Clone();
entity.Content = model;
entity.OriginalId = model.Id;
entity.Name = model.Name;
return await SaveModel(entity, meta: x =>
{
x[Rc.Constants.Documents.Metadata.Expires] = GetExpiry(model);
});
}
///
public async Task GetById(string id)
{
return await GetById(id);
}
///
/// Get preview expiration for a document
///
DateTime GetExpiry(ZeroEntity model)
{
return DateTime.Now.AddHours(1);
}
}
public interface IPreviewApi
{
///
/// Adds an entity to the preview collection. This will generate a preview with an id which can be used for the preview view.
///
Task> Add(TEntity model) where TEntity : ZeroEntity;
///
/// Updates an entity in the preview collection. This will generate a preview with an id which can be used for the preview view.
///
Task> Update(string id, TEntity model) where TEntity : ZeroEntity;
///
/// Get preview entity by Id
///
Task GetById(string id);
}
}