Add more data access services

This commit is contained in:
Neil Brommer 2021-11-13 17:19:28 -08:00
parent 691e049103
commit d18dea826e
12 changed files with 333 additions and 45 deletions

View file

@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces;
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 (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(string userId,
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)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return (BookmarkStatus.OK, bookmarkContainer);
}
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();
}
public (BookmarkStatus, BookmarkContainer?) CreateBookmarkContainer(string userId,
string title) {
BookmarkContainer newContainer = new(userId, title);
this.db.BookmarkContainers.Add(newContainer);
return (BookmarkStatus.OK, newContainer);
}
public (BookmarkStatus, BookmarkContainer?) UpdateBookmarkContainer(string userId,
BookmarkContainer bookmarkContainer) {
BookmarkContainer? exitingBookmarkContainer = this.db.BookmarkContainers
.SingleOrDefault(bc => bc.BookmarkContainerId
== bookmarkContainer.BookmarkContainerId);
if (exitingBookmarkContainer == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainer.BookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
this.db.Entry(bookmarkContainer).State = EntityState.Modified;
this.db.SaveChanges();
return (BookmarkStatus.OK, bookmarkContainer);
}
public BookmarkStatus DeleteBookmarkContainer(string userId, int bookmarkContainerId) {
BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
.SingleOrDefault();
if (bookmarkContainer == null)
return (BookmarkStatus.BookmarkDoesNotExist);
if (!BookmarkOwnershipTools.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return BookmarkStatus.OwnerDoesNotMatch;
this.db.BookmarkContainers.Remove(bookmarkContainer);
this.db.SaveChanges();
return BookmarkStatus.OK;
}
}
}

View file

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces;
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 (BookmarkStatus, BookmarkGroup?) GetBookmarkGroup(string userId,
int bookmarkGroupId, bool includeBookmarks = false) {
BookmarkGroup? group = db.BookmarkGroups
.Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
.If(includeBookmarks, q => q.Include(bg => bg.Bookmarks))
.SingleOrDefault();
if (group == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return (BookmarkStatus.OK, group);
}
public IList<BookmarkGroup> GetUserBookmarkGroups(string userId,
bool includeBookmarkGroups = false) {
return this.db.BookmarkGroups
.Where(bg => bg.BookmarkContainer!.ApplicationUserId == userId)
.If(includeBookmarkGroups, q => q.Include(bg => bg.Bookmarks))
.ToList();
}
public (BookmarkStatus, BookmarkGroup?) CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId) {
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId);
this.db.BookmarkGroups.Add(newBookmarkGroup);
this.db.SaveChanges();
return (BookmarkStatus.OK, newBookmarkGroup);
}
public (BookmarkStatus, BookmarkGroup?) UpdateBookmarkGroup(string userId,
BookmarkGroup bookmarkGroup) {
BookmarkGroup? existingGroup = this.db.BookmarkGroups
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId);
if (existingGroup == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
this.db.Entry(bookmarkGroup).State = EntityState.Modified;
this.db.SaveChanges();
return (BookmarkStatus.OK, bookmarkGroup);
}
public BookmarkStatus DeleteBookmarkGroup(string userId, int bookmarkGroupId) {
BookmarkGroup? bookmarkGroup = this.db.BookmarkGroups
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroupId);
if (bookmarkGroup == null)
return BookmarkStatus.BookmarkDoesNotExist;
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return BookmarkStatus.OwnerDoesNotMatch;
this.db.BookmarkGroups.Remove(bookmarkGroup);
this.db.SaveChanges();
return BookmarkStatus.OK;
}
}
}

View file

@ -0,0 +1,35 @@
using System;
using System.Linq;
namespace Start.Server.Data.Services {
public static class BookmarkOwnershipTools {
public static bool IsBookmarkOwner(ApplicationDbContext db, string userId, int bookmarkId) {
string? bookmarkOwnerId = db.Bookmarks
.Where(b => b.BookmarkId == bookmarkId)
.Select(b => b.BookmarkGroup!.BookmarkContainer!.ApplicationUserId)
.SingleOrDefault();
return userId == bookmarkOwnerId;
}
public static bool IsBookmarkGroupOwner(ApplicationDbContext db, string userId,
int bookmarkGroupId) {
string? groupOwnerId = db.BookmarkGroups
.Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
.Select(bg => bg.BookmarkContainer!.ApplicationUserId)
.SingleOrDefault();
return userId == groupOwnerId;
}
public static bool IsBookmarkContainerOwner(ApplicationDbContext db, string userId,
int bookmarkContainerId) {
string? containerOwnerId = db.BookmarkContainers
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
.Select(bc => bc.ApplicationUserId)
.SingleOrDefault();
return userId == containerOwnerId;
}
}
}

View file

@ -15,7 +15,7 @@ namespace Start.Server.Data.Services {
}
public (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId) {
if (!IsBookmarkOwner(userId, bookmarkId))
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
Bookmark? bookmark = this.db.Bookmarks
@ -33,18 +33,17 @@ namespace Start.Server.Data.Services {
.ToList();
}
public Bookmark CreateBookmark(string userId, string title, string url, string? notes,
public (BookmarkStatus, Bookmark?) CreateBookmark(string userId, string title, string url, string? notes,
int bookmarkGroupId) {
if (!this.IsBookmarkGroupOwner(userId, bookmarkGroupId))
throw new SecurityException(
"The provided user ID doesn't match the bookmark group owner ID");
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
Bookmark newBookmark = new(title, url, bookmarkGroupId);
db.Bookmarks.Add(newBookmark);
db.SaveChanges();
return newBookmark;
return (BookmarkStatus.OK, newBookmark);
}
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark) {
@ -54,10 +53,11 @@ namespace Start.Server.Data.Services {
if (existingBookmark == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!IsBookmarkOwner(userId, bookmark.BookmarkId))
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
if (!IsBookmarkGroupOwner(userId, bookmark.BookmarkGroupId))
if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
db.Entry(bookmark).State = EntityState.Modified;
@ -73,7 +73,7 @@ namespace Start.Server.Data.Services {
if (bookmark == null)
return BookmarkStatus.BookmarkDoesNotExist;
if (!IsBookmarkOwner(userId, bookmarkId))
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return BookmarkStatus.OwnerDoesNotMatch;
db.Bookmarks.Remove(bookmark);
@ -81,23 +81,5 @@ namespace Start.Server.Data.Services {
return BookmarkStatus.OK;
}
private bool IsBookmarkOwner(string userId, int bookmarkId) {
string? bookmarkOwnerId = this.db.Bookmarks
.Where(b => b.BookmarkId == bookmarkId)
.Select(b => b.BookmarkGroup!.BookmarkContainer!.ApplicationUserId)
.SingleOrDefault();
return userId == bookmarkOwnerId;
}
private bool IsBookmarkGroupOwner(string userId, int bookmarkGroupId) {
string? groupOwnerId = this.db.BookmarkGroups
.Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
.Select(bg => bg.BookmarkContainer!.ApplicationUserId)
.SingleOrDefault();
return userId == groupOwnerId;
}
}
}

View file

@ -0,0 +1,9 @@
using System;
namespace Start.Server.Data.Services.Interfaces {
public enum BookmarkStatus {
OK = 1,
BookmarkDoesNotExist = 2,
OwnerDoesNotMatch = 3,
UserDoesNotExist = 4
}
}

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using Start.Server.Models;
namespace Start.Server.Data.Services.Interfaces {
public interface IBookmarkContainerService {
public (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(string userId,
int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false);
public IList<BookmarkContainer> GetUserBookmarkContainers(string userId,
bool includeGroups = false, bool includeBookmarks = false);
public (BookmarkStatus, BookmarkContainer?) CreateBookmarkContainer(string userId,
string title);
public (BookmarkStatus, BookmarkContainer?) UpdateBookmarkContainer(string userId,
BookmarkContainer bookmarkContainer);
public BookmarkStatus DeleteBookmarkContainer(string userId, int bookmarkContainerId);
}
}

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using Start.Server.Models;
namespace Start.Server.Data.Services.Interfaces {
public interface IBookmarkGroupService {
public (BookmarkStatus, BookmarkGroup?) GetBookmarkGroup(string userId,
int bookmarkGroupId, bool includeBookmarks = false);
public IList<BookmarkGroup> GetUserBookmarkGroups(string userId,
bool includeBookmarks = false);
public (BookmarkStatus, BookmarkGroup?) CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId);
public (BookmarkStatus, BookmarkGroup?) UpdateBookmarkGroup(string userId,
BookmarkGroup bookmarkGroup);
public BookmarkStatus DeleteBookmarkGroup(string userId, int bookmarkGroupId);
}
}

View file

@ -7,16 +7,9 @@ namespace Start.Server.Data.Services.Interfaces {
public (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId);
public IList<Bookmark> GetUserBookmarks(string userId);
public Bookmark CreateBookmark(string userId, string title, string url, string? notes,
int bookmarkGroupId);
public (BookmarkStatus, Bookmark?) CreateBookmark(string userId, string title, string url,
string? notes, int bookmarkGroupId);
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark);
public BookmarkStatus DeleteBookmark(string userId, int bookmarkId);
}
public enum BookmarkStatus {
OK = 1,
BookmarkDoesNotExist = 2,
OwnerDoesNotMatch = 3,
UserDoesNotExist = 4
}
}