add GetLink stuff (#19)
This commit is contained in:
parent
5a2ece04bd
commit
06469b52d4
@ -5,7 +5,7 @@ using MediatR;
|
||||
namespace HN.Application
|
||||
{
|
||||
|
||||
public class AddLinkCommand : IRequest<Guid>
|
||||
public sealed class AddLinkCommand : IRequest<Guid>
|
||||
{
|
||||
[Required]
|
||||
[Url]
|
||||
|
||||
17
Application/GetLink/GetLinkQuery.cs
Normal file
17
Application/GetLink/GetLinkQuery.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Application/GetLink/GetLinkQueryHandler.cs
Normal file
26
Application/GetLink/GetLinkQueryHandler.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -17,12 +17,7 @@ namespace HN.Application
|
||||
public Task<LinkDTO[]> 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());
|
||||
}
|
||||
|
||||
@ -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<IActionResult> Index()
|
||||
{
|
||||
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()
|
||||
{
|
||||
return View(new AddLinkCommand());
|
||||
|
||||
3
Apps/Website/Views/Links/Show.cshtml
Normal file
3
Apps/Website/Views/Links/Show.cshtml
Normal file
@ -0,0 +1,3 @@
|
||||
@model HN.Application.LinkDTO
|
||||
|
||||
<partial name="_LinkItem" model="@Model" />
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user