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> Map(IEnumerable books) { var booksIds = books.Select(x => x.Id).ToList(); var tags_list = (await Tables.UserTag.Get(Builders.Filter.In(t => t.BookId, booksIds))); var tags = new Dictionary>(); 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 { 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! }); } } }