add GetLink stuff (#19)
This commit is contained in:
parent
5a2ece04bd
commit
06469b52d4
@ -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]
|
||||||
|
|||||||
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 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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());
|
||||||
|
|||||||
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,7 +2,7 @@
|
|||||||
|
|
||||||
namespace HN.Domain
|
namespace HN.Domain
|
||||||
{
|
{
|
||||||
public class Link
|
public sealed class Link
|
||||||
{
|
{
|
||||||
public Guid Id { get; }
|
public Guid Id { get; }
|
||||||
public string Url { get; }
|
public string Url { get; }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user