Simplify return stack

This commit is contained in:
Neil Brommer 2021-11-15 22:27:28 -08:00
parent 5ccc28516f
commit c5403ca206
8 changed files with 84 additions and 106 deletions

View file

@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Start.Server.Data.Services.Interfaces; using Start.Server.Data.Services.Interfaces;
using Start.Server.Extensions; using Start.Server.Extensions;
using Start.Server.Models;
using Start.Shared; using Start.Shared;
namespace Start.Server.Controllers { namespace Start.Server.Controllers {
@ -35,36 +34,32 @@ namespace Start.Server.Controllers {
} }
[HttpGet] [HttpGet]
public (BookmarkStatus, BookmarkContainerDto?) GetBookmarkContainer(int bookmarkContainerId) { public BookmarkContainerDto? GetBookmarkContainer(int bookmarkContainerId) {
(BookmarkStatus status, BookmarkContainer? container) = this.bookmarkContainerService return this.bookmarkContainerService
.GetBookmarkContainer(this.userId, bookmarkContainerId, true, true); .GetBookmarkContainer(this.userId, bookmarkContainerId, true, true)
?.MapToDto();
return (status, container?.MapToDto());
} }
[HttpGet] [HttpGet]
public (BookmarkStatus, BookmarkDto?) GetBookmark(int bookmarkId) { public BookmarkDto? GetBookmark(int bookmarkId) {
(BookmarkStatus status, Bookmark? bookmark) = this.bookmarkService return this.bookmarkService
.GetBookmark(this.userId, bookmarkId); .GetBookmark(this.userId, bookmarkId)
?.MapToDto();
return (status, bookmark?.MapToDto());
} }
[HttpPost] [HttpPost]
public (BookmarkStatus, BookmarkDto?) CreateBookmark(string title, string url, string? notes, public BookmarkDto? CreateBookmark(string title, string url, string? notes,
int bookmarkGroupId) { int bookmarkGroupId) {
(BookmarkStatus status, Bookmark? bookmark) = this.bookmarkService return this.bookmarkService
.CreateBookmark(this.userId, title, url, notes, bookmarkGroupId); .CreateBookmark(this.userId, title, url, notes, bookmarkGroupId)
?.MapToDto();
return (status, bookmark?.MapToDto());
} }
[HttpPost] [HttpPost]
public (BookmarkStatus, BookmarkContainerDto?) CreateBookmarkContainer(string title) { public BookmarkContainerDto? CreateBookmarkContainer(string title) {
(BookmarkStatus status, BookmarkContainer? container) = this return this.bookmarkContainerService
.bookmarkContainerService.CreateBookmarkContainer(this.userId, title); .CreateBookmarkContainer(this.userId, title)
?.MapToDto();
return (status, container?.MapToDto());
} }
} }
} }

View file

@ -1,11 +1,9 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces; using Start.Server.Data.Services.Interfaces;
using Start.Server.Extensions; using Start.Server.Extensions;
using Start.Server.Models; using Start.Server.Models;
using Start.Shared;
namespace Start.Server.Data.Services { namespace Start.Server.Data.Services {
public class BookmarkContainerService : IBookmarkContainerService { public class BookmarkContainerService : IBookmarkContainerService {
@ -15,7 +13,7 @@ namespace Start.Server.Data.Services {
this.db = dbContext; this.db = dbContext;
} }
public (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(string userId, public BookmarkContainer? GetBookmarkContainer(string userId,
int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) { int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) {
BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId) .Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
@ -26,13 +24,13 @@ namespace Start.Server.Data.Services {
.SingleOrDefault(); .SingleOrDefault();
if (bookmarkContainer == null) if (bookmarkContainer == null)
return (BookmarkStatus.BookmarkDoesNotExist, null); return null;
if (!BookmarkOwnershipTools if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
return (BookmarkStatus.OK, bookmarkContainer); return bookmarkContainer;
} }
public IList<BookmarkContainer> GetUserBookmarkContainers(string userId, public IList<BookmarkContainer> GetUserBookmarkContainers(string userId,
@ -46,47 +44,47 @@ namespace Start.Server.Data.Services {
.ToList(); .ToList();
} }
public (BookmarkStatus, BookmarkContainer?) CreateBookmarkContainer(string userId, public BookmarkContainer? CreateBookmarkContainer(string userId,
string title) { string title) {
// No need to worry about ownership here
BookmarkContainer newContainer = new(userId, title); BookmarkContainer newContainer = new(userId, title);
this.db.BookmarkContainers.Add(newContainer); this.db.BookmarkContainers.Add(newContainer);
return (BookmarkStatus.OK, newContainer); return newContainer;
} }
public (BookmarkStatus, BookmarkContainer?) UpdateBookmarkContainer(string userId, public BookmarkContainer? UpdateBookmarkContainer(string userId,
BookmarkContainer bookmarkContainer) { BookmarkContainer bookmarkContainer) {
BookmarkContainer? exitingBookmarkContainer = this.db.BookmarkContainers BookmarkContainer? exitingBookmarkContainer = this.db.BookmarkContainers
.SingleOrDefault(bc => bc.BookmarkContainerId .SingleOrDefault(bc => bc.BookmarkContainerId
== bookmarkContainer.BookmarkContainerId); == bookmarkContainer.BookmarkContainerId);
if (exitingBookmarkContainer == null) if (exitingBookmarkContainer == null
return (BookmarkStatus.BookmarkDoesNotExist, null); || !BookmarkOwnershipTools
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainer.BookmarkContainerId)) .IsBookmarkContainerOwner(this.db, userId, bookmarkContainer.BookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
this.db.Entry(bookmarkContainer).State = EntityState.Modified; this.db.Entry(bookmarkContainer).State = EntityState.Modified;
this.db.SaveChanges(); this.db.SaveChanges();
return (BookmarkStatus.OK, bookmarkContainer); return bookmarkContainer;
} }
public BookmarkStatus DeleteBookmarkContainer(string userId, int bookmarkContainerId) { public bool DeleteBookmarkContainer(string userId, int bookmarkContainerId) {
BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers
.Where(bc => bc.BookmarkContainerId == bookmarkContainerId) .Where(bc => bc.BookmarkContainerId == bookmarkContainerId)
.SingleOrDefault(); .SingleOrDefault();
if (bookmarkContainer == null) if (bookmarkContainer == null)
return (BookmarkStatus.BookmarkDoesNotExist); return false;
if (!BookmarkOwnershipTools.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) if (!BookmarkOwnershipTools.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return BookmarkStatus.OwnerDoesNotMatch; return false;
this.db.BookmarkContainers.Remove(bookmarkContainer); this.db.BookmarkContainers.Remove(bookmarkContainer);
this.db.SaveChanges(); this.db.SaveChanges();
return BookmarkStatus.OK; return true;
} }
} }
} }

View file

@ -15,20 +15,17 @@ namespace Start.Server.Data.Services {
this.db = dbContext; this.db = dbContext;
} }
public (BookmarkStatus, BookmarkGroup?) GetBookmarkGroup(string userId, public BookmarkGroup? GetBookmarkGroup(string userId, int bookmarkGroupId,
int bookmarkGroupId, bool includeBookmarks = false) { bool includeBookmarks = false) {
BookmarkGroup? group = db.BookmarkGroups BookmarkGroup? group = db.BookmarkGroups
.Where(bg => bg.BookmarkGroupId == bookmarkGroupId) .Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
.If(includeBookmarks, q => q.Include(bg => bg.Bookmarks)) .If(includeBookmarks, q => q.Include(bg => bg.Bookmarks))
.SingleOrDefault(); .SingleOrDefault();
if (group == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId)) if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
return (BookmarkStatus.OK, group); return group;
} }
public IList<BookmarkGroup> GetUserBookmarkGroups(string userId, public IList<BookmarkGroup> GetUserBookmarkGroups(string userId,
@ -39,51 +36,55 @@ namespace Start.Server.Data.Services {
.ToList(); .ToList();
} }
public (BookmarkStatus, BookmarkGroup?) CreateBookmarkGroup(string userId, string title, public BookmarkGroup? CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId) { string color, int bookmarkContainerId) {
if (!BookmarkOwnershipTools if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId); BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId);
this.db.BookmarkGroups.Add(newBookmarkGroup); this.db.BookmarkGroups.Add(newBookmarkGroup);
this.db.SaveChanges(); this.db.SaveChanges();
return (BookmarkStatus.OK, newBookmarkGroup); return newBookmarkGroup;
} }
public (BookmarkStatus, BookmarkGroup?) UpdateBookmarkGroup(string userId, public BookmarkGroup? UpdateBookmarkGroup(string userId,
BookmarkGroup bookmarkGroup) { BookmarkGroup bookmarkGroup) {
BookmarkGroup? existingGroup = this.db.BookmarkGroups BookmarkGroup? existingGroup = this.db.BookmarkGroups
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId); .SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId);
if (existingGroup == null) if (existingGroup == null)
return (BookmarkStatus.BookmarkDoesNotExist, null); return null;
if (!BookmarkOwnershipTools if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId)) .IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId))
return null;
this.db.Entry(bookmarkGroup).State = EntityState.Modified; this.db.Entry(bookmarkGroup).State = EntityState.Modified;
this.db.SaveChanges(); this.db.SaveChanges();
return (BookmarkStatus.OK, bookmarkGroup); return bookmarkGroup;
} }
public BookmarkStatus DeleteBookmarkGroup(string userId, int bookmarkGroupId) { public bool DeleteBookmarkGroup(string userId, int bookmarkGroupId) {
BookmarkGroup? bookmarkGroup = this.db.BookmarkGroups BookmarkGroup? bookmarkGroup = this.db.BookmarkGroups
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroupId); .SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroupId);
if (bookmarkGroup == null) if (bookmarkGroup == null)
return BookmarkStatus.BookmarkDoesNotExist; return false;
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId)) if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return BookmarkStatus.OwnerDoesNotMatch; return false;
this.db.BookmarkGroups.Remove(bookmarkGroup); this.db.BookmarkGroups.Remove(bookmarkGroup);
this.db.SaveChanges(); this.db.SaveChanges();
return BookmarkStatus.OK; return true;
} }
} }
} }

View file

@ -1,11 +1,8 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces; using Start.Server.Data.Services.Interfaces;
using Start.Server.Models; using Start.Server.Models;
using Start.Shared;
namespace Start.Server.Data.Services { namespace Start.Server.Data.Services {
public class BookmarkService : IBookmarkService { public class BookmarkService : IBookmarkService {
@ -15,17 +12,12 @@ namespace Start.Server.Data.Services {
this.db = dbContext; this.db = dbContext;
} }
public (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId) { public Bookmark? GetBookmark(string userId, int bookmarkId) {
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId)) if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
Bookmark? bookmark = this.db.Bookmarks return this.db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmarkId); .SingleOrDefault(b => b.BookmarkId == bookmarkId);
if (bookmark == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
return (BookmarkStatus.OK, bookmark);
} }
public IList<Bookmark> GetUserBookmarks(string userId) { public IList<Bookmark> GetUserBookmarks(string userId) {
@ -34,53 +26,54 @@ namespace Start.Server.Data.Services {
.ToList(); .ToList();
} }
public (BookmarkStatus, Bookmark?) CreateBookmark(string userId, string title, string url, string? notes, public Bookmark? CreateBookmark(string userId, string title, string url, string? notes,
int bookmarkGroupId) { int bookmarkGroupId) {
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId)) if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
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 (BookmarkStatus.OK, newBookmark); if (newBookmark.BookmarkId <= 0)
return null;
return newBookmark;
} }
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark) { public Bookmark? UpdateBookmark(string userId, Bookmark bookmark) {
Bookmark? existingBookmark = db.Bookmarks Bookmark? existingBookmark = db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmark.BookmarkId); .SingleOrDefault(b => b.BookmarkId == bookmark.BookmarkId);
if (existingBookmark == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId)) if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
// Could be moving to a different group
if (!BookmarkOwnershipTools if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId)) .IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null); return null;
db.Entry(bookmark).State = EntityState.Modified; db.Entry(bookmark).State = EntityState.Modified;
db.SaveChanges(); db.SaveChanges();
return (BookmarkStatus.OK, bookmark); return bookmark;
} }
public BookmarkStatus DeleteBookmark(string userId, int bookmarkId) { public bool DeleteBookmark(string userId, int bookmarkId) {
Bookmark? bookmark = db.Bookmarks Bookmark? bookmark = db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmarkId); .SingleOrDefault(b => b.BookmarkId == bookmarkId);
if (bookmark == null) if (bookmark == null)
return BookmarkStatus.BookmarkDoesNotExist; return false;
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId)) if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return BookmarkStatus.OwnerDoesNotMatch; return false;
db.Bookmarks.Remove(bookmark); db.Bookmarks.Remove(bookmark);
db.SaveChanges(); db.SaveChanges();
return BookmarkStatus.OK; return true;
} }
} }
} }

View file

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

View file

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

View file

@ -5,12 +5,12 @@ using Start.Shared;
namespace Start.Server.Data.Services.Interfaces { namespace Start.Server.Data.Services.Interfaces {
public interface IBookmarkService { public interface IBookmarkService {
public (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId); public Bookmark? GetBookmark(string userId, int bookmarkId);
public IList<Bookmark> GetUserBookmarks(string userId); public IList<Bookmark> GetUserBookmarks(string userId);
public (BookmarkStatus, Bookmark?) CreateBookmark(string userId, string title, string url, public Bookmark? CreateBookmark(string userId, string title, string url,
string? notes, int bookmarkGroupId); string? notes, int bookmarkGroupId);
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark); public Bookmark? UpdateBookmark(string userId, Bookmark bookmark);
public BookmarkStatus DeleteBookmark(string userId, int bookmarkId); public bool DeleteBookmark(string userId, int bookmarkId);
} }
} }

View file

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