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

90 lines
2.8 KiB
C#
Raw 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 (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId))
return null;
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
}
}
}