From f2abd35522084b92cc7870701e173bb9fbb8bd37 Mon Sep 17 00:00:00 2001 From: Julien Leicher Date: Wed, 9 Dec 2020 15:04:26 +0100 Subject: [PATCH] add GetLink stuff --- Application/AddLink/AddLinkCommand.cs | 2 +- Application/GetLink/GetLinkQuery.cs | 17 ++++++++++ Application/GetLink/GetLinkQueryHandler.cs | 26 +++++++++++++++ Application/ListLinks/LinkDTO.cs | 8 +++++ .../ListLinks/ListLinksQueryHandler.cs | 7 +--- Apps/Website/Controllers/LinksController.cs | 8 +++++ Apps/Website/Views/Links/Show.cshtml | 3 ++ Domain/ILinkRepository.cs | 8 ++--- Domain/Link.cs | 32 +++++++++---------- 9 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 Application/GetLink/GetLinkQuery.cs create mode 100644 Application/GetLink/GetLinkQueryHandler.cs create mode 100644 Apps/Website/Views/Links/Show.cshtml diff --git a/Application/AddLink/AddLinkCommand.cs b/Application/AddLink/AddLinkCommand.cs index b1824eb..4b71e73 100644 --- a/Application/AddLink/AddLinkCommand.cs +++ b/Application/AddLink/AddLinkCommand.cs @@ -5,7 +5,7 @@ using MediatR; namespace HN.Application { - public class AddLinkCommand : IRequest + public sealed class AddLinkCommand : IRequest { [Required] [Url] diff --git a/Application/GetLink/GetLinkQuery.cs b/Application/GetLink/GetLinkQuery.cs new file mode 100644 index 0000000..7abbedf --- /dev/null +++ b/Application/GetLink/GetLinkQuery.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; +using MediatR; + +namespace HN.Application +{ + public sealed class GetLinkQuery : IRequest + { + [Required] + public Guid Id { get; set; } + + public GetLinkQuery(Guid id) + { + Id = id; + } + } +} \ No newline at end of file diff --git a/Application/GetLink/GetLinkQueryHandler.cs b/Application/GetLink/GetLinkQueryHandler.cs new file mode 100644 index 0000000..b1beb34 --- /dev/null +++ b/Application/GetLink/GetLinkQueryHandler.cs @@ -0,0 +1,26 @@ +using System.Threading; +using System.Linq; +using System.Threading.Tasks; +using MediatR; + +namespace HN.Application +{ + public sealed class GetLinkQueryHandler : IRequestHandler + { + private readonly IDbContext _context; + + public GetLinkQueryHandler(IDbContext context) + { + _context = context; + } + + public Task Handle(GetLinkQuery request, CancellationToken cancellationToken) + { + var result = from link in _context.Links + where link.Id == request.Id + select new LinkDTO(link); + + return Task.FromResult(result.FirstOrDefault()); + } + } +} \ No newline at end of file diff --git a/Application/ListLinks/LinkDTO.cs b/Application/ListLinks/LinkDTO.cs index f1d1745..c8999cc 100644 --- a/Application/ListLinks/LinkDTO.cs +++ b/Application/ListLinks/LinkDTO.cs @@ -1,4 +1,5 @@ using System; +using HN.Domain; namespace HN.Application { @@ -7,5 +8,12 @@ namespace HN.Application public Guid Id { get; set; } public string Url { get; set; } public DateTime CreatedAt { get; set; } + + public LinkDTO(Link link) + { + Id = link.Id; + Url = link.Url; + CreatedAt = link.CreatedAt; + } } } \ No newline at end of file diff --git a/Application/ListLinks/ListLinksQueryHandler.cs b/Application/ListLinks/ListLinksQueryHandler.cs index a853eae..6fa867e 100644 --- a/Application/ListLinks/ListLinksQueryHandler.cs +++ b/Application/ListLinks/ListLinksQueryHandler.cs @@ -17,12 +17,7 @@ namespace HN.Application public Task Handle(ListLinksQuery request, CancellationToken cancellationToken) { var links = from link in _context.Links - select new LinkDTO - { - Id = link.Id, - Url = link.Url, - CreatedAt = link.CreatedAt - }; + select new LinkDTO(link); return Task.FromResult(links.ToArray()); } diff --git a/Apps/Website/Controllers/LinksController.cs b/Apps/Website/Controllers/LinksController.cs index 8fe0c98..c99c365 100644 --- a/Apps/Website/Controllers/LinksController.cs +++ b/Apps/Website/Controllers/LinksController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using HN.Application; using MediatR; using System.Threading.Tasks; +using System; namespace Website.Controllers { @@ -14,11 +15,18 @@ namespace Website.Controllers _bus = bus; } + [HttpGet] public async Task Index() { return View(await _bus.Send(new ListLinksQuery())); } + [HttpGet("{controller}/{id}")] + public async Task Show(Guid id) + { + return View(await _bus.Send(new GetLinkQuery(id))); + } + public IActionResult Create() { return View(new AddLinkCommand()); diff --git a/Apps/Website/Views/Links/Show.cshtml b/Apps/Website/Views/Links/Show.cshtml new file mode 100644 index 0000000..b880dd3 --- /dev/null +++ b/Apps/Website/Views/Links/Show.cshtml @@ -0,0 +1,3 @@ +@model HN.Application.LinkDTO + + \ No newline at end of file diff --git a/Domain/ILinkRepository.cs b/Domain/ILinkRepository.cs index 9651269..0798d0b 100644 --- a/Domain/ILinkRepository.cs +++ b/Domain/ILinkRepository.cs @@ -2,8 +2,8 @@ using System.Threading.Tasks; namespace HN.Domain { - public interface ILinkRepository - { - Task AddAsync(Link link); - } + public interface ILinkRepository + { + Task AddAsync(Link link); + } } \ No newline at end of file diff --git a/Domain/Link.cs b/Domain/Link.cs index 3afcfc5..64132e3 100644 --- a/Domain/Link.cs +++ b/Domain/Link.cs @@ -2,22 +2,22 @@ namespace HN.Domain { - public class Link + public sealed class Link + { + public Guid Id { get; } + public string Url { get; } + public DateTime CreatedAt { get; } + + private Link(string url) { - public Guid Id { get; } - public string Url { get; } - public DateTime CreatedAt { get; } - - private Link(string url) - { - this.Id = Guid.NewGuid(); - this.CreatedAt = DateTime.UtcNow; - this.Url = url; - } - - public static Link FromUrl(string url) - { - return new Link(url); - } + this.Id = Guid.NewGuid(); + this.CreatedAt = DateTime.UtcNow; + this.Url = url; } + + public static Link FromUrl(string url) + { + return new Link(url); + } + } }