diff --git a/ProjectLibrary/Repositores/IOrderRepository.cs b/ProjectLibrary/Repositores/IOrderRepository.cs index 0249e15..660cdc3 100644 --- a/ProjectLibrary/Repositores/IOrderRepository.cs +++ b/ProjectLibrary/Repositores/IOrderRepository.cs @@ -4,7 +4,7 @@ public interface IOrderRepository { - IEnumerable ReadOrders(); + IEnumerable ReadOrders(DateTime? StartDate = null, DateTime? EndDate = null); Orders ReadOrderById(int id); void CreateOrder(Orders order); void DeleteOrder(int id); diff --git a/ProjectLibrary/Repositores/Implementations/LibraryRepository.cs b/ProjectLibrary/Repositores/Implementations/LibraryRepository.cs index fccda71..d99e1fa 100644 --- a/ProjectLibrary/Repositores/Implementations/LibraryRepository.cs +++ b/ProjectLibrary/Repositores/Implementations/LibraryRepository.cs @@ -164,14 +164,35 @@ namespace ProjectLibrary.Repositories.Implementations public IEnumerable ReadLibraries() { _logger.LogInformation("Получение всех библиотек"); - + var builder = new QueryBuilder(); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM Library"; - var libraries = connection.Query(querySelect); + var querySelect = @$"SELECT lbr.*, + bl.Bookid as Bookid, + bl.count + from library lbr + left join book_library bl on lbr.id = bl.libraryid {builder.Build()}"; + var BooksFromLibraryDict = new Dictionary>(); + + var libraries = connection.Query(querySelect, (libr, bookfromlib) => + { + if (!BooksFromLibraryDict.TryGetValue(libr.Id, out var booklibrer)) + { + booklibrer = []; + BooksFromLibraryDict.Add(libr.Id, booklibrer); + } + booklibrer.Add(bookfromlib); + return libr; + }, + splitOn: "Bookid", param: new { }); _logger.LogDebug("Полученные библиотеки: {json}", JsonConvert.SerializeObject(libraries)); - return libraries; + return BooksFromLibraryDict.Select(x => + { + var lbfb = libraries.First(y => y.Id == x.Key); + lbfb.SetLibraryForBook(x.Value); + return lbfb; + }).ToList(); } catch (Exception ex) { diff --git a/ProjectLibrary/Repositores/Implementations/OrderRepository.cs b/ProjectLibrary/Repositores/Implementations/OrderRepository.cs index b839c99..6d8e7e2 100644 --- a/ProjectLibrary/Repositores/Implementations/OrderRepository.cs +++ b/ProjectLibrary/Repositores/Implementations/OrderRepository.cs @@ -108,20 +108,48 @@ namespace ProjectLibrary.Repositories.Implementations } // Получение всех заказов - public IEnumerable ReadOrders() + public IEnumerable ReadOrders(DateTime? StartDate = null, DateTime? EndDate = null) { _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT ord.*, bo.BookId, bo.Count + var builder = new QueryBuilder(); + if(StartDate.HasValue) + { + builder.AddCollerction("ord.OrderDate >= @StartDate"); + } + + var querySelect = $@"SELECT ord.*, + Obo.BookId as bookid, + Obo.Count, + bk.Name FROM Orders ord - INNER JOIN Book_Orders bo ON bo.orderId = ord.Id"; - var order = connection.Query(querySelect); + INNER JOIN Book_Orders Obo ON Obo.orderId = ord.Id + Inner join book bk on bk.ID = obo.Bookid + {builder.Build()}"; + var OrderBookDict = new Dictionary>(); + + var order = connection + .Query(querySelect, (orders, books_orders) => + { + if (!OrderBookDict.TryGetValue(orders.Id, out var Book_Orders)) + { + Book_Orders = []; + OrderBookDict.Add(orders.Id, Book_Orders); + } + Book_Orders.Add(books_orders); + return orders; + }, + splitOn: "bookid", param: new { StartDate, EndDate }); + _logger.LogDebug("Получ енные объекты: {json}", JsonConvert.SerializeObject(order)); - return order.GroupBy(x => x.Id, y => y, - (key, value) => Orders.CreateEntity(value.First(), - value.Select(z => Book_Orders.CreateEntity(z.Id, z.BookID, z.Count)))).ToList(); + return OrderBookDict.Select(x => + { + var or = order.First(y => y.Id == x.Key); + or.SetOrdersOfBooks(x.Value); + return or; + }).ToArray(); } catch (Exception ex) {