156 lines
5.4 KiB
C#
Raw Normal View History

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
{
private readonly IConnectionString _connectionString;
private readonly ILogger<OrderR> _logger;
2024-12-20 20:09:17 +04:00
public OrderR(IConnectionString connectionString, ILogger<OrderR> logger)
{
_connectionString = connectionString;
_logger = logger;
}
2024-12-20 20:09:17 +04:00
public void CreateOrder(Order order)
2024-12-20 20:09:17 +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;
}
}
// ----------------------------- [ REVIEWED (waits for further editing) ] -------------------------------
public IEnumerable<Order> GetOrdersInfo(int? orderID = null, int ? librarianID = null, int? customerID = null)
{
_logger.LogInformation("< Getting all ORDERS >");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
var querySelectAll = @"SELECT * FROM Orders";
var orders = connection.Query<Order>(querySelectAll);
_logger.LogDebug("Aimed objects: {json}", JsonConvert.SerializeObject(orders));
return orders;
}
catch (Exception ex)
{
_logger.LogError(ex, "< Error while reading ORDERS >");
throw;
}
2024-12-20 20:09:17 +04:00
}
}