public async Task <BookmarkGroup?> CreateBookmarkGroup(string userId, string title, string color, int sortOrder, int bookmarkContainerId) { if (!BookmarkOwnershipTools .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) { return(null); } BookmarkGroup newBookmarkGroup = new(title, color, sortOrder, bookmarkContainerId); await this.db.BookmarkGroups.AddAsync(newBookmarkGroup); await this.db.SaveChangesAsync(); return(newBookmarkGroup); }
public async Task <BookmarkContainer?> UpdateBookmarkContainer(string userId, BookmarkContainer bookmarkContainer) { BookmarkContainer?existingBookmarkContainer = await this.db.BookmarkContainers .SingleOrDefaultAsync(bc => bc.BookmarkContainerId == bookmarkContainer.BookmarkContainerId); if (existingBookmarkContainer == null || !BookmarkOwnershipTools .IsBookmarkContainerOwner(this.db, userId, bookmarkContainer.BookmarkContainerId)) { return(null); } // If the sort order has changed, then the other containers need to be shuffled around if (existingBookmarkContainer.SortOrder < bookmarkContainer.SortOrder) { // The container has been moved to a higher sort order var affectedContainers = db.BookmarkContainers .Where(bc => bc.ApplicationUserId == userId) .Where(bc => bc.SortOrder > existingBookmarkContainer.SortOrder) .Where(bc => bc.SortOrder <= bookmarkContainer.SortOrder) .ToList(); affectedContainers.ForEach(bc => bc.SortOrder -= 1); // Let the save changes below save this } else if (existingBookmarkContainer.SortOrder > bookmarkContainer.SortOrder) { // The container has been moved to a lower sort order var affectedContainers = db.BookmarkContainers .Where(bc => bc.ApplicationUserId == userId) .Where(bc => bc.SortOrder < existingBookmarkContainer.SortOrder) .Where(bc => bc.SortOrder >= bookmarkContainer.SortOrder) .ToList(); affectedContainers.ForEach(bc => bc.SortOrder += 1); // Let the save changes below save this } this.db.Entry(bookmarkContainer).State = EntityState.Modified; await this.db.SaveChangesAsync(); return(bookmarkContainer); }
public async Task <bool> DeleteBookmarkContainer(string userId, int bookmarkContainerId) { BookmarkContainer?bookmarkContainer = await this.db.BookmarkContainers .Where(bc => bc.BookmarkContainerId == bookmarkContainerId) .SingleOrDefaultAsync(); if (bookmarkContainer == null) { return(false); } if (!BookmarkOwnershipTools.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) { return(false); } this.db.BookmarkContainers.Remove(bookmarkContainer); await this.db.SaveChangesAsync(); List <BookmarkContainer>?containers = this.db.BookmarkContainers .Where(bc => bc.ApplicationUserId == userId) .SortContainers() .ToList(); if (containers == null) { // The container *was* deleted, so indicate as such return(true); } // Fix up sort order just in case for (int i = 0; i < containers.Count; i++) { containers[i].SortOrder = i; } await this.db.SaveChangesAsync(); return(true); }
public async Task <BookmarkContainer?> GetBookmarkContainer(string userId, int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) { BookmarkContainer?bookmarkContainer = await this.db.BookmarkContainers .Where(bc => bc.BookmarkContainerId == bookmarkContainerId) .If(includeGroups, q => q.Include(bc => bc.BookmarkGroups)) .If(includeBookmarks, q => q .Include(bc => bc.BookmarkGroups !) .ThenInclude(bg => bg.Bookmarks)) .SingleOrDefaultAsync(); if (bookmarkContainer == null) { return(null); } if (!BookmarkOwnershipTools .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) { return(null); } return(bookmarkContainer); }
public async Task <BookmarkGroup?> UpdateBookmarkGroup(string userId, BookmarkGroup bookmarkGroup) { BookmarkGroup?existingGroup = await this.db.BookmarkGroups .SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId); if (existingGroup == null) { return(null); } if (!BookmarkOwnershipTools .IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId)) { return(null); } // If it's been moved to a new container if (existingGroup.BookmarkContainerId != bookmarkGroup.BookmarkContainerId && !BookmarkOwnershipTools .IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId)) { return(null); } if (existingGroup.BookmarkContainerId != bookmarkGroup.BookmarkContainerId) { // It's been moved to a different container - shuffle the sort order around List <BookmarkGroup>?oldContainerGroups = await db.BookmarkGroups .Where(bg => bg.BookmarkContainerId == existingGroup.BookmarkContainerId) .Where(bg => bg.SortOrder > existingGroup.SortOrder) .ToListAsync(); oldContainerGroups.ForEach(bg => bg.SortOrder -= 1); List <BookmarkGroup>?newContainerGroups = await db.BookmarkGroups .Where(bg => bg.BookmarkContainerId == bookmarkGroup.BookmarkContainerId) .Where(bg => bg.SortOrder >= bookmarkGroup.SortOrder) .ToListAsync(); newContainerGroups.ForEach(bg => bg.SortOrder += 1); } else if (existingGroup.SortOrder != bookmarkGroup.SortOrder) { // The group was moved within the same container List <BookmarkGroup>?containerGroups = await db.BookmarkGroups .Where(bg => bg.BookmarkContainerId == bookmarkGroup.BookmarkContainerId) .Where(bg => bg.BookmarkGroupId != bookmarkGroup.BookmarkGroupId) .ToListAsync(); containerGroups .Where(bg => bg.SortOrder > existingGroup.SortOrder) .ToList() .ForEach(bg => bg.SortOrder -= 1); containerGroups .Where(bg => bg.SortOrder > bookmarkGroup.SortOrder) .ToList() .ForEach(bg => bg.SortOrder += 1); } this.db.Entry(bookmarkGroup).State = EntityState.Modified; await this.db.SaveChangesAsync(); return(bookmarkGroup); }