Use DTOs in controller

This commit is contained in:
Neil Brommer 2021-11-15 21:44:16 -08:00
parent b52610e126
commit 106956157b
5 changed files with 56 additions and 11 deletions

View file

@ -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<BookmarkContainer> GetAllBookmarkContainers() {
return this.bookmarkContainerService.GetUserBookmarkContainers(this.userId);
public IList<BookmarkContainerDto> 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());
}
}
}

View file

@ -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());
}
}
}

View file

@ -19,7 +19,7 @@ namespace Start.Shared {
}
public BookmarkContainerDto(int bookmarkContainerId, string title,
IList<BookmarkGroupDto> bookmarkGroups) : this(bookmarkContainerId, title) {
IList<BookmarkGroupDto>? bookmarkGroups) : this(bookmarkContainerId, title) {
this.BookmarkGroups = bookmarkGroups;
}
}

View file

@ -23,7 +23,7 @@ namespace Start.Shared {
}
public BookmarkGroupDto(int bookmarkGroupId, string title, string color,
IList<BookmarkDto> bookmarks) : this(bookmarkGroupId, title, color) {
IList<BookmarkDto>? bookmarks) : this(bookmarkGroupId, title, color) {
this.Bookmarks = bookmarks;
}
}

View file

@ -1,5 +1,5 @@
using System;
namespace Start.Server.Data.Services.Interfaces {
namespace Start.Shared {
public enum BookmarkStatus {
OK = 1,
BookmarkDoesNotExist = 2,