From db34091d6b63b4997329dc52805b59d02fb19a28 Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Thu, 29 Apr 2021 12:01:02 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20nom=20du=20cr=C3=A9ateur=20sur=20l?= =?UTF-8?q?'appli=20MVC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Application/CommentDTO.cs | 1 + Application/CommentService.cs | 29 +++++++++++++++------- Application/IData.cs | 1 + Application/IUser.cs | 10 ++++++++ Application/LinkDTO.cs | 1 + Application/LinkService.cs | 2 ++ Apps/Website/Views/Links/Show.cshtml | 2 +- Apps/Website/Views/Shared/_LinkItem.cshtml | 2 +- Infrastructure/HNDbContext.cs | 2 ++ Infrastructure/Identity/User.cs | 3 ++- Infrastructure/Repositories/Memory/Data.cs | 2 ++ 11 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 Application/IUser.cs diff --git a/Application/CommentDTO.cs b/Application/CommentDTO.cs index 341bf33..d54f8fb 100644 --- a/Application/CommentDTO.cs +++ b/Application/CommentDTO.cs @@ -8,5 +8,6 @@ namespace Application public string Content { get; set; } public int UpvotesCount { get; set; } public int DownvotesCount { get; set; } + public string CreatedByUsername { get; set; } } } \ No newline at end of file diff --git a/Application/CommentService.cs b/Application/CommentService.cs index df6249d..a2ca148 100644 --- a/Application/CommentService.cs +++ b/Application/CommentService.cs @@ -45,15 +45,26 @@ namespace Application _data.Comments.Where(comment => comment.LinkId == linkId) : _data.Comments; - return comments - .OrderByDescending(comment => comment.CreatedAt) - .Select(comment => new CommentDTO - { - Id = comment.Id, - Content = comment.Content, - UpvotesCount = 0, - DownvotesCount = 0, - }); + return from comment in comments + join user in _data.Users on comment.CreatedBy equals user.Id + select new CommentDTO + { + Id = comment.Id, + Content = comment.Content, + UpvotesCount = 0, + DownvotesCount = 0, + CreatedByUsername = user.UserName + }; + + // return comments + // .OrderByDescending(comment => comment.CreatedAt) + // .Select(comment => new CommentDTO + // { + // Id = comment.Id, + // Content = comment.Content, + // UpvotesCount = 0, + // DownvotesCount = 0, + // }); } } } \ No newline at end of file diff --git a/Application/IData.cs b/Application/IData.cs index a4e063a..9721736 100644 --- a/Application/IData.cs +++ b/Application/IData.cs @@ -10,5 +10,6 @@ namespace Application { IQueryable Links { get; } IQueryable Comments { get; } + IQueryable Users { get; } } } \ No newline at end of file diff --git a/Application/IUser.cs b/Application/IUser.cs new file mode 100644 index 0000000..17f32e6 --- /dev/null +++ b/Application/IUser.cs @@ -0,0 +1,10 @@ +using System; + +namespace Application +{ + public interface IUser + { + Guid Id { get; } + string UserName { get; } + } +} \ No newline at end of file diff --git a/Application/LinkDTO.cs b/Application/LinkDTO.cs index daeea23..5f16026 100644 --- a/Application/LinkDTO.cs +++ b/Application/LinkDTO.cs @@ -10,5 +10,6 @@ namespace Application public int UpvotesCount { get; set; } public int DownvotesCount { get; set; } public int CommentsCount { get; set; } + public string CreatedByUsername { get; set; } } } \ No newline at end of file diff --git a/Application/LinkService.cs b/Application/LinkService.cs index abda42b..5a226f9 100644 --- a/Application/LinkService.cs +++ b/Application/LinkService.cs @@ -52,6 +52,7 @@ namespace Application { return (from link in _data.Links // join comment in _data.Comments on link.Id equals comment.LinkId into comments + join user in _data.Users on link.CreatedBy equals user.Id orderby link.CreatedAt descending select new LinkDTO { @@ -61,6 +62,7 @@ namespace Application UpvotesCount = 2, DownvotesCount = 4, CommentsCount = _data.Comments.Count(comment => comment.LinkId == link.Id), //comments.Count(), + CreatedByUsername = user.UserName, }); // return _data.Links diff --git a/Apps/Website/Views/Links/Show.cshtml b/Apps/Website/Views/Links/Show.cshtml index d1ee4a8..a1a5b23 100644 --- a/Apps/Website/Views/Links/Show.cshtml +++ b/Apps/Website/Views/Links/Show.cshtml @@ -19,7 +19,7 @@ else
    @foreach (var comment in Model.Comments) { -
  • @comment.Content
  • +
  • @comment.Content - @comment.CreatedByUsername
  • }
} \ No newline at end of file diff --git a/Apps/Website/Views/Shared/_LinkItem.cshtml b/Apps/Website/Views/Shared/_LinkItem.cshtml index c4b25b2..85beb5f 100644 --- a/Apps/Website/Views/Shared/_LinkItem.cshtml +++ b/Apps/Website/Views/Shared/_LinkItem.cshtml @@ -4,7 +4,7 @@

@Model.Url

Show - - Published at @Model.CreatedAt.ToLongDateString() + - Published at @Model.CreatedAt.ToLongDateString() by @Model.CreatedByUsername - 🗨 @Model.CommentsCount - 👍 @Model.UpvotesCount / 👎 @Model.DownvotesCount

diff --git a/Infrastructure/HNDbContext.cs b/Infrastructure/HNDbContext.cs index 1e80adb..826c5d3 100644 --- a/Infrastructure/HNDbContext.cs +++ b/Infrastructure/HNDbContext.cs @@ -17,6 +17,8 @@ namespace Infrastructure IQueryable IData.Comments => Comments; + IQueryable IData.Users => Users; + public HNDbContext() : base() { diff --git a/Infrastructure/Identity/User.cs b/Infrastructure/Identity/User.cs index d696a93..25effe9 100644 --- a/Infrastructure/Identity/User.cs +++ b/Infrastructure/Identity/User.cs @@ -1,9 +1,10 @@ using System; +using Application; using Microsoft.AspNetCore.Identity; namespace Infrastructure.Identity { - public class User : IdentityUser + public class User : IdentityUser, IUser { // On peut ajouter des propriétés ici au besoin } diff --git a/Infrastructure/Repositories/Memory/Data.cs b/Infrastructure/Repositories/Memory/Data.cs index 3050981..3f52e1e 100644 --- a/Infrastructure/Repositories/Memory/Data.cs +++ b/Infrastructure/Repositories/Memory/Data.cs @@ -13,6 +13,8 @@ namespace Infrastructure.Repositories.Memory public IQueryable Comments => _commentRepository.Comments.AsQueryable(); + public IQueryable Users => throw new System.NotImplementedException(); + public Data(LinkRepository linkRepository, CommentRepository commentRepository) { _linkRepository = linkRepository;