From 106956157ba327ec2786b9f73dc131bc9856a71b Mon Sep 17 00:00:00 2001 From: Neil Brommer Date: Mon, 15 Nov 2021 21:44:16 -0800 Subject: [PATCH] Use DTOs in controller --- .../Server/Controllers/BookmarksController.cs | 37 +++++++++++++++---- Start/Server/Extensions/BookmarkMaps.cs | 24 ++++++++++++ Start/Shared/BookmarkContainerDto.cs | 2 +- Start/Shared/BookmarkGroupDto.cs | 2 +- .../Interfaces => Shared}/BookmarkStatus.cs | 2 +- 5 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 Start/Server/Extensions/BookmarkMaps.cs rename Start/{Server/Data/Services/Interfaces => Shared}/BookmarkStatus.cs (73%) diff --git a/Start/Server/Controllers/BookmarksController.cs b/Start/Server/Controllers/BookmarksController.cs index 80398fa..4ca6fe3 100644 --- a/Start/Server/Controllers/BookmarksController.cs +++ b/Start/Server/Controllers/BookmarksController.cs @@ -1,9 +1,11 @@ using System.Collections.Generic; +using System.Linq; 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 { [Authorize] @@ -26,24 +28,43 @@ namespace Start.Server.Controllers { } [HttpGet] - public IList GetAllBookmarkContainers() { - return this.bookmarkContainerService.GetUserBookmarkContainers(this.userId); + public IList GetAllBookmarkContainers() { + return this.bookmarkContainerService.GetUserBookmarkContainers(this.userId) + .Select(bc => bc.MapToDto()) + .ToList(); } [HttpGet] - public (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(int bookmarkContainerId) { - return this.bookmarkContainerService.GetBookmarkContainer(this.userId, bookmarkContainerId, true, true); + public (BookmarkStatus, BookmarkContainerDto?) GetBookmarkContainer(int bookmarkContainerId) { + (BookmarkStatus status, BookmarkContainer? container) = this.bookmarkContainerService + .GetBookmarkContainer(this.userId, bookmarkContainerId, true, true); + + return (status, container?.MapToDto()); } [HttpGet] - public (BookmarkStatus, Bookmark?) GetBookmark(int bookmarkId) { - return this.bookmarkService.GetBookmark(this.userId, bookmarkId); + public (BookmarkStatus, BookmarkDto?) GetBookmark(int bookmarkId) { + (BookmarkStatus status, Bookmark? bookmark) = this.bookmarkService + .GetBookmark(this.userId, bookmarkId); + + return (status, bookmark?.MapToDto()); } [HttpPost] - public (BookmarkStatus, Bookmark?) CreateBookmark(string title, string url, string? notes, + public (BookmarkStatus, BookmarkDto?) CreateBookmark(string title, string url, string? notes, int bookmarkGroupId) { - return this.bookmarkService.CreateBookmark(this.userId, title, url, notes, bookmarkGroupId); + (BookmarkStatus status, Bookmark? bookmark) = this.bookmarkService + .CreateBookmark(this.userId, title, url, notes, bookmarkGroupId); + + return (status, bookmark?.MapToDto()); + } + + [HttpPost] + public (BookmarkStatus, BookmarkContainerDto?) CreateBookmarkContainer(string title) { + (BookmarkStatus status, BookmarkContainer? container) = this + .bookmarkContainerService.CreateBookmarkContainer(this.userId, title); + + return (status, container?.MapToDto()); } } } diff --git a/Start/Server/Extensions/BookmarkMaps.cs b/Start/Server/Extensions/BookmarkMaps.cs new file mode 100644 index 0000000..0f88ad1 --- /dev/null +++ b/Start/Server/Extensions/BookmarkMaps.cs @@ -0,0 +1,24 @@ +using System; +using System.Linq; +using Start.Server.Models; +using Start.Shared; + +namespace Start.Server.Extensions { + public static class BookmarkMaps { + public static BookmarkDto MapToDto(this Bookmark bookmark) { + return new BookmarkDto(bookmark.BookmarkId, bookmark.Title, bookmark.Url, + bookmark.Notes); + } + + public static BookmarkGroupDto MapToDto(this BookmarkGroup bookmarkGroup) { + return new BookmarkGroupDto(bookmarkGroup.BookmarkGroupId, bookmarkGroup.Title, + bookmarkGroup.Color, bookmarkGroup.Bookmarks?.Select(b => b.MapToDto()).ToList()); + } + + public static BookmarkContainerDto MapToDto(this BookmarkContainer bookmarkContainer) { + return new BookmarkContainerDto(bookmarkContainer.BookmarkContainerId, + bookmarkContainer.Title, + bookmarkContainer.BookmarkGroups?.Select(bg => bg.MapToDto()).ToList()); + } + } +} diff --git a/Start/Shared/BookmarkContainerDto.cs b/Start/Shared/BookmarkContainerDto.cs index 73a0eb9..f151d7c 100644 --- a/Start/Shared/BookmarkContainerDto.cs +++ b/Start/Shared/BookmarkContainerDto.cs @@ -19,7 +19,7 @@ namespace Start.Shared { } public BookmarkContainerDto(int bookmarkContainerId, string title, - IList bookmarkGroups) : this(bookmarkContainerId, title) { + IList? bookmarkGroups) : this(bookmarkContainerId, title) { this.BookmarkGroups = bookmarkGroups; } } diff --git a/Start/Shared/BookmarkGroupDto.cs b/Start/Shared/BookmarkGroupDto.cs index cfafd4e..fcdb39e 100644 --- a/Start/Shared/BookmarkGroupDto.cs +++ b/Start/Shared/BookmarkGroupDto.cs @@ -23,7 +23,7 @@ namespace Start.Shared { } public BookmarkGroupDto(int bookmarkGroupId, string title, string color, - IList bookmarks) : this(bookmarkGroupId, title, color) { + IList? bookmarks) : this(bookmarkGroupId, title, color) { this.Bookmarks = bookmarks; } } diff --git a/Start/Server/Data/Services/Interfaces/BookmarkStatus.cs b/Start/Shared/BookmarkStatus.cs similarity index 73% rename from Start/Server/Data/Services/Interfaces/BookmarkStatus.cs rename to Start/Shared/BookmarkStatus.cs index 35e373f..fc4c0c2 100644 --- a/Start/Server/Data/Services/Interfaces/BookmarkStatus.cs +++ b/Start/Shared/BookmarkStatus.cs @@ -1,5 +1,5 @@ using System; -namespace Start.Server.Data.Services.Interfaces { +namespace Start.Shared { public enum BookmarkStatus { OK = 1, BookmarkDoesNotExist = 2,