2024-12-20 23:05:27 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using LDBproject.Entities;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using Dapper;
|
2024-12-20 20:09:17 +04:00
|
|
|
|
|
|
|
|
|
namespace LDBproject.Repositories.Implementations;
|
|
|
|
|
|
|
|
|
|
public class OrderR : IOrderRep
|
|
|
|
|
{
|
2024-12-20 23:05:27 +04:00
|
|
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
|
private readonly ILogger<OrderR> _logger;
|
2024-12-20 20:09:17 +04:00
|
|
|
|
|
2024-12-20 23:05:27 +04:00
|
|
|
|
public OrderR(IConnectionString connectionString, ILogger<OrderR> logger)
|
|
|
|
|
{
|
|
|
|
|
_connectionString = connectionString;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
2024-12-20 20:09:17 +04:00
|
|
|
|
|
2024-12-20 23:05:27 +04:00
|
|
|
|
public void CreateOrder(Order order)
|
2024-12-20 20:09:17 +04:00
|
|
|
|
{
|
2024-12-20 23:05:27 +04:00
|
|
|
|
_logger.LogInformation("< Adding new ORDER > [!]");
|
|
|
|
|
_logger.LogDebug("Object - {json}", JsonConvert.SerializeObject(order));
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
connection.Open();
|
|
|
|
|
using var transaction = connection.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
// 1. Insert the Order without RETURNING
|
|
|
|
|
var queryInsert = @"INSERT INTO Orders (CardID, LibrarianID, BorrowDate) VALUES (@CardID, @LibrarianID, @BorrowDate)";
|
|
|
|
|
|
|
|
|
|
connection.Execute(queryInsert, new
|
|
|
|
|
{
|
|
|
|
|
order.CardID,
|
|
|
|
|
order.LibrarianID,
|
|
|
|
|
order.BorrowDate
|
|
|
|
|
}, transaction);
|
|
|
|
|
|
|
|
|
|
// 2. Get the last inserted OrderID
|
|
|
|
|
var queryGetLastInsertedId = "SELECT MAX(OrderID) FROM Orders";
|
|
|
|
|
|
|
|
|
|
int orderID = connection.QuerySingle<int>(queryGetLastInsertedId, transaction: transaction);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Insert the Registrations associated with the order
|
|
|
|
|
var querySubInsert = @"INSERT INTO Registrations (OrderID, BookID, Note) VALUES (@OrderID, @BookID, @Note)";
|
|
|
|
|
foreach (var elem in order.Registrations)
|
|
|
|
|
{
|
|
|
|
|
connection.Execute(querySubInsert, new { OrderID = orderID, elem.BookID, elem.Note }, transaction);
|
|
|
|
|
}
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "< Error while adding ORDER >");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateOrderInfo(Order order)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("< Updating order info >");
|
|
|
|
|
_logger.LogDebug("Object - {json}", JsonConvert.SerializeObject(order));
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
using var transaction = connection.BeginTransaction();
|
|
|
|
|
// 1. Update order
|
|
|
|
|
var queryUpdate = @"
|
|
|
|
|
UPDATE Orders SET
|
|
|
|
|
CardID = @CardID,
|
|
|
|
|
LibrarianID = @LibrarianID,
|
|
|
|
|
BorrowDate = @BorrowDate
|
|
|
|
|
WHERE OrderID = @OrderID";
|
|
|
|
|
|
|
|
|
|
connection.Execute(queryUpdate, new
|
|
|
|
|
{
|
|
|
|
|
order.CardID,
|
|
|
|
|
order.LibrarianID,
|
|
|
|
|
order.BorrowDate,
|
|
|
|
|
order.OrderID
|
|
|
|
|
}, transaction);
|
|
|
|
|
|
|
|
|
|
//2. Update registrations:
|
|
|
|
|
var queryDeleteRegistrations = "DELETE FROM Registrations WHERE OrderID = @OrderID";
|
|
|
|
|
connection.Execute(queryDeleteRegistrations, new { OrderID = order.OrderID }, transaction);
|
|
|
|
|
|
|
|
|
|
var querySubInsert = @"INSERT INTO Registrations (OrderID, BookID, Note) VALUES (@OrderID, @BookID, @Note)";
|
|
|
|
|
foreach (var elem in order.Registrations)
|
|
|
|
|
{
|
|
|
|
|
connection.Execute(querySubInsert, new { OrderID = order.OrderID, elem.BookID, elem.Note }, transaction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "< Error while updating order info >");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteOrderinfo(int orderID)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("< Deleting exact order >");
|
|
|
|
|
_logger.LogDebug("Obj: {id}", orderID);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
connection.Open();
|
|
|
|
|
using var transaction = connection.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
// 1. Delete registrations for the order
|
|
|
|
|
var queryDeleteRegistrations = @"DELETE FROM Registrations WHERE OrderID=@OrderID";
|
|
|
|
|
connection.Execute(queryDeleteRegistrations, new { OrderID = orderID }, transaction);
|
|
|
|
|
// 2. Delete the order itself
|
|
|
|
|
var queryDel = @"DELETE FROM Orders WHERE OrderID=@OrderID";
|
|
|
|
|
connection.Execute(queryDel, new { orderID }, transaction);
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "< Error while deleting ORDER >");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 20:42:47 +04:00
|
|
|
|
public IEnumerable<Order> GetOrdersInfo()
|
2024-12-20 23:05:27 +04:00
|
|
|
|
{
|
2024-12-21 20:42:47 +04:00
|
|
|
|
_logger.LogInformation("< Getting ORDERS based on a date >");
|
2024-12-20 23:05:27 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
connection.Open();
|
2024-12-21 20:42:47 +04:00
|
|
|
|
var querySelectAll = @"SELECT orders.*, regs.BookID, regs.Note FROM Orders orders
|
|
|
|
|
INNER JOIN Registrations regs ON regs.OrderID = orders.OrderID";
|
2024-12-20 23:05:27 +04:00
|
|
|
|
|
2024-12-21 20:42:47 +04:00
|
|
|
|
var orders = connection.Query<TemprOrderReg>(querySelectAll);
|
2024-12-20 23:05:27 +04:00
|
|
|
|
_logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(orders));
|
2024-12-21 20:42:47 +04:00
|
|
|
|
return orders.GroupBy(x => x.OrderID, y => y, (key, value) => Order.NewOrder(value.First(),
|
|
|
|
|
value.Select(z => Registration.OrderReg(z.OrderID, z.BookID, z.Note)))).ToList();
|
2024-12-20 23:05:27 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "< Error while reading ORDERS >");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-12-20 20:09:17 +04:00
|
|
|
|
}
|
|
|
|
|
}
|