diff --git a/Application/GetLink/GetLinkQueryHandler.cs b/Application/GetLink/GetLinkQueryHandler.cs index 65dc04e..b3e8695 100644 --- a/Application/GetLink/GetLinkQueryHandler.cs +++ b/Application/GetLink/GetLinkQueryHandler.cs @@ -18,11 +18,13 @@ namespace HN.Application public Task 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) diff --git a/Application/IHNContext.cs b/Application/IHNContext.cs index 1facc5e..375186a 100644 --- a/Application/IHNContext.cs +++ b/Application/IHNContext.cs @@ -1,3 +1,4 @@ +using System.Linq; using HN.Domain; using Microsoft.EntityFrameworkCore; @@ -10,5 +11,6 @@ namespace HN.Application { DbSet Links { get; } DbSet Comments { get; } + IQueryable Users { get; } } } diff --git a/Application/IUser.cs b/Application/IUser.cs new file mode 100644 index 0000000..2d260cf --- /dev/null +++ b/Application/IUser.cs @@ -0,0 +1,10 @@ +using System; + +namespace HN.Application +{ + public interface IUser + { + Guid Id { get; } + string UserName { get; } + } +} \ No newline at end of file diff --git a/Application/ListLinks/LinkDTO.cs b/Application/ListLinks/LinkDTO.cs index fa3bf7c..7cab81a 100644 --- a/Application/ListLinks/LinkDTO.cs +++ b/Application/ListLinks/LinkDTO.cs @@ -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; } diff --git a/Application/ListLinks/ListLinksQueryHandler.cs b/Application/ListLinks/ListLinksQueryHandler.cs index 8851c34..8c1464c 100644 --- a/Application/ListLinks/ListLinksQueryHandler.cs +++ b/Application/ListLinks/ListLinksQueryHandler.cs @@ -46,10 +46,12 @@ namespace HN.Application public Task 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) diff --git a/Apps/Website/Views/Shared/_CommentForm.cshtml b/Apps/Website/Views/Shared/_CommentForm.cshtml index 57fd06c..5d88d12 100644 --- a/Apps/Website/Views/Shared/_CommentForm.cshtml +++ b/Apps/Website/Views/Shared/_CommentForm.cshtml @@ -2,11 +2,15 @@

Add a comment

-
- - - - - -
+ @if(User.Identity.IsAuthenticated) { +
+ + + + + +
+ } else { +

Only logged in users can comment.

+ }
\ No newline at end of file diff --git a/Apps/Website/Views/Shared/_CommentItem.cshtml b/Apps/Website/Views/Shared/_CommentItem.cshtml index 18aedb4..4667c92 100644 --- a/Apps/Website/Views/Shared/_CommentItem.cshtml +++ b/Apps/Website/Views/Shared/_CommentItem.cshtml @@ -4,8 +4,11 @@
πŸ‘: @Model.UpVotes / πŸ‘Ž: @Model.DownVotes
-
- - - -
\ No newline at end of file + +@if (User.Identity.IsAuthenticated) { +
+ + + +
+} \ No newline at end of file diff --git a/Apps/Website/Views/Shared/_LinkItem.cshtml b/Apps/Website/Views/Shared/_LinkItem.cshtml index cf16e40..6e480ea 100644 --- a/Apps/Website/Views/Shared/_LinkItem.cshtml +++ b/Apps/Website/Views/Shared/_LinkItem.cshtml @@ -1,9 +1,11 @@ @model HN.Application.LinkDto -@Model.Url - created at @Model.CreatedAt.ToLocalTime() (πŸ‘: @Model.UpVotes / πŸ‘Ž: @Model.DownVotes) +@Model.Url - created at @Model.CreatedAt.ToLocalTime() by @Model.CreatedByName (πŸ‘: @Model.UpVotes / πŸ‘Ž: @Model.DownVotes) -
- - - -
\ No newline at end of file +@if(User.Identity.IsAuthenticated) { +
+ + + +
+} \ No newline at end of file diff --git a/Infrastructure/HNDbContext.cs b/Infrastructure/HNDbContext.cs index e4e387d..ba600f1 100644 --- a/Infrastructure/HNDbContext.cs +++ b/Infrastructure/HNDbContext.cs @@ -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 Links { get; set; } public DbSet Comments { get; set; } + IQueryable IHNContext.Users => Users; + public HNDbContext() { diff --git a/Infrastructure/User.cs b/Infrastructure/User.cs index 8645123..fb961fa 100644 --- a/Infrastructure/User.cs +++ b/Infrastructure/User.cs @@ -1,9 +1,10 @@ using System; +using HN.Application; using Microsoft.AspNetCore.Identity; namespace HN.Infrastructure { - public sealed class User : IdentityUser + public sealed class User : IdentityUser, IUser { public User(string userName) : base(userName) {