public async Task <IEnumerable <Object> > GetAvailableItems(string userId) { if (String.IsNullOrWhiteSpace(userId)) { return(null); } FilterDefinition <AvailableFolder> filterFolder = FilterService <AvailableFolder> .GetFilterByOneParam("UserId", new ObjectId(userId)); FilterDefinition <AvailableNote> filterNote = FilterService <AvailableNote> .GetFilterByOneParam("UserId", new ObjectId(userId)); List <Object> result = new List <Object>(); List <string> folderIds = new List <string>(); List <AvailableFolder> AvailableFolerIds = await AccessFolders.Find(filterFolder).ToListAsync(); List <AvailableNote> AvailableNoteIds = await AccessNotes.Find(filterNote).ToListAsync(); foreach (AvailableFolder AvailableFolderId in AvailableFolerIds) { folderIds.Add(AvailableFolderId.FolderId); } foreach (AvailableFolder AvailableFolderId in AvailableFolerIds) { Folder folder = FolderService.GetFolder(AvailableFolderId.FolderId).GetAwaiter().GetResult(); if (folder != null && !folderIds.Contains(folder.ParentFolderId)) { JObject addToResult = JObject.FromObject(folder); addToResult.Add("Role", AvailableFolderId.Role); result.Add(addToResult); } } foreach (AvailableNote availableNote in AvailableNoteIds) { Note note = NoteService.GetNote(availableNote.NoteId).GetAwaiter().GetResult(); if (note != null && !folderIds.Contains(note.FolderId)) { JObject addToResult = JObject.FromObject(NoteService.GetNote(availableNote.NoteId).GetAwaiter().GetResult()); addToResult.Add("Role", availableNote.Role); result.Add(addToResult); } } return(result); }
public async Task <AvailableNote> GetAvailableNote(string noteId, string userId) { List <string> paramList = new List <string>() { "NoteId", "UserId" }; List <object> valueList = new List <object>() { new ObjectId(noteId), new ObjectId(userId) }; FilterDefinition <AvailableNote> filter = FilterService <AvailableNote> .GetFilterByTwoParam(paramList, valueList); if (await AccessNotes.Find(filter).FirstOrDefaultAsync() != null) { return(await AccessNotes.Find(filter).FirstOrDefaultAsync()); } return(null); }
public async Task CreateNewNoteAccess(string userId, string noteId, string role) { AvailableNote availableNote = await GetAvailableNote(noteId, userId); if (availableNote == null) { availableNote = new AvailableNote() { UserId = userId, NoteId = noteId, Role = role }; await AccessNotes.InsertOneAsync(availableNote); } else if (availableNote.Role != role) { availableNote.Role = role; await UpdateNoteAccess(availableNote); } }
public async Task UpdateNoteAccess(AvailableNote availableNote) => await AccessNotes.ReplaceOneAsync(new BsonDocument("_id", new ObjectId(availableNote.Id)), availableNote);