using System; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Extensions; using Rc = Raven.Client; namespace zero.Core.Api { public class PreviewApi : BackofficeApi, IPreviewApi { IPreview Blueprint; public PreviewApi(IBackofficeStore store, IPreview blueprint) : base(store) { Blueprint = blueprint; } /// public async Task> Add(TEntity model) where TEntity : IZeroEntity { return await Update(null, model); } /// public async Task> Update(string id, TEntity model) where TEntity : IZeroEntity { IPreview 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(IZeroEntity 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 : IZeroEntity; /// /// 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 : IZeroEntity; /// /// Get preview entity by Id /// Task GetById(string id); } }