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

@ -30,14 +30,20 @@ namespace Start.Server.Controllers {
} }
*/ */
[HttpPost] [HttpGet]
public Bookmark CreateBookmark(string title, string url, string? notes, public (BookmarkStatus, Bookmark?) GetBookmark(int bookmarkId) {
int bookmarkGroupId) { return this.bookmarkService.GetBookmark(this.GetUserId(), bookmarkId);
string userId = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
return bookmarkService.CreateBookmark(userId, title, url, notes, bookmarkGroupId);
} }
[HttpPost]
public (BookmarkStatus, Bookmark?) CreateBookmark(string title, string url, string? notes,
int bookmarkGroupId) {
return this.bookmarkService.CreateBookmark(this.GetUserId(), title, url, notes,
bookmarkGroupId);
}
private string GetUserId() {
return this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
}
} }
} }

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) { public (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId) {
if (!IsBookmarkOwner(userId, bookmarkId)) if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return (BookmarkStatus.OwnerDoesNotMatch, null);
Bookmark? bookmark = this.db.Bookmarks Bookmark? bookmark = this.db.Bookmarks
@ -33,18 +33,17 @@ namespace Start.Server.Data.Services {
.ToList(); .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) { int bookmarkGroupId) {
if (!this.IsBookmarkGroupOwner(userId, bookmarkGroupId)) if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
throw new SecurityException( return (BookmarkStatus.OwnerDoesNotMatch, null);
"The provided user ID doesn't match the bookmark group owner ID");
Bookmark newBookmark = new(title, url, bookmarkGroupId); Bookmark newBookmark = new(title, url, bookmarkGroupId);
db.Bookmarks.Add(newBookmark); db.Bookmarks.Add(newBookmark);
db.SaveChanges(); db.SaveChanges();
return newBookmark; return (BookmarkStatus.OK, newBookmark);
} }
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark) { public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark) {
@ -54,10 +53,11 @@ namespace Start.Server.Data.Services {
if (existingBookmark == null) if (existingBookmark == null)
return (BookmarkStatus.BookmarkDoesNotExist, null); return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!IsBookmarkOwner(userId, bookmark.BookmarkId)) if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return (BookmarkStatus.OwnerDoesNotMatch, null);
if (!IsBookmarkGroupOwner(userId, bookmark.BookmarkGroupId)) if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return (BookmarkStatus.OwnerDoesNotMatch, null);
db.Entry(bookmark).State = EntityState.Modified; db.Entry(bookmark).State = EntityState.Modified;
@ -73,7 +73,7 @@ namespace Start.Server.Data.Services {
if (bookmark == null) if (bookmark == null)
return BookmarkStatus.BookmarkDoesNotExist; return BookmarkStatus.BookmarkDoesNotExist;
if (!IsBookmarkOwner(userId, bookmarkId)) if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return BookmarkStatus.OwnerDoesNotMatch; return BookmarkStatus.OwnerDoesNotMatch;
db.Bookmarks.Remove(bookmark); db.Bookmarks.Remove(bookmark);
@ -81,23 +81,5 @@ namespace Start.Server.Data.Services {
return BookmarkStatus.OK; 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 (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId);
public IList<Bookmark> GetUserBookmarks(string userId); public IList<Bookmark> GetUserBookmarks(string userId);
public Bookmark CreateBookmark(string userId, string title, string url, string? notes, public (BookmarkStatus, Bookmark?) CreateBookmark(string userId, string title, string url,
int bookmarkGroupId); string? notes, int bookmarkGroupId);
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark); public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark);
public BookmarkStatus DeleteBookmark(string userId, int bookmarkId); public BookmarkStatus DeleteBookmark(string userId, int bookmarkId);
} }
public enum BookmarkStatus {
OK = 1,
BookmarkDoesNotExist = 2,
OwnerDoesNotMatch = 3,
UserDoesNotExist = 4
}
} }

View file

@ -0,0 +1,47 @@
using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;
namespace Start.Server {
/// <summary>Extension methods for LINQ queries</summary>
public static class LinqExtensions {
/// <summary>
/// If <paramref name="condition"/> is true, then returns <c>query.Where(expression)</c>.
/// Otherwise returns the <paramref name="query"/> unchanged.
/// </summary>
/// <param name="query">The query to potentially apply the expression to</param>
/// <param name="condition">
/// Determines whether or not the expression should be applied
/// </param>
/// <param name="expression">A function to test each element</param>
public static IQueryable<T> IfWhere<T>(this IQueryable<T> query, bool condition,
Expression<Func<T, bool>> expression) {
if (condition)
return query.Where(expression);
return query;
}
/// <summary>
/// If the <paramref name="condition"/> is true, apply the <paramref name="transform"/> to
/// the query, otherwise return the query unchanged.
/// </summary>
/// <typeparam name="T">The <paramref name="query"/>'s type</typeparam>
/// <param name="query">The query to potentially transform</param>
/// <param name="condition">
/// If true, apply the <paramref name="transform"/> to the <paramref name="query"/>
/// </param>
/// <param name="transform">
/// A function to apply to the <paramref name="query"/> if the <paramref name="condition"/>
/// is true
/// </param>
public static IQueryable<T> If<T>(this IQueryable<T> query, bool condition,
Func<IQueryable<T>, IQueryable<T>> transform) {
if (condition)
return transform(query);
return query;
}
}
}

View file

@ -24,12 +24,14 @@ namespace Start.Server.Models {
/// <summary>The bookmarks in this group</summary> /// <summary>The bookmarks in this group</summary>
public List<Bookmark>? Bookmarks { get; set; } public List<Bookmark>? Bookmarks { get; set; }
public BookmarkGroup(string title, string color) { public BookmarkGroup(string title, string color, int bookmarkContainerId) {
this.Title = title; this.Title = title;
this.Color = color; this.Color = color;
this.BookmarkContainerId = bookmarkContainerId;
} }
public BookmarkGroup(int bookmarkGroupId, string title, string color) : this(title, color) { public BookmarkGroup(int bookmarkGroupId, string title, string color,
int bookmarkContainerId) : this(title, color, bookmarkContainerId) {
this.BookmarkGroupId = bookmarkGroupId; this.BookmarkGroupId = bookmarkGroupId;
} }
} }

View file

@ -41,6 +41,8 @@ namespace Start.Server {
services.AddRazorPages(); services.AddRazorPages();
services.AddScoped<IBookmarkService, BookmarkService>(); services.AddScoped<IBookmarkService, BookmarkService>();
services.AddScoped<IBookmarkGroupService, BookmarkGroupService>();
services.AddScoped<IBookmarkContainerService, BookmarkContainerService>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request // This method gets called by the runtime. Use this method to configure the HTTP request
@ -49,7 +51,6 @@ namespace Start.Server {
ApplicationDbContext context) { ApplicationDbContext context) {
if (env.IsDevelopment()) { if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
app.UseMigrationsEndPoint();
app.UseWebAssemblyDebugging(); app.UseWebAssemblyDebugging();
} }
else { else {