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

View file

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

View file

@ -15,20 +15,17 @@ namespace Start.Server.Data.Services {
this.db = dbContext;
}
public (BookmarkStatus, BookmarkGroup?) GetBookmarkGroup(string userId,
int bookmarkGroupId, bool includeBookmarks = false) {
public 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 null;
return (BookmarkStatus.OK, group);
return group;
}
public IList<BookmarkGroup> GetUserBookmarkGroups(string userId,
@ -39,51 +36,55 @@ namespace Start.Server.Data.Services {
.ToList();
}
public (BookmarkStatus, BookmarkGroup?) CreateBookmarkGroup(string userId, string title,
public BookmarkGroup? CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId) {
if (!BookmarkOwnershipTools
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return null;
BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId);
this.db.BookmarkGroups.Add(newBookmarkGroup);
this.db.SaveChanges();
return (BookmarkStatus.OK, newBookmarkGroup);
return newBookmarkGroup;
}
public (BookmarkStatus, BookmarkGroup?) UpdateBookmarkGroup(string userId,
public BookmarkGroup? UpdateBookmarkGroup(string userId,
BookmarkGroup bookmarkGroup) {
BookmarkGroup? existingGroup = this.db.BookmarkGroups
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId);
if (existingGroup == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
return null;
if (!BookmarkOwnershipTools
.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.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
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroupId);
if (bookmarkGroup == null)
return BookmarkStatus.BookmarkDoesNotExist;
return false;
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return BookmarkStatus.OwnerDoesNotMatch;
return false;
this.db.BookmarkGroups.Remove(bookmarkGroup);
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.Security;
using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces;
using Start.Server.Models;
using Start.Shared;
namespace Start.Server.Data.Services {
public class BookmarkService : IBookmarkService {
@ -15,17 +12,12 @@ namespace Start.Server.Data.Services {
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))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return null;
Bookmark? bookmark = this.db.Bookmarks
return this.db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
if (bookmark == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
return (BookmarkStatus.OK, bookmark);
}
public IList<Bookmark> GetUserBookmarks(string userId) {
@ -34,53 +26,54 @@ namespace Start.Server.Data.Services {
.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) {
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return null;
Bookmark newBookmark = new(title, url, bookmarkGroupId);
db.Bookmarks.Add(newBookmark);
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
.SingleOrDefault(b => b.BookmarkId == bookmark.BookmarkId);
if (existingBookmark == null)
return (BookmarkStatus.BookmarkDoesNotExist, null);
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return null;
// Could be moving to a different group
if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
return (BookmarkStatus.OwnerDoesNotMatch, null);
return null;
db.Entry(bookmark).State = EntityState.Modified;
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
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
if (bookmark == null)
return BookmarkStatus.BookmarkDoesNotExist;
return false;
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
return BookmarkStatus.OwnerDoesNotMatch;
return false;
db.Bookmarks.Remove(bookmark);
db.SaveChanges();
return BookmarkStatus.OK;
return true;
}
}
}

View file

@ -5,15 +5,15 @@ using Start.Shared;
namespace Start.Server.Data.Services.Interfaces {
public interface IBookmarkContainerService {
public (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(string userId,
public 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,
public BookmarkContainer? CreateBookmarkContainer(string userId,
string title);
public (BookmarkStatus, BookmarkContainer?) UpdateBookmarkContainer(string userId,
public BookmarkContainer? UpdateBookmarkContainer(string userId,
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 {
public interface IBookmarkGroupService {
public (BookmarkStatus, BookmarkGroup?) GetBookmarkGroup(string userId,
public 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,
public BookmarkGroup? CreateBookmarkGroup(string userId, string title,
string color, int bookmarkContainerId);
public (BookmarkStatus, BookmarkGroup?) UpdateBookmarkGroup(string userId,
public BookmarkGroup? UpdateBookmarkGroup(string userId,
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 {
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 (BookmarkStatus, Bookmark?) CreateBookmark(string userId, string title, string url,
public 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 Bookmark? UpdateBookmark(string userId, Bookmark bookmark);
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
}
}