2021-11-16 06:27:28 +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 BookmarkContainerService : IBookmarkContainerService {
|
|
|
|
|
private readonly ApplicationDbContext db;
|
|
|
|
|
|
|
|
|
|
public BookmarkContainerService(ApplicationDbContext dbContext) {
|
|
|
|
|
this.db = dbContext;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +00:00
|
|
|
|
public async Task<BookmarkContainer?> GetBookmarkContainer(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) {
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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)
|
|
|
|
|
.ThenInclude(bg => bg.Bookmarks))
|
2021-11-29 06:32:21 +00:00
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +00:00
|
|
|
|
public async Task<IList<BookmarkContainer>> GetUserBookmarkContainers(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
bool includeGroups = false, bool includeBookmarks = false) {
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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)
|
|
|
|
|
.ThenInclude(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<BookmarkContainer?> CreateBookmarkContainer(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
string title) {
|
2021-11-16 06:27:28 +00:00
|
|
|
|
// No need to worry about ownership here
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
BookmarkContainer newContainer = new(userId, title);
|
2021-11-29 06:32:21 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +00:00
|
|
|
|
public async Task<BookmarkContainer?> UpdateBookmarkContainer(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
BookmarkContainer bookmarkContainer) {
|
2021-11-29 06:32:21 +00:00
|
|
|
|
BookmarkContainer? exitingBookmarkContainer = await this.db.BookmarkContainers
|
|
|
|
|
.SingleOrDefaultAsync(bc => bc.BookmarkContainerId
|
2021-11-14 01:19:28 +00:00
|
|
|
|
== bookmarkContainer.BookmarkContainerId);
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
if (exitingBookmarkContainer == null
|
|
|
|
|
|| !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
|
|
|
|
|
|
|
|
|
this.db.Entry(bookmarkContainer).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 bookmarkContainer;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 06:32:21 +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)
|
2021-11-29 06:32:21 +00:00
|
|
|
|
.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);
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|