using FluentValidation; using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Documents.Session; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Core.Collections; using zero.Core.Database.Indexes; using zero.Core.Entities; using zero.Core.Extensions; using zero.Core.Validation; namespace zero.Core.Api { public class MediaFolderApi : BackofficeApi, IMediaFolderApi { IValidator Validator; public MediaFolderApi(ICollectionContext store, IValidator validator) : base(store) { Validator = validator; } /// public async Task GetById(string id) { return await GetById(id); } /// public async Task> GetAll(string parentId = null) { return await Session.Query() .WhereIf(x => x.ParentId == parentId, !parentId.IsNullOrEmpty(), x => x.ParentId == null) .OrderByDescending(x => x.Name) .ToListAsync(); } /// public async Task> GetAllAsTree(string parentId = null, string activeId = null) { List items = new List(); string[] openIds = new string[0] { }; IList folders = await Session.Query() .WhereIf(x => x.ParentId == parentId, !parentId.IsNullOrEmpty(), x => x.ParentId == null) .OrderByDescending(x => x.CreatedDate).ThenBy(x => x.Name) .ToListAsync(); // get hierarchy so we know if we should set the folder to open if (!activeId.IsNullOrEmpty()) { MediaFolder_ByHierarchy.Result result = await Session.Query() .ProjectInto() .Include(x => x.Path.Select(p => p.Id)) .FirstOrDefaultAsync(x => x.Id == activeId); if (result != null) { openIds = result.Path.Select(x => x.Id).ToArray(); } } // get children for all folders string[] folderIds = folders.Select(x => x.Id).ToArray(); IList children = await Session.Query() .ProjectInto() .Where(x => x.Id.In(folderIds)) .ToListAsync(); foreach (MediaFolder folder in folders) { int childCount = children.Count(x => x.Id == folder.Id); items.Add(new TreeItem() { Id = folder.Id, Name = folder.Name, HasChildren = childCount > 0, ChildCount = childCount, ParentId = folder.ParentId, Sort = folder.Sort, Icon = "fth-folder", IsOpen = openIds.Contains(folder.Id), IsInactive = !folder.IsActive, HasActions = true, Modifier = !folder.IsActive ? new TreeItemModifier() { Icon = "fth-minus-circle color-yellow", Name = "Inactive" } : null }); } //if (parentId.IsNullOrEmpty()) //{ // items.Add(new TreeItem() // { // Id = "recyclebin", // ParentId = null, // Sort = 99999, // Name = "@recyclebin.name", // Icon = "fth-trash", // HasChildren = false // }); //} return items; } /// public async Task> GetHierarchy(string id) { MediaFolder_ByHierarchy.Result result = await Session.Query() .ProjectInto() .Include(x => x.Path.Select(p => p.Id)) .FirstOrDefaultAsync(x => x.Id == id); if (result == null) { return new List(); } List ids = result.Path.Select(x => x.Id).ToList(); ids.Add(id); return (await Session.LoadAsync(ids)).Select(x => x.Value).ToList(); } /// public async Task> Save(MediaFolder model) { model.IsActive = true; return await SaveModel(model, Validator); } /// public async Task> Move(string id, string parentId) { MediaFolder model = await GetById(id); MediaFolder parent = await GetById(parentId); if (model == null || (!parentId.IsNullOrEmpty() && parent == null)) { return EntityResult.Fail("@errors.idnotfound"); } model.ParentId = parent?.Id; return await Save(model); } /// public async Task> Delete(string id) { return await DeleteById(id); } } public interface IMediaFolderApi : IBackofficeApi { /// /// Get application by Id /// Task GetById(string id); /// /// Get hierarchy for a folder /// Task> GetHierarchy(string id); /// /// Get all folders with the specified parent or on root /// Task> GetAll(string parentId = null); /// /// Get all folders with the specified parent or on root for tree output /// Task> GetAllAsTree(string parentId = null, string activeId = null); /// /// Creates or updates a folder /// Task> Save(MediaFolder model); /// /// Move a folder to a new parent /// Task> Move(string id, string parentId); /// /// Deletes a folder /// Task> Delete(string id); } }