2021-12-15 16:05:50 +01:00

99 lines
2.4 KiB
C#

using System.ComponentModel.DataAnnotations;
using HackerNet.Domain;
namespace HackerNet.Application;
public class LinkService
{
private readonly ILinkRepository _repository;
private readonly ICommentRepository _commentRepository;
private readonly IReadStore _readStore;
private readonly ICurrentUser _currentUser;
public LinkService(
ILinkRepository repository,
ICommentRepository commentRepository,
IReadStore readStore,
ICurrentUser currentUser)
{
_repository = repository;
_commentRepository = commentRepository;
_readStore = readStore;
_currentUser = currentUser;
}
public Guid PublishLink(PublishLinkCommand cmd)
{
var link = new Link(_currentUser.GetCurrentUserId(),
cmd.Url,
cmd.Description);
_repository.Add(link);
return link.Id;
}
public Guid PublishComment(PublishCommentCommand cmd)
{
var comment = new Comment(_currentUser.GetCurrentUserId(), cmd.LinkId, cmd.Content);
_commentRepository.Add(comment);
return comment.Id;
}
public LinkHomePage[] GetPublishedLinks()
=> _readStore.GetPublishedLinks();
public LinkHomePage GetLinkDetail(Guid id)
=> _readStore.GetLinkDetail(id);
public LinkComment[] GetLinkComments(Guid linkId)
=> _readStore.GetLinkComments(linkId);
}
public interface IReadStore
{
LinkHomePage[] GetPublishedLinks();
LinkHomePage GetLinkDetail(Guid id);
LinkComment[] GetLinkComments(Guid linkId);
}
public interface ICurrentUser
{
string GetCurrentUserId();
}
public class LinkHomePage
{
public Guid Id { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public int CommentsCount { get; set; }
public string Author { get; set; }
}
public class LinkComment
{
public string Content { get; set; }
public string User { get; set; }
}
public class PublishLinkCommand
{
[Required(ErrorMessage = "L'url est requise")]
[Display(Name = "Url du site")]
[Url]
public string Url { get; set; }
[Required(ErrorMessage = "La description est obligatoire")]
public string Description { get; set; }
}
public class PublishCommentCommand
{
public Guid LinkId { get; set; }
[Required(ErrorMessage = "Le contenu est obligatoire")]
[Display(Name = "Votre commentaire")]
public string Content { get; set; }
}