add user in get requests
This commit is contained in:
parent
fb886c5c33
commit
e00ba99050
@ -18,11 +18,13 @@ namespace HN.Application
|
||||
public Task<LinkDto> Handle(GetLinkQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var result = from link in _context.Links
|
||||
join user in _context.Users on link.CreatedBy equals user.Id
|
||||
where link.Id == request.Id
|
||||
select new LinkDto
|
||||
{
|
||||
Id = link.Id,
|
||||
Url = link.Url,
|
||||
CreatedByName = user.UserName,
|
||||
CreatedAt = link.CreatedAt,
|
||||
UpVotes = link.Votes.Count(v => v.Type == VoteType.Up),
|
||||
DownVotes = link.Votes.Count(v => v.Type == VoteType.Down)
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
using System.Linq;
|
||||
using HN.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@ -10,5 +11,6 @@ namespace HN.Application
|
||||
{
|
||||
DbSet<Link> Links { get; }
|
||||
DbSet<Comment> Comments { get; }
|
||||
IQueryable<IUser> Users { get; }
|
||||
}
|
||||
}
|
||||
|
||||
10
Application/IUser.cs
Normal file
10
Application/IUser.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace HN.Application
|
||||
{
|
||||
public interface IUser
|
||||
{
|
||||
Guid Id { get; }
|
||||
string UserName { get; }
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ namespace HN.Application
|
||||
public Guid Id { get; set; }
|
||||
public string Url { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string CreatedByName { get; set; }
|
||||
public int UpVotes { get; set; }
|
||||
public int DownVotes { get; set; }
|
||||
|
||||
|
||||
@ -46,10 +46,12 @@ namespace HN.Application
|
||||
public Task<LinkDto[]> Handle(ListLinksQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var links = from link in _context.Links
|
||||
join user in _context.Users on link.CreatedBy equals user.Id
|
||||
select new LinkDto
|
||||
{
|
||||
Id = link.Id,
|
||||
Url = link.Url,
|
||||
CreatedByName = user.UserName,
|
||||
CreatedAt = link.CreatedAt,
|
||||
UpVotes = link.Votes.Count(v => v.Type == VoteType.Up),
|
||||
DownVotes = link.Votes.Count(v => v.Type == VoteType.Down)
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
<div>
|
||||
<h2>Add a comment</h2>
|
||||
@if(User.Identity.IsAuthenticated) {
|
||||
<form asp-action="Create" asp-controller="Comments" method="post">
|
||||
<input type="hidden" asp-for="@Model.LinkId" />
|
||||
<textarea asp-for="@Model.Content"></textarea>
|
||||
@ -9,4 +10,7 @@
|
||||
|
||||
<button type="submit">Post a comment</button>
|
||||
</form>
|
||||
} else {
|
||||
<p>Only logged in users can comment.</p>
|
||||
}
|
||||
</div>
|
||||
@ -4,8 +4,11 @@
|
||||
<div>
|
||||
👍: @Model.UpVotes / 👎: @Model.DownVotes
|
||||
</div>
|
||||
<form asp-action="Vote" asp-controller="Comments" asp-route-id="@Model.Id" method="post">
|
||||
|
||||
@if (User.Identity.IsAuthenticated) {
|
||||
<form asp-action="Vote" asp-controller="Comments" asp-route-id="@Model.Id" method="post">
|
||||
<input type="hidden" name="redirectTo" value="@Context.Request.Path" />
|
||||
<input type="submit" name="type" value="up" />
|
||||
<input type="submit" name="type" value="down" />
|
||||
</form>
|
||||
</form>
|
||||
}
|
||||
@ -1,9 +1,11 @@
|
||||
@model HN.Application.LinkDto
|
||||
|
||||
<a asp-action="Show" asp-controller="Links" asp-route-id="@Model.Id">@Model.Url - created at @Model.CreatedAt.ToLocalTime() (👍: @Model.UpVotes / 👎: @Model.DownVotes)</a>
|
||||
<a asp-action="Show" asp-controller="Links" asp-route-id="@Model.Id">@Model.Url - created at @Model.CreatedAt.ToLocalTime() by @Model.CreatedByName (👍: @Model.UpVotes / 👎: @Model.DownVotes)</a>
|
||||
|
||||
<form asp-controller="Links" asp-action="Vote" asp-route-id="@Model.Id" asp-route-url="@Model.Url" method="post">
|
||||
@if(User.Identity.IsAuthenticated) {
|
||||
<form asp-controller="Links" asp-action="Vote" asp-route-id="@Model.Id" asp-route-url="@Model.Url" method="post">
|
||||
<input type="hidden" name="redirectTo" value="@Context.Request.Path" />
|
||||
<input type="submit" name="type" value="up" />
|
||||
<input type="submit" name="type" value="down" />
|
||||
</form>
|
||||
</form>
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using HN.Application;
|
||||
using HN.Domain;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
@ -13,6 +14,8 @@ namespace HN.Infrastructure
|
||||
public DbSet<Link> Links { get; set; }
|
||||
public DbSet<Comment> Comments { get; set; }
|
||||
|
||||
IQueryable<IUser> IHNContext.Users => Users;
|
||||
|
||||
public HNDbContext()
|
||||
{
|
||||
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
using System;
|
||||
using HN.Application;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace HN.Infrastructure
|
||||
{
|
||||
public sealed class User : IdentityUser<Guid>
|
||||
public sealed class User : IdentityUser<Guid>, IUser
|
||||
{
|
||||
public User(string userName) : base(userName)
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user