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

93 lines
3.1 KiB
C#
Raw Normal View History

2021-11-16 06:27:28 +00:00
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 BookmarkContainerService : IBookmarkContainerService {
private readonly ApplicationDbContext db;
public BookmarkContainerService(ApplicationDbContext dbContext) {
this.db = dbContext;
}
public async Task<BookmarkContainer?> GetBookmarkContainer(string userId,
2021-11-14 01:19:28 +00:00
int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) {
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))
.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
}
public async Task<IList<BookmarkContainer>> GetUserBookmarkContainers(string userId,
2021-11-14 01:19:28 +00:00
bool includeGroups = false, bool includeBookmarks = false) {
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))
.ToListAsync();
2021-11-14 01:19:28 +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);
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
}
public async Task<BookmarkContainer?> UpdateBookmarkContainer(string userId,
2021-11-14 01:19:28 +00:00
BookmarkContainer bookmarkContainer) {
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;
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
}
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)
.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);
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
}
}
}