public async Task <DocumentElementDto> CreateNewNodeAsync(ObjectId documentElementID, ObjectId branchID, ObjectId userID, string nodeName, string comment) { try { await database.Connect().ConfigureAwait(false); FilterBase getFilter = new EqualityFilter <ObjectId>("_id", documentElementID); var element = (await database.Get(getFilter).ConfigureAwait(false)).FirstOrDefault(); if (element is null) { throw new ArgumentException("No document element was found for id"); } var branch = element.Branches.Find(b => b.BranchID == branchID); if (branch is null) { throw new ArgumentException("No branch found for this id"); } branch.BranchNodes.Add(BranchNode.GetEmptyNode(element.Type, dateService, userID, nodeName, comment)); await database.Update(element).ConfigureAwait(false); var dto = new DocumentElementDto(element); dto.SetBranches(element.Branches, userID); return(dto); } catch (Exception ex) when(ex.GetType() != typeof(ArgumentException)) { exceptionLogger.Log(new ApplicationError(ex), LogLevel.Error, logConfiguration); throw new DatabaseException("Error occurred while creating new node"); } }
public async Task <DocumentElementDto> DeleteBranchAsync(ObjectId elementID, ObjectId branchID, ObjectId userID) { try { await database.Connect().ConfigureAwait(false); FilterBase getFilter = new EqualityFilter <ObjectId>("_id", elementID); var element = (await database.Get(getFilter).ConfigureAwait(false)).FirstOrDefault(); if (element is null) { throw new ArgumentException("No document element was found for id"); } var branch = element.Branches.Find(branch => branch.BranchID == branchID); if (branch is null) { throw new ArgumentException("There is no branch for given id"); } element.Branches.Remove(branch); if (element.Branches.Count == 0) { element.Branches.Add(Branch.GetNewBranch("New branch", dateService, element.Type, new List <BranchAccess>() { new BranchAccess(userID, BranchAccessType.ReadWrite) }, userID)); } element.CurrentBranchID = element.Branches.First().BranchID; if (element.Branches.First().BranchNodes.Count == 0) { element.Branches.First().BranchNodes.Add(BranchNode.GetEmptyNode(element.Type, dateService, userID, "New node", "Comment")); } element.CurrentBranchNodeID = element.Branches.First().BranchNodes.First().BranchNodeID; await database.Update(element).ConfigureAwait(false); var dto = new DocumentElementDto(element); dto.SetBranches(element.Branches, userID); return(dto); } catch (Exception ex) when(ex.GetType() != typeof(ArgumentException)) { exceptionLogger.Log(new ApplicationError(ex), LogLevel.Error, logConfiguration); throw new DatabaseException("Error occurred while deleting branch"); } }