Ajout des commentaires dans Domain/Application/Infrastructure
This commit is contained in:
parent
18473a0174
commit
274e309a81
12
Application/CommentDTO.cs
Normal file
12
Application/CommentDTO.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Application
|
||||
{
|
||||
public class CommentDTO
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Content { get; set; }
|
||||
public int UpvotesCount { get; set; }
|
||||
public int DownvotesCount { get; set; }
|
||||
}
|
||||
}
|
||||
43
Application/CommentService.cs
Normal file
43
Application/CommentService.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Domain;
|
||||
|
||||
namespace Application
|
||||
{
|
||||
public class CommentService
|
||||
{
|
||||
private readonly ILinkRepository _linkRepository;
|
||||
private readonly ICommentRepository _commentRepository;
|
||||
private readonly IData _data;
|
||||
|
||||
public CommentService(ILinkRepository linkRepository, ICommentRepository commentRepository, IData data)
|
||||
{
|
||||
_linkRepository = linkRepository;
|
||||
_commentRepository = commentRepository;
|
||||
_data = data;
|
||||
}
|
||||
|
||||
public void PublishComment(PublishCommentCommand cmd)
|
||||
{
|
||||
var link = _linkRepository.GetById(cmd.LinkId);
|
||||
var comment = link.AddComment(cmd.Content);
|
||||
|
||||
_commentRepository.Add(comment);
|
||||
}
|
||||
|
||||
public CommentDTO[] GetAllLinkComments(Guid linkId)
|
||||
{
|
||||
return _data.Comments
|
||||
.OrderByDescending(comment => comment.CreatedAt)
|
||||
.Where(comment => comment.LinkId == linkId)
|
||||
.Select(comment => new CommentDTO
|
||||
{
|
||||
Id = comment.Id,
|
||||
Content = comment.Content,
|
||||
UpvotesCount = 0,
|
||||
DownvotesCount = 0,
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -9,5 +9,6 @@ namespace Application
|
||||
public interface IData
|
||||
{
|
||||
IQueryable<Link> Links { get; }
|
||||
IQueryable<Comment> Comments { get; }
|
||||
}
|
||||
}
|
||||
@ -38,18 +38,6 @@ namespace Application
|
||||
/// <returns></returns>
|
||||
public LinkDTO[] GetAllLinks()
|
||||
{
|
||||
// return (from link in _data.Links
|
||||
// orderby link.CreatedAt descending
|
||||
// select new LinkDTO
|
||||
// {
|
||||
// Id = link.Id,
|
||||
// Url = link.Url,
|
||||
// CreatedAt = link.CreatedAt,
|
||||
// UpvotesCount = 2,
|
||||
// DownvotesCount = 4,
|
||||
// CommentsCount = 54,
|
||||
// }).ToArray();
|
||||
|
||||
return LinksDTO().ToArray();
|
||||
}
|
||||
|
||||
@ -60,18 +48,30 @@ namespace Application
|
||||
|
||||
private IQueryable<LinkDTO> LinksDTO()
|
||||
{
|
||||
return _data.Links
|
||||
.Select(link => new LinkDTO
|
||||
{
|
||||
Id = link.Id,
|
||||
Url = link.Url,
|
||||
CreatedAt = link.CreatedAt,
|
||||
UpvotesCount = 2,
|
||||
DownvotesCount = 4,
|
||||
CommentsCount = 54,
|
||||
})
|
||||
// .Where(linkDto => linkDto.UpvotesCount > 5)
|
||||
.OrderByDescending(linkDto => linkDto.CreatedAt);
|
||||
return (from link in _data.Links
|
||||
join comment in _data.Comments on link.Id equals comment.LinkId into comments
|
||||
orderby link.CreatedAt descending
|
||||
select new LinkDTO
|
||||
{
|
||||
Id = link.Id,
|
||||
Url = link.Url,
|
||||
CreatedAt = link.CreatedAt,
|
||||
UpvotesCount = 2,
|
||||
DownvotesCount = 4,
|
||||
CommentsCount = comments.Count(),
|
||||
});
|
||||
|
||||
// return _data.Links
|
||||
// .Select(link => new LinkDTO
|
||||
// {
|
||||
// Id = link.Id,
|
||||
// Url = link.Url,
|
||||
// CreatedAt = link.CreatedAt,
|
||||
// UpvotesCount = 2,
|
||||
// DownvotesCount = 4,
|
||||
// CommentsCount = _data.Comments.Count(comment => comment.LinkId == link.Id),
|
||||
// })
|
||||
// .OrderByDescending(linkDto => linkDto.CreatedAt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
14
Application/PublishCommentCommand.cs
Normal file
14
Application/PublishCommentCommand.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Application
|
||||
{
|
||||
public class PublishCommentCommand
|
||||
{
|
||||
[Required]
|
||||
public Guid LinkId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Content { get; set; }
|
||||
}
|
||||
}
|
||||
20
Domain/Comment.cs
Normal file
20
Domain/Comment.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Domain
|
||||
{
|
||||
public class Comment
|
||||
{
|
||||
public Guid Id { get; }
|
||||
public Guid LinkId { get; }
|
||||
public string Content { get; }
|
||||
public DateTime CreatedAt { get; }
|
||||
|
||||
internal Comment(Guid linkId, string content)
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
LinkId = linkId;
|
||||
Content = content;
|
||||
CreatedAt = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Domain/ICommentRepository.cs
Normal file
7
Domain/ICommentRepository.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Domain
|
||||
{
|
||||
public interface ICommentRepository
|
||||
{
|
||||
void Add(Comment comment);
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace Domain
|
||||
{
|
||||
public interface ILinkRepository
|
||||
{
|
||||
void Add(Link link);
|
||||
Link GetById(Guid id);
|
||||
}
|
||||
}
|
||||
@ -17,5 +17,16 @@ namespace Domain
|
||||
CreatedAt = DateTime.UtcNow;
|
||||
Url = url;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Crée un commentaire pour ce lien. Permet de valider l'existence du lien et
|
||||
/// potentiellement des règles métier de création plus complexes.
|
||||
/// </summary>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public Comment AddComment(string content)
|
||||
{
|
||||
return new Comment(this.Id, content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
Infrastructure/Repositories/Memory/CommentRepository.cs
Normal file
21
Infrastructure/Repositories/Memory/CommentRepository.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Domain;
|
||||
|
||||
namespace Infrastructure.Repositories.Memory
|
||||
{
|
||||
public class CommentRepository : ICommentRepository
|
||||
{
|
||||
public List<Comment> Comments { get; }
|
||||
|
||||
public CommentRepository(params Comment[] comments)
|
||||
{
|
||||
Comments = comments.ToList();
|
||||
}
|
||||
|
||||
public void Add(Comment comment)
|
||||
{
|
||||
Comments.Add(comment);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,12 +7,16 @@ namespace Infrastructure.Repositories.Memory
|
||||
public class Data : IData
|
||||
{
|
||||
private readonly LinkRepository _linkRepository;
|
||||
private readonly CommentRepository _commentRepository;
|
||||
|
||||
public IQueryable<Link> Links => _linkRepository.Links.AsQueryable();
|
||||
|
||||
public Data(LinkRepository linkRepository)
|
||||
public IQueryable<Comment> Comments => _commentRepository.Comments.AsQueryable();
|
||||
|
||||
public Data(LinkRepository linkRepository, CommentRepository commentRepository)
|
||||
{
|
||||
_linkRepository = linkRepository;
|
||||
_commentRepository = commentRepository;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Domain;
|
||||
@ -17,5 +18,10 @@ namespace Infrastructure.Repositories.Memory
|
||||
{
|
||||
Links.Add(link);
|
||||
}
|
||||
|
||||
public Link GetById(Guid id)
|
||||
{
|
||||
return Links.Single(link => link.Id == id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user