BlazorStart/Start/Server/Data/Services/BookmarkContainerService.cs

151 lines
5 KiB
C#
Raw Normal View History

2021-11-16 06:27:28 +00:00
using System.Collections.Generic;
2021-11-14 01:19:28 +00:00
using System.Linq;
using System.Threading.Tasks;
2021-11-14 01:19:28 +00:00
using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces;
2021-11-14 03:27:01 +00:00
using Start.Server.Extensions;
2021-11-14 01:19:28 +00:00
using Start.Server.Models;
namespace Start.Server.Data.Services {
public class BookmarkContainerService : IBookmarkContainerService {
private readonly ApplicationDbContext db;
public BookmarkContainerService(ApplicationDbContext dbContext) {
this.db = dbContext;
}
public async Task<BookmarkContainer?> GetBookmarkContainer(string userId,
2021-11-14 01:19:28 +00:00
int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) {
BookmarkContainer? bookmarkContainer = await this.db.BookmarkContainers
2021-11-14 01:19:28 +00:00
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
.If(includeGroups, q => q.Include(bc => bc.BookmarkGroups))
.If(includeBookmarks, q => q
.Include(bc => bc.BookmarkGroups!)
2021-11-14 01:19:28 +00:00
.ThenInclude(bg => bg.Bookmarks))
.SingleOrDefaultAsync();
2021-11-14 01:19:28 +00:00
if (bookmarkContainer == null)
2021-11-16 06:27:28 +00:00
return null;
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-14 01:19:28 +00:00
2021-11-16 06:27:28 +00:00
return bookmarkContainer;
2021-11-14 01:19:28 +00:00
}
public async Task<IList<BookmarkContainer>> GetUserBookmarkContainers(string userId,
2021-11-14 01:19:28 +00:00
bool includeGroups = false, bool includeBookmarks = false) {
return await this.db.BookmarkContainers
2021-11-14 01:19:28 +00:00
.Where(bc => bc.ApplicationUserId == userId)
.If(includeGroups, q => q.Include(bc => bc.BookmarkGroups))
.If(includeBookmarks, q => q
.Include(bc => bc.BookmarkGroups!)
2021-11-14 01:19:28 +00:00
.ThenInclude(bg => bg.Bookmarks))
.ToListAsync();
2021-11-14 01:19:28 +00:00
}
public async Task<BookmarkContainer?> CreateBookmarkContainer(string userId,
2022-04-19 20:04:38 +00:00
string title, int sortOrder) {
2021-11-16 06:27:28 +00:00
// No need to worry about ownership here
2022-04-19 20:04:38 +00:00
// 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();
2021-11-16 06:27:28 +00:00
return newContainer;
2021-11-14 01:19:28 +00:00
}
public async Task<BookmarkContainer?> UpdateBookmarkContainer(string userId,
2021-11-14 01:19:28 +00:00
BookmarkContainer bookmarkContainer) {
2022-04-19 20:04:38 +00:00
BookmarkContainer? existingBookmarkContainer = await this.db.BookmarkContainers
.SingleOrDefaultAsync(bc =>
bc.BookmarkContainerId == bookmarkContainer.BookmarkContainerId);
2021-11-14 01:19:28 +00:00
2022-04-19 20:04:38 +00:00
if (existingBookmarkContainer == null
2021-11-16 06:27:28 +00:00
|| !BookmarkOwnershipTools
2021-11-14 01:19:28 +00:00
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainer.BookmarkContainerId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-14 01:19:28 +00:00
2022-04-19 20:04:38 +00:00
// 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
}
2021-11-14 01:19:28 +00:00
this.db.Entry(bookmarkContainer).State = EntityState.Modified;
await this.db.SaveChangesAsync();
2021-11-14 01:19:28 +00:00
2021-11-16 06:27:28 +00:00
return bookmarkContainer;
2021-11-14 01:19:28 +00:00
}
public async Task<bool> DeleteBookmarkContainer(string userId, int bookmarkContainerId) {
BookmarkContainer? bookmarkContainer = await this.db.BookmarkContainers
2021-11-14 01:19:28 +00:00
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
.SingleOrDefaultAsync();
2021-11-14 01:19:28 +00:00
if (bookmarkContainer == null)
2021-11-16 06:27:28 +00:00
return false;
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
2021-11-16 06:27:28 +00:00
return false;
2021-11-14 01:19:28 +00:00
this.db.BookmarkContainers.Remove(bookmarkContainer);
await this.db.SaveChangesAsync();
2021-11-14 01:19:28 +00:00
2022-04-19 20:04:38 +00:00
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();
2021-11-16 06:27:28 +00:00
return true;
2021-11-14 01:19:28 +00:00
}
}
}