2021-11-29 06:32:21 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
using System.Linq;
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +00:00
|
|
|
|
public async Task<BookmarkGroup?> GetBookmarkGroup(string userId, int bookmarkGroupId,
|
2021-11-16 06:27:28 +00:00
|
|
|
|
bool includeBookmarks = false) {
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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))
|
2021-11-29 06:32:21 +00:00
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +00:00
|
|
|
|
public async Task<IList<BookmarkGroup>> GetUserBookmarkGroups(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
bool includeBookmarkGroups = false) {
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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))
|
2021-11-29 06:32:21 +00:00
|
|
|
|
.ToListAsync();
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +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);
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +00:00
|
|
|
|
public async Task<BookmarkGroup?> UpdateBookmarkGroup(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
BookmarkGroup bookmarkGroup) {
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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;
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +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);
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|