залив папки Repositores

This commit is contained in:
Aleksandr4350 2024-12-20 11:52:29 +04:00
parent 01a7cffa4e
commit d42bfcd0a3
3 changed files with 61 additions and 12 deletions

View File

@ -4,7 +4,7 @@
public interface IOrderRepository
{
IEnumerable<Orders> ReadOrders();
IEnumerable<Orders> ReadOrders(DateTime? StartDate = null, DateTime? EndDate = null);
Orders ReadOrderById(int id);
void CreateOrder(Orders order);
void DeleteOrder(int id);

View File

@ -164,14 +164,35 @@ namespace ProjectLibrary.Repositories.Implementations
public IEnumerable<Library> ReadLibraries()
{
_logger.LogInformation("Получение всех библиотек");
var builder = new QueryBuilder();
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM Library";
var libraries = connection.Query<Library>(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<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));
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)
{

View File

@ -108,20 +108,48 @@ namespace ProjectLibrary.Repositories.Implementations
}
// Получение всех заказов
public IEnumerable<Orders> ReadOrders()
public IEnumerable<Orders> 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<TempBookOrders>(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<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));
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)
{