add GetLink stuff #19

Merged
jleicher merged 1 commits from view-link into master 2020-12-09 15:06:36 +01:00
9 changed files with 84 additions and 27 deletions

View File

@ -5,7 +5,7 @@ using MediatR;
namespace HN.Application namespace HN.Application
{ {
public class AddLinkCommand : IRequest<Guid> public sealed class AddLinkCommand : IRequest<Guid>
{ {
[Required] [Required]
[Url] [Url]

View File

@ -0,0 +1,17 @@
using System;
using System.ComponentModel.DataAnnotations;
using MediatR;
namespace HN.Application
{
public sealed class GetLinkQuery : IRequest<LinkDTO>
{
[Required]
public Guid Id { get; set; }
public GetLinkQuery(Guid id)
{
Id = id;
}
}
}

View File

@ -0,0 +1,26 @@
using System.Threading;
using System.Linq;
using System.Threading.Tasks;
using MediatR;
namespace HN.Application
{
public sealed class GetLinkQueryHandler : IRequestHandler<GetLinkQuery, LinkDTO>
{
private readonly IDbContext _context;
public GetLinkQueryHandler(IDbContext context)
{
_context = context;
}
public Task<LinkDTO> 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());
}
}
}

View File

@ -1,4 +1,5 @@
using System; using System;
using HN.Domain;
namespace HN.Application namespace HN.Application
{ {
@ -7,5 +8,12 @@ namespace HN.Application
public Guid Id { get; set; } public Guid Id { get; set; }
public string Url { get; set; } public string Url { get; set; }
public DateTime CreatedAt { get; set; } public DateTime CreatedAt { get; set; }
public LinkDTO(Link link)
{
Id = link.Id;
Url = link.Url;
CreatedAt = link.CreatedAt;
}
} }
} }

View File

@ -17,12 +17,7 @@ namespace HN.Application
public Task<LinkDTO[]> Handle(ListLinksQuery request, CancellationToken cancellationToken) public Task<LinkDTO[]> Handle(ListLinksQuery request, CancellationToken cancellationToken)
{ {
var links = from link in _context.Links var links = from link in _context.Links
select new LinkDTO select new LinkDTO(link);
{
Id = link.Id,
Url = link.Url,
CreatedAt = link.CreatedAt
};
return Task.FromResult(links.ToArray()); return Task.FromResult(links.ToArray());
} }

View File

@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
using HN.Application; using HN.Application;
using MediatR; using MediatR;
using System.Threading.Tasks; using System.Threading.Tasks;
using System;
namespace Website.Controllers namespace Website.Controllers
{ {
@ -14,11 +15,18 @@ namespace Website.Controllers
_bus = bus; _bus = bus;
} }
[HttpGet]
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
return View(await _bus.Send(new ListLinksQuery())); return View(await _bus.Send(new ListLinksQuery()));
} }
[HttpGet("{controller}/{id}")]
public async Task<IActionResult> Show(Guid id)
{
return View(await _bus.Send(new GetLinkQuery(id)));
}
public IActionResult Create() public IActionResult Create()
{ {
return View(new AddLinkCommand()); return View(new AddLinkCommand());

View File

@ -0,0 +1,3 @@
@model HN.Application.LinkDTO
<partial name="_LinkItem" model="@Model" />

View File

@ -2,8 +2,8 @@ using System.Threading.Tasks;
namespace HN.Domain namespace HN.Domain
{ {
public interface ILinkRepository public interface ILinkRepository
{ {
Task AddAsync(Link link); Task AddAsync(Link link);
} }
} }

View File

@ -2,22 +2,22 @@
namespace HN.Domain 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; } this.Id = Guid.NewGuid();
public string Url { get; } this.CreatedAt = DateTime.UtcNow;
public DateTime CreatedAt { get; } this.Url = url;
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);
}
} }
public static Link FromUrl(string url)
{
return new Link(url);
}
}
} }