From 4537ab6d07c92fe4d3a1ce6a35f625836d8578ba Mon Sep 17 00:00:00 2001 From: Neil Brommer Date: Thu, 12 May 2022 16:19:19 -0700 Subject: [PATCH] Update SortOrder on bookmark groups in the container when a group is moved --- .../Data/Services/BookmarkGroupService.cs | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/Start/Server/Data/Services/BookmarkGroupService.cs b/Start/Server/Data/Services/BookmarkGroupService.cs index 67a69a6..c412291 100644 --- a/Start/Server/Data/Services/BookmarkGroupService.cs +++ b/Start/Server/Data/Services/BookmarkGroupService.cs @@ -60,10 +60,48 @@ namespace Start.Server.Data.Services { .IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId)) return null; - if (!BookmarkOwnershipTools - .IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId)) + // 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? oldContainerGroups = await db.BookmarkGroups + .Where(bg => bg.BookmarkContainerId == existingGroup.BookmarkContainerId) + .Where(bg => bg.SortOrder > existingGroup.SortOrder) + .ToListAsync(); + + oldContainerGroups.ForEach(bg => bg.SortOrder -= 1); + + List? 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? 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();