//using Raven.Client.Documents;
//using Raven.Client.Documents.Linq;
//using Raven.Client.Documents.Session;
//using System.Collections.Generic;
//using System.Threading.Tasks;
//using zero.Core.Collections;
//using zero.Core.Database.Indexes;
//using zero.Core.Entities;
//using zero.Core.Extensions;
//using zero.Core.Utils;
//namespace zero.Core.Api
//{
// public class RecycleBinApi : BackofficeApi, IRecycleBinApi
// {
// RecycledEntity Blueprint;
// public RecycleBinApi(IStoreContext store, RecycledEntity blueprint) : base(store)
// {
// Blueprint = blueprint;
// }
// ///
// public async Task> Add(TEntity model, string group = null, string operationId = null) where TEntity : ZeroEntity
// {
// RecycledEntity entity = Blueprint.Clone();
// entity.Group = group;
// entity.Content = model;
// entity.OriginalId = model.Id;
// entity.OperationId = operationId;
// entity.Name = model.Name;
// return await SaveModel(entity);
// }
// ///
// public async Task>> Add(IEnumerable models, string group = null) where TEntity : ZeroEntity
// {
// IList results = new List();
// string operationId = IdGenerator.Create();
// foreach (TEntity model in models)
// {
// EntityResult result = await Add(model, group, operationId);
// if (!result.IsSuccess)
// {
// return EntityResult>.Fail(result.Errors);
// }
// results.Add(result.Model);
// }
// return EntityResult>.Success(results);
// }
// ///
// public async Task GetById(string id)
// {
// return await GetById(id);
// }
// ///
// public async Task> GetByQuery(RecycleBinListQuery query)
// {
// query.SearchSelector = x => x.Name;
// return await Session.Query()
// .WhereIf(x => x.Group == query.Group, !query.Group.IsNullOrWhiteSpace())
// .WhereIf(x => x.OperationId == query.OperationId, !query.OperationId.IsNullOrWhiteSpace())
// .ToQueriedListAsync(query);
// }
// ///
// public async Task> GetByOperation(string operationId)
// {
// return await Session.Query()
// .Where(x => x.OperationId == operationId)
// .ToListAsync();
// }
// ///
// /// Get affected entity count from a specific operation
// ///
// public async Task GetCountByOperation(string operationId)
// {
// return await Session.Query()
// .Where(x => x.OperationId == operationId)
// .CountAsync();
// }
// ///
// public async Task> Delete(string id)
// {
// return await DeleteById(id);
// }
// ///
// public async Task> DeleteAll()
// {
// // TODO make Purge operations app-aware
// return await Purge();
// }
// ///
// public async Task> DeleteByOperation(string operationId)
// {
// return await Purge("where c.OperationId = $id", new Raven.Client.Parameters() { { "id", operationId } });
// }
// ///
// public async Task> DeleteByGroup(string group)
// {
// return await Purge("where c.Group = $group", new Raven.Client.Parameters() { { "group", group } });
// }
// }
// public interface IRecycleBinApi
// {
// ///
// /// Adds an entity to the recycle bin. This operation will not remove this entity from it's own collection!
// ///
// Task> Add(TEntity model, string group = null, string operationId = null) where TEntity : ZeroEntity;
// ///
// /// Adds the specified entities to the recycle bin. This operation will not remove entities from their own collection!
// ///
// Task>> Add(IEnumerable models, string group = null) where TEntity : ZeroEntity;
// ///
// /// Get recycled entity by Id
// ///
// Task GetById(string id);
// ///
// /// Get all recycled items
// ///
// Task> GetByQuery(RecycleBinListQuery query);
// ///
// /// Get affected entities from a specific operation
// ///
// Task> GetByOperation(string operationId);
// ///
// /// Get affected entity count from a specific operation
// ///
// Task GetCountByOperation(string operationId);
// //
// /// Deletes a recycled entity by Id
// ///
// Task> Delete(string id);
// ///
// /// Purges the recycle bin
// ///
// Task> DeleteAll();
// ///
// /// Deletes all recycled items from a specific operation
// ///
// Task> DeleteByOperation(string operationId);
// ///
// /// Deletes all recycled items from a specific group
// ///
// Task> DeleteByGroup(string group);
// }
//}
/*
* RESTORE FOR PAGES
*
*
*
///
/// Restores a page from the recycle bin
///
Task> Restore(string id, bool includeDescendants = false);
///
public async Task> Restore(string id, bool includeDescendants = false)
{
EntityResult result = new EntityResult();
RecycledEntity recycledEntity = await RecycleBinApi.GetById(id);
List entities = new List() { recycledEntity };
if (recycledEntity == null)
{
return EntityResult.Fail(); // TODO correct error message
}
// get descendants from the operation
if (includeDescendants && !recycledEntity.OperationId.IsNullOrEmpty())
{
entities = (await RecycleBinApi.GetByOperation(recycledEntity.OperationId)).ToList();
}
// fill ids
string[] ids = entities.Select(x => x.OriginalId).ToArray();
// check if parents are available
string[] parentIds = entities.Select(x => x.Content as Page).Where(x => x != null).Select(x => x.ParentId).ToArray();
parentIds = (await Load(parentIds)).Where(x => x.Value != null).Select(x => x.Value.Id).ToArray();
// validate and restore all items
foreach (RecycledEntity entity in entities)
{
// check if it contains page data
if (entity.Group != RECYCLE_BIN_GROUP || !(entity.Content is Page))
{
//result.AddError("Cannot parse recycled entity as an Page in group \"" + RECYCLE_BIN_GROUP + "\""); // TODO correct error message
continue;
}
// get page
Page page = entity.Content as Page;
page.IsActive = false;
// validate app and parent
if (!page.ParentId.IsNullOrEmpty() && !ids.Contains(page.ParentId) && !parentIds.Contains(page.ParentId))
{
// TODO correct error message
continue;
}
// restore to pages
EntityResult saveResult = await Create(page);
}
// delete affected entities from recycle bin
if (!recycledEntity.OperationId.IsNullOrEmpty())
{
await RecycleBinApi.DeleteByOperation(recycledEntity.OperationId);
}
// set result
result.Model = ids;
result.IsSuccess = true;
return result;
}
*/