ПИбд-22 Морозов Д.В. Лабораторная 3 #8
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRepairCompany.Entities;
|
||||
|
||||
public class DetailMovement
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int OrderId { get; set; }
|
||||
public int DetailId { get; set; }
|
||||
public int StorageId { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
|
||||
|
||||
public Order Order { get; set; }
|
||||
public Detail Detail { get; set; }
|
||||
public Storage Storage { get; set; }
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRepairCompany.Repositories;
|
||||
|
||||
public interface IDetailMovementRepository
|
||||
{
|
||||
IEnumerable<DetailMovement> ReadOrderDetailMovements(
|
||||
DateTime? dateFrom = null,
|
||||
DateTime? dateTo = null,
|
||||
int? storageId = null,
|
||||
int? detailId = null,
|
||||
int? orderId = null);
|
||||
|
||||
void CreateOrderDetailMovement(DetailMovement DetailMovement);
|
||||
|
||||
void DeleteOrderDetailMovement(int id);
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class DetailMovementRepository : IDetailMovementRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<DetailMovementRepository> _logger;
|
||||
|
||||
public DetailMovementRepository(IConnectionString connectionString, ILogger<DetailMovementRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateOrderDetailMovement(DetailMovement DetailMovement)
|
||||
{
|
||||
_logger.LogInformation("Добавление записи о движении детали.");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(DetailMovement));
|
||||
try
|
||||
{
|
||||
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO OrderDetailMovement (OrderId, DetailId, StorageId, Quantity, MovementDate)
|
||||
VALUES (@OrderId, @DetailId, @StorageId, @Quantity, @MovementDate)
|
||||
";
|
||||
connection.Execute(queryInsert, DetailMovement);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка добавления записи о движении детали.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteOrderDetailMovement(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление записи о движении детали.");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = "DELETE FROM OrderDetailMovement WHERE Id = @Id";
|
||||
connection.Execute(queryDelete, new { Id = id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении записи о движении детали.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<DetailMovement> ReadOrderDetailMovements(DateTime? dateFrom = null, DateTime? dateTo = null, int? storageId = null, int? detailId = null, int? orderId = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех записей о движении деталей.");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM OrderDetailMovement";
|
||||
|
||||
var orderDetailMovements = connection.Query<DetailMovement>(querySelect).ToList();
|
||||
_logger.LogDebug("Полученные записи: {json}", JsonConvert.SerializeObject(orderDetailMovements));
|
||||
return orderDetailMovements;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении записей о движении деталей.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user