Use DTOs in controller
This commit is contained in:
parent
b52610e126
commit
106956157b
5 changed files with 56 additions and 11 deletions
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
using System;
|
||||
namespace Start.Server.Data.Services.Interfaces {
|
||||
public enum BookmarkStatus {
|
||||
OK = 1,
|
||||
BookmarkDoesNotExist = 2,
|
||||
OwnerDoesNotMatch = 3,
|
||||
UserDoesNotExist = 4
|
||||
}
|
||||
}
|
||||
24
Start/Server/Extensions/BookmarkMaps.cs
Normal file
24
Start/Server/Extensions/BookmarkMaps.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue