public async Task <DocumentElementDto> CreateNewBranchAsync(ObjectId documentElementID, string branchName, ObjectId userID) { 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"); } element.Branches.Add(Branch.GetNewBranch(branchName, dateService, element.Type, new List <BranchAccess>() { new BranchAccess(userID, BranchAccessType.ReadWrite) }, userID)); 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 branch"); } }
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> DeleteNodeAsync(ObjectId documentElementID, ObjectId branchNodeID, ObjectId userID) { 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"); } element.DeleteNode(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 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"); } }
public async Task <IEnumerable <DocumentElementDto> > GetDocumentElementsAsync(ObjectId documentID, ObjectId parentItemID, ObjectId userID) { try { await database.Connect().ConfigureAwait(false); var documentStructure = await documentStructureService.GetDocumentStructureAsync(documentID) .ConfigureAwait(false); if (documentStructure is null) { throw new ArgumentException("No document structure found"); } FindItemRecursive(documentStructure.Items, parentItemID, out Item item); if (item is null) { throw new ArgumentException("No item for given id"); } var elementWrappers = (await database.Get(new EqualityFilter <ObjectId>("parentItemID", parentItemID)) .ConfigureAwait(false)).ToDictionary(w => w.ID, w => w); return(item.ElementsIds is null ? new DocumentElementDto[0] { } : item.ElementsIds.Select(id => { var wrapper = elementWrappers.GetValueOrDefault(id); if (wrapper is null) { return null; } DocumentElementDto dto = new DocumentElementDto(wrapper); dto.SetBranches(wrapper.Branches, userID); return dto; }).Where(dto => dto is {})); }
public async Task <DocumentElementDto> UpdateContentAsync(DocumentElementContentUpdateDto dto) { try { await database.Connect().ConfigureAwait(false); FilterBase getFilter = new EqualityFilter <ObjectId>("_id", dto.DocumentElementID); var element = (await database.Get(getFilter).ConfigureAwait(false)).FirstOrDefault(); if (element is null) { throw new ArgumentException("No document element was found for id"); } BranchNode node = element.FindBranchNode(dto.BranchNodeID); Console.WriteLine(dto.NewContent); #warning Replace with IDeserailizer node.DocumentElement = element.Type switch { DocumentElementType.Table => JsonConvert.DeserializeObject <Table>(dto.NewContent), DocumentElementType.Image => JsonConvert.DeserializeObject <Image>(dto.NewContent), DocumentElementType.Paragraph => JsonConvert.DeserializeObject <Paragraph>(dto.NewContent), DocumentElementType.NumberedList => JsonConvert.DeserializeObject <NumberedList>(dto.NewContent), _ => null, }; await database.Update(element).ConfigureAwait(false); var elementDto = new DocumentElementDto(element); elementDto.SetBranches(element.Branches, dto.UserID); return(elementDto); } catch (Exception ex) when(ex.GetType() != typeof(ArgumentException)) { exceptionLogger.Log(new ApplicationError(ex), LogLevel.Error, logConfiguration); throw new DatabaseException("Error occurred while updating node;s content"); } }