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

128 lines
4.3 KiB
C#
Raw Permalink Normal View History

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 BookmarkGroupService : IBookmarkGroupService {
private readonly ApplicationDbContext db;
public BookmarkGroupService(ApplicationDbContext dbContext) {
this.db = dbContext;
}
public async Task<BookmarkGroup?> GetBookmarkGroup(string userId, int bookmarkGroupId,
2021-11-16 06:27:28 +00:00
bool includeBookmarks = false) {
BookmarkGroup? group = await db.BookmarkGroups
2021-11-14 01:19:28 +00:00
.Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
.If(includeBookmarks, q => q.Include(bg => bg.Bookmarks))
.SingleOrDefaultAsync();
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId))
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 group;
2021-11-14 01:19:28 +00:00
}
public async Task<IList<BookmarkGroup>> GetUserBookmarkGroups(string userId,
2021-11-14 01:19:28 +00:00
bool includeBookmarkGroups = false) {
return await this.db.BookmarkGroups
2021-11-14 01:19:28 +00:00
.Where(bg => bg.BookmarkContainer!.ApplicationUserId == userId)
.If(includeBookmarkGroups, q => q.Include(bg => bg.Bookmarks))
.ToListAsync();
2021-11-14 01:19:28 +00:00
}
public async Task<BookmarkGroup?> CreateBookmarkGroup(string userId, string title,
2022-04-19 20:04:38 +00:00
string color, int sortOrder, int bookmarkContainerId) {
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
2022-04-19 20:04:38 +00:00
BookmarkGroup newBookmarkGroup = new(title, color, sortOrder, bookmarkContainerId);
await this.db.BookmarkGroups.AddAsync(newBookmarkGroup);
await this.db.SaveChangesAsync();
2021-11-14 01:19:28 +00:00
2021-11-16 06:27:28 +00:00
return newBookmarkGroup;
2021-11-14 01:19:28 +00:00
}
public async Task<BookmarkGroup?> UpdateBookmarkGroup(string userId,
2021-11-14 01:19:28 +00:00
BookmarkGroup bookmarkGroup) {
BookmarkGroup? existingGroup = await this.db.BookmarkGroups
.SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId);
2021-11-14 01:19:28 +00:00
if (existingGroup == null)
2021-11-16 06:27:28 +00:00
return null;
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId))
2021-11-16 06:27:28 +00:00
return null;
// If it's been moved to a new container
if (existingGroup.BookmarkContainerId != bookmarkGroup.BookmarkContainerId
&& !BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-14 01:19:28 +00:00
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);
}
2021-11-14 01:19:28 +00:00
this.db.Entry(bookmarkGroup).State = EntityState.Modified;
await this.db.SaveChangesAsync();
2021-11-14 01:19:28 +00:00
2021-11-16 06:27:28 +00:00
return bookmarkGroup;
2021-11-14 01:19:28 +00:00
}
public async Task<bool> DeleteBookmarkGroup(string userId, int bookmarkGroupId) {
BookmarkGroup? bookmarkGroup = await this.db.BookmarkGroups
.SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroupId);
2021-11-14 01:19:28 +00:00
if (bookmarkGroup == null)
2021-11-16 06:27:28 +00:00
return false;
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
2021-11-16 06:27:28 +00:00
return false;
2021-11-14 01:19:28 +00:00
this.db.BookmarkGroups.Remove(bookmarkGroup);
await this.db.SaveChangesAsync();
2021-11-14 01:19:28 +00:00
2021-11-16 06:27:28 +00:00
return true;
2021-11-14 01:19:28 +00:00
}
}
}