86 lines
2.0 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;
public LinkService(
ILinkRepository repository,
ICommentRepository commentRepository,
IReadStore readStore)
{
_repository = repository;
_commentRepository = commentRepository;
_readStore = readStore;
}
public Guid PublishLink(PublishLinkCommand cmd)
{
var link = new Link(cmd.Url, cmd.Description);
_repository.Add(link);
return link.Id;
}
public Guid PublishComment(PublishCommentCommand cmd)
{
var comment = new Comment(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 class LinkHomePage
{
public Guid Id { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public int CommentsCount { get; set; }
}
public class LinkComment
{
public string Content { 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]
public string Content { get; set; }
}