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

92 lines
2.9 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 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-16 06:27:28 +00:00
public BookmarkContainer? GetBookmarkContainer(string userId,
2021-11-14 01:19:28 +00:00
int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) {
BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers
.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))
.SingleOrDefault();
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 IList<BookmarkContainer> GetUserBookmarkContainers(string userId,
bool includeGroups = false, bool includeBookmarks = false) {
return this.db.BookmarkContainers
.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))
.ToList();
}
2021-11-16 06:27:28 +00:00
public 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);
this.db.BookmarkContainers.Add(newContainer);
this.db.SaveChanges();
2021-11-16 06:27:28 +00:00
return newContainer;
2021-11-14 01:19:28 +00:00
}
2021-11-16 06:27:28 +00:00
public BookmarkContainer? UpdateBookmarkContainer(string userId,
2021-11-14 01:19:28 +00:00
BookmarkContainer bookmarkContainer) {
BookmarkContainer? exitingBookmarkContainer = this.db.BookmarkContainers
.SingleOrDefault(bc => bc.BookmarkContainerId
== 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;
this.db.SaveChanges();
2021-11-16 06:27:28 +00:00
return bookmarkContainer;
2021-11-14 01:19:28 +00:00
}
2021-11-16 06:27:28 +00:00
public bool DeleteBookmarkContainer(string userId, int bookmarkContainerId) {
2021-11-14 01:19:28 +00:00
BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
.SingleOrDefault();
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);
this.db.SaveChanges();
2021-11-16 06:27:28 +00:00
return true;
2021-11-14 01:19:28 +00:00
}
}
}