You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.6 KiB

using BukiVedi.Shared.Entities;
using BukiVedi.Shared.Models;
using BukiVedi.Shared.Services;
using MongoDB.Driver;
namespace BukiVedi.App.Services.Mappers
{
public class BookEntityMapper
{
public static async Task<IEnumerable<BookInfo>> Map(IEnumerable<BookEntity> books)
{
var booksIds = books.Select(x => x.Id).ToList();
var tags_list = (await Tables.UserTag.Get(Builders<UserTag>.Filter.In(t => t.BookId, booksIds)));
var tags = new Dictionary<string, List<TagInfo>>();
foreach (var t in tags_list)
{
if (tags.TryGetValue(t.BookId, out var list)) { list.Add(new TagInfo { Id = t.Id, Name = t.Name }); }
else
{
tags.Add(t.BookId,
new List<TagInfo>
{
new TagInfo { Id = t.Id, Name = t.Name }
});
}
}
return books.Select(book =>
new BookInfo
{
Authors = book.Authors.Select(a => new AuthorInfo { Id = a.Id, Name = a.Name }).ToArray(),
Description = book.Description,
Format = book.Format,
Id = book.Id,
Genres = new GenreInfo[1] { new GenreInfo { Id = book.Genre.Id, Name = book.Genre.Code } },
Title = book.Title,
Year = book.Year,
Tags = tags.ContainsKey(book.Id) ? tags[book.Id] : null!
});
}
}
}

Powered by TurnKey Linux.