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

119 lines
3.6 KiB
C#
Raw Normal View History

2021-11-16 06:27:28 +00:00
using System.Collections.Generic;
2021-11-13 03:21:59 +00:00
using System.Linq;
using System.Threading.Tasks;
2021-11-13 03:21:59 +00:00
using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces;
using Start.Server.Models;
namespace Start.Server.Data.Services {
public class BookmarkService : IBookmarkService {
private readonly ApplicationDbContext db;
public BookmarkService(ApplicationDbContext dbContext) {
this.db = dbContext;
}
public async Task<Bookmark?> GetBookmark(string userId, int bookmarkId) {
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
return await this.db.Bookmarks
.SingleOrDefaultAsync(b => b.BookmarkId == bookmarkId);
2021-11-13 03:21:59 +00:00
}
public async Task<IList<Bookmark>> GetUserBookmarks(string userId) {
return await this.db.Bookmarks
2021-11-13 03:21:59 +00:00
.Where(b => b.BookmarkGroup!.BookmarkContainer!.ApplicationUserId == userId)
.ToListAsync();
2021-11-13 03:21:59 +00:00
}
2022-04-19 20:04:38 +00:00
public async Task<Bookmark?> CreateBookmark(string userId, string title, string url,
string? notes, int sortOrder, int bookmarkGroupId) {
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
2022-04-19 20:04:38 +00:00
Bookmark newBookmark = new(title, url, notes, sortOrder, bookmarkGroupId);
2021-11-13 03:21:59 +00:00
await db.Bookmarks.AddAsync(newBookmark);
await db.SaveChangesAsync();
2021-11-13 03:21:59 +00:00
2021-11-16 06:27:28 +00:00
if (newBookmark.BookmarkId <= 0)
return null;
return newBookmark;
2021-11-13 03:21:59 +00:00
}
public async Task<Bookmark?> UpdateBookmark(string userId, Bookmark bookmark) {
Bookmark? existingBookmark = await db.Bookmarks
.SingleOrDefaultAsync(b => b.BookmarkId == bookmark.BookmarkId);
if (existingBookmark == null)
return null;
2021-11-13 03:21:59 +00:00
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
2021-11-16 06:27:28 +00:00
// Could be moving to a different group
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
if (bookmark.BookmarkGroupId != existingBookmark.BookmarkGroupId) {
// The bookmark was moved to a different group - shuffle sort order around
List<Bookmark>? oldGroupBookmarks = await db.Bookmarks
.Where(b => b.BookmarkGroupId == existingBookmark.BookmarkGroupId)
.Where(b => b.SortOrder > existingBookmark.SortOrder)
.ToListAsync();
oldGroupBookmarks.ForEach(b => b.SortOrder -= 1);
List<Bookmark>? newGroupBookmarks = await db.Bookmarks
.Where(b => b.BookmarkGroupId == bookmark.BookmarkGroupId)
.Where(b => b.SortOrder >= bookmark.SortOrder)
.ToListAsync();
newGroupBookmarks.ForEach(b => b.SortOrder += 1);
}
else if (bookmark.SortOrder != existingBookmark.SortOrder) {
// The bookmark has been moved within the same group
List<Bookmark>? groupBookmarks = await db.Bookmarks
.Where(b => b.BookmarkGroupId == bookmark.BookmarkGroupId)
.Where(b => b.BookmarkId != bookmark.BookmarkId)
.ToListAsync();
groupBookmarks
.Where(b => b.SortOrder > existingBookmark.SortOrder)
.ToList()
.ForEach(b => b.SortOrder -=1);
groupBookmarks
.Where(b => b.SortOrder > bookmark.SortOrder)
.ToList()
.ForEach(b => b.SortOrder += 1);
}
2021-11-13 03:21:59 +00:00
db.Entry(bookmark).State = EntityState.Modified;
await db.SaveChangesAsync();
2021-11-13 03:21:59 +00:00
2021-11-16 06:27:28 +00:00
return bookmark;
2021-11-13 03:21:59 +00:00
}
public async Task<bool> DeleteBookmark(string userId, int bookmarkId) {
2021-11-13 03:21:59 +00:00
Bookmark? bookmark = db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
if (bookmark == null)
2021-11-16 06:27:28 +00:00
return false;
2021-11-13 03:21:59 +00:00
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
2021-11-16 06:27:28 +00:00
return false;
2021-11-13 03:21:59 +00:00
db.Bookmarks.Remove(bookmark);
await db.SaveChangesAsync();
2021-11-13 03:21:59 +00:00
2021-11-16 06:27:28 +00:00
return true;
2021-11-13 03:21:59 +00:00
}
}
}