залив папки Repositores
This commit is contained in:
parent
01a7cffa4e
commit
d42bfcd0a3
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
public interface IOrderRepository
|
public interface IOrderRepository
|
||||||
{
|
{
|
||||||
IEnumerable<Orders> ReadOrders();
|
IEnumerable<Orders> ReadOrders(DateTime? StartDate = null, DateTime? EndDate = null);
|
||||||
Orders ReadOrderById(int id);
|
Orders ReadOrderById(int id);
|
||||||
void CreateOrder(Orders order);
|
void CreateOrder(Orders order);
|
||||||
void DeleteOrder(int id);
|
void DeleteOrder(int id);
|
||||||
|
@ -164,14 +164,35 @@ namespace ProjectLibrary.Repositories.Implementations
|
|||||||
public IEnumerable<Library> ReadLibraries()
|
public IEnumerable<Library> ReadLibraries()
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Получение всех библиотек");
|
_logger.LogInformation("Получение всех библиотек");
|
||||||
|
var builder = new QueryBuilder();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
var querySelect = "SELECT * FROM Library";
|
var querySelect = @$"SELECT lbr.*,
|
||||||
var libraries = connection.Query<Library>(querySelect);
|
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<int, List<Book_Library>>();
|
||||||
|
|
||||||
|
var libraries = connection.Query<Library, Book_Library, Library>(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));
|
_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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -108,20 +108,48 @@ namespace ProjectLibrary.Repositories.Implementations
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Получение всех заказов
|
// Получение всех заказов
|
||||||
public IEnumerable<Orders> ReadOrders()
|
public IEnumerable<Orders> ReadOrders(DateTime? StartDate = null, DateTime? EndDate = null)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Получение всех объектов");
|
_logger.LogInformation("Получение всех объектов");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
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
|
FROM Orders ord
|
||||||
INNER JOIN Book_Orders bo ON bo.orderId = ord.Id";
|
INNER JOIN Book_Orders Obo ON Obo.orderId = ord.Id
|
||||||
var order = connection.Query<TempBookOrders>(querySelect);
|
Inner join book bk on bk.ID = obo.Bookid
|
||||||
|
{builder.Build()}";
|
||||||
|
var OrderBookDict = new Dictionary<int, List<Book_Orders>>();
|
||||||
|
|
||||||
|
var order = connection
|
||||||
|
.Query<Orders, Book_Orders, Orders>(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));
|
_logger.LogDebug("Получ енные объекты: {json}", JsonConvert.SerializeObject(order));
|
||||||
return order.GroupBy(x => x.Id, y => y,
|
return OrderBookDict.Select(x =>
|
||||||
(key, value) => Orders.CreateEntity(value.First(),
|
{
|
||||||
value.Select(z => Book_Orders.CreateEntity(z.Id, z.BookID, z.Count)))).ToList();
|
var or = order.First(y => y.Id == x.Key);
|
||||||
|
or.SetOrdersOfBooks(x.Value);
|
||||||
|
return or;
|
||||||
|
}).ToArray();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user