public void InsertFiles(DalFolder fldr, params DalFile[] fls) { if (ReferenceEquals(fldr, null)) { var error = new ArgumentNullException(nameof(fldr), "folder can't be null"); logger.Error(error, error.Message); throw error; } if (fls.Length == 0) { var error = new ArgumentOutOfRangeException(nameof(fls), "invalid files count"); logger.Error(error, error.Message); throw error; } var enumerable = fls.Select(item => item.ToOrmFile()); var folder = fldr.ToOrmFolder(); var files = enumerable as Files[] ?? enumerable.ToArray(); foreach (Files file in files) { folder.Files.Add(file); file.folderId = folder.id; file.Folder = folder; context.Entry(file).State = EntityState.Modified; } folder.dateTime = files[files.Length - 1].dateTime; context.Entry(folder).State = EntityState.Modified; }
public void Update(DalFolder entity) { if (ReferenceEquals(entity, null)) { var error = new ArgumentNullException(nameof(entity), "parametr can't be null"); logger.Error(error, error.Message); throw error; } var e = entity.ToOrmFolder(); if (e.ownerId.Equals(0)) { var error = new ArgumentException("incorrect Folder object"); logger.Error(error, error.Message); throw error; } var folder = context.Set <Folders>().Find(e.id); folder.User = e.User; folder.ownerId = e.ownerId; folder.dateTime = e.dateTime; folder.leftKey = e.leftKey; folder.rightKey = e.rightKey; folder.level = e.level; folder.name = e.name; folder.Files = e.Files; folder.UsersShared = e.UsersShared; context.Entry(folder).State = EntityState.Modified; }
public DalFolder Add(DalFolder prnt, string newFolderName) { if (ReferenceEquals(prnt, null)) { var error = new ArgumentNullException(nameof(prnt), "folder can't be null"); logger.Error(error, error.Message); throw error; } if (string.IsNullOrEmpty(newFolderName)) { var error = new ArgumentNullException(nameof(newFolderName), "new folder name can't be null or empty"); logger.Error(error, error.Message); throw error; } var parent = prnt.ToOrmFolder(); int leftKey = parent.rightKey; int rightKey = parent.rightKey + 1; foreach (Folders item in context.Set <Folders>().Where(a => (a.leftKey > parent.rightKey) && (a.ownerId == parent.ownerId))) { item.leftKey = item.leftKey + 2; item.rightKey = item.rightKey + 2; context.Entry(item).State = EntityState.Modified; } foreach (Folders item in context.Set <Folders>().Where(a => (a.rightKey >= parent.rightKey) && (a.leftKey < parent.rightKey) && (a.ownerId == parent.ownerId))) { item.rightKey = item.rightKey + 2; context.Entry(item).State = EntityState.Modified; } var folder = new Folders { ownerId = parent.ownerId, name = newFolderName, dateTime = DateTime.Now, level = parent.level + 1, leftKey = leftKey, rightKey = rightKey, Files = new HashSet <Files>(), User = null, UsersShared = new HashSet <Users>() }; Create(folder); return(folder.ToDalFolder()); }
public IEnumerable <DalFolder> GetChildNodes(DalFolder fldr) { if (ReferenceEquals(fldr, null)) { var error = new ArgumentNullException(nameof(fldr), "folder can't be null"); logger.Error(error, error.Message); throw error; } var folder = fldr.ToOrmFolder(); return(context.Set <Folders>().Where(a => (a.leftKey >= folder.leftKey) && (a.rightKey <= folder.rightKey) && (a.ownerId == folder.ownerId)).AsEnumerable().Select(item => item.ToDalFolder())); }
public DalFolder GetPreviousLevelParentNode(DalFolder fldr) { if (ReferenceEquals(fldr, null)) { var error = new ArgumentNullException(nameof(fldr), "folder can't be null"); logger.Error(error, error.Message); throw error; } var folder = fldr.ToOrmFolder(); return(context.Set <Folders>().FirstOrDefault(a => (a.leftKey <= folder.leftKey) && (a.rightKey >= folder.rightKey) && (a.level == folder.level - 1) && (a.ownerId == folder.ownerId)).ToDalFolder()); }
public void Delete(DalFolder entity) { if (ReferenceEquals(entity, null)) { var error = new ArgumentNullException(nameof(entity), "parametr can't be null"); logger.Error(error, error.Message); throw error; } var e = entity.ToOrmFolder(); var possibleFolder = context.Set <Folders>().Single(folder => folder.id == e.id); if (ReferenceEquals(possibleFolder, null)) { throw new ArgumentNullException(nameof(possibleFolder), "didn't find equally Folder In database"); } long id = GetParentsNodes(possibleFolder).First().id; context.Set <Folders>().RemoveRange(context.Set <Folders>().Where(a => (a.leftKey >= possibleFolder.leftKey) && (a.rightKey <= possibleFolder.rightKey) && (a.ownerId == possibleFolder.ownerId)).AsEnumerable()); int range = possibleFolder.rightKey - possibleFolder.leftKey + 1; foreach (Folders item in context.Set <Folders>().Where(a => (a.rightKey > possibleFolder.rightKey) && (a.ownerId == possibleFolder.ownerId))) { if (item.leftKey > possibleFolder.leftKey) { item.leftKey = item.leftKey - range; } item.rightKey = item.rightKey - range; context.Entry(item).State = EntityState.Modified; } /*foreach (Folders item in context.Set<Folders>().Where(a => a.ownerId == possibleFolder.ownerId)) * { * item.id = id; * id++; * * context.Entry(item).State = EntityState.Modified; * }*/ }
DalFolder IRepository <DalFolder> .Create(DalFolder entity) => Create(entity.ToOrmFolder()).ToDalFolder();