Add sorting columns

This commit is contained in:
Neil Brommer 2022-04-19 13:04:38 -07:00
parent adf24cbd5c
commit 90adbcfb7c
34 changed files with 833 additions and 80 deletions

View file

@ -46,10 +46,28 @@ namespace Start.Server.Data.Services {
}
public async Task<BookmarkContainer?> CreateBookmarkContainer(string userId,
string title) {
string title, int sortOrder) {
// No need to worry about ownership here
BookmarkContainer newContainer = new(userId, title);
// Increase the sorting ID for these items if it's needed to make room for this item
List<BookmarkContainer>? containers = this.db.BookmarkContainers
.Where(bc => bc.ApplicationUserId == userId)
.SortContainers()
.ToList();
if (containers == null)
return null;
// Fix up sort order just in case
for (int i = 0; i < containers.Count; i++) {
containers[i].SortOrder = i;
// Make room for the new container
if (i >= sortOrder)
containers[i].SortOrder++;
}
BookmarkContainer newContainer = new(userId, title, sortOrder);
await this.db.BookmarkContainers.AddAsync(newContainer);
await this.db.SaveChangesAsync();
return newContainer;
@ -57,15 +75,39 @@ namespace Start.Server.Data.Services {
public async Task<BookmarkContainer?> UpdateBookmarkContainer(string userId,
BookmarkContainer bookmarkContainer) {
BookmarkContainer? exitingBookmarkContainer = await this.db.BookmarkContainers
.SingleOrDefaultAsync(bc => bc.BookmarkContainerId
== bookmarkContainer.BookmarkContainerId);
BookmarkContainer? existingBookmarkContainer = await this.db.BookmarkContainers
.SingleOrDefaultAsync(bc =>
bc.BookmarkContainerId == bookmarkContainer.BookmarkContainerId);
if (exitingBookmarkContainer == null
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();
@ -86,6 +128,22 @@ namespace Start.Server.Data.Services {
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;
}
}

View file

@ -36,12 +36,12 @@ namespace Start.Server.Data.Services {
}
public async Task<BookmarkGroup?> CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId) {
string color, int sortOrder, int bookmarkContainerId) {
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return null;
BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId);
BookmarkGroup newBookmarkGroup = new(title, color, sortOrder, bookmarkContainerId);
await this.db.BookmarkGroups.AddAsync(newBookmarkGroup);
await this.db.SaveChangesAsync();

View file

@ -27,12 +27,12 @@ namespace Start.Server.Data.Services {
.ToListAsync();
}
public async Task<Bookmark?> CreateBookmark(string userId, string title, string url, string? notes,
int bookmarkGroupId) {
public async Task<Bookmark?> CreateBookmark(string userId, string title, string url,
string? notes, int sortOrder, int bookmarkGroupId) {
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return null;
Bookmark newBookmark = new(title, url, notes, bookmarkGroupId);
Bookmark newBookmark = new(title, url, notes, sortOrder, bookmarkGroupId);
await db.Bookmarks.AddAsync(newBookmark);
await db.SaveChangesAsync();

View file

@ -10,7 +10,7 @@ namespace Start.Server.Data.Services.Interfaces {
bool includeGroups = false, bool includeBookmarks = false);
public Task<BookmarkContainer?> CreateBookmarkContainer(string userId,
string title);
string title, int sortOrder);
public Task<BookmarkContainer?> UpdateBookmarkContainer(string userId,
BookmarkContainer bookmarkContainer);
public Task<bool> DeleteBookmarkContainer(string userId, int bookmarkContainerId);

View file

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using Start.Server.Models;
using Start.Shared;
namespace Start.Server.Data.Services.Interfaces {
public interface IBookmarkGroupService {
@ -12,7 +10,7 @@ namespace Start.Server.Data.Services.Interfaces {
bool includeBookmarks = false);
public Task<BookmarkGroup?> CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId);
string color, int sortOrder, int bookmarkContainerId);
public Task<BookmarkGroup?> UpdateBookmarkGroup(string userId,
BookmarkGroup bookmarkGroup);
public Task<bool> DeleteBookmarkGroup(string userId, int bookmarkGroupId);

View file

@ -8,7 +8,7 @@ namespace Start.Server.Data.Services.Interfaces {
public Task<IList<Bookmark>> GetUserBookmarks(string userId);
public Task<Bookmark?> CreateBookmark(string userId, string title, string url,
string? notes, int bookmarkGroupId);
string? notes, int sortOrder, int bookmarkGroupId);
public Task<Bookmark?> UpdateBookmark(string userId, Bookmark bookmark);
public Task<bool> DeleteBookmark(string userId, int bookmarkId);
}