using System;
using System.Linq;
using Domain;
namespace Application
{
///
/// Service applicatif permettant la gestion des liens de la plateforme.
///
public class LinkService
{
private readonly ILinkRepository _linkRepository;
private readonly IData _data;
public LinkService(ILinkRepository linkRepository, IData data)
{
_linkRepository = linkRepository;
_data = data;
}
///
/// Publie un nouveau lien sur la plateforme.
///
///
///
public Guid PublishLink(PublishLinkCommand cmd)
{
var link = new Link(cmd.Url);
_linkRepository.Add(link);
return link.Id;
}
///
/// Récupère tous les liens publiés jusqu'à présent.
///
///
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();
}
public LinkDTO GetLinkById(Guid id)
{
return LinksDTO().Single(link => link.Id == id);
}
private IQueryable 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);
}
}
}