LabWork 2.1.0 (Подключение к БД)
This commit is contained in:
parent
3335f9ec1a
commit
4c93a225d9
@ -3,9 +3,10 @@
|
|||||||
[Flags]
|
[Flags]
|
||||||
public enum BusColors
|
public enum BusColors
|
||||||
{
|
{
|
||||||
White = 0,
|
None = 0,
|
||||||
Yellow = 1,
|
White = 1,
|
||||||
Black = 2,
|
Yellow = 2,
|
||||||
Blue = 4,
|
Black = 4,
|
||||||
Green = 8
|
Blue = 8,
|
||||||
|
Green = 16
|
||||||
}
|
}
|
@ -7,20 +7,20 @@ public class Itinerary
|
|||||||
{
|
{
|
||||||
public int Id { get; private set; }
|
public int Id { get; private set; }
|
||||||
public int BusId { get; private set; }
|
public int BusId { get; private set; }
|
||||||
public IEnumerable<ItineraryRoute> ItineraryRoute { get; private set; } = [];
|
public IEnumerable<ItineraryRoute> ItineraryRoutes { get; private set; } = [];
|
||||||
public int DriverId { get; private set; }
|
public int DriverId { get; private set; }
|
||||||
public int ConductorId { get; private set; }
|
public int ConductorId { get; private set; }
|
||||||
public DateTime ItineraryDate { get; private set; }
|
public DateTime ItineraryDate { get; private set; }
|
||||||
public string Description { get; private set; } = string.Empty;
|
public string Description { get; private set; } = string.Empty;
|
||||||
|
|
||||||
public static Itinerary CreateOperation(int id, int busId, int driverId, int conductorId, DateTime itineraryDate, string description,
|
public static Itinerary CreateOperation(int id, int busId, int driverId, int conductorId, DateTime itineraryDate, string description,
|
||||||
IEnumerable<ItineraryRoute> itineraryRoute)
|
IEnumerable<ItineraryRoute> itineraryRoutes)
|
||||||
{
|
{
|
||||||
return new Itinerary
|
return new Itinerary
|
||||||
{
|
{
|
||||||
Id = id,
|
Id = id,
|
||||||
BusId = busId,
|
BusId = busId,
|
||||||
ItineraryRoute = itineraryRoute,
|
ItineraryRoutes = itineraryRoutes,
|
||||||
DriverId = driverId,
|
DriverId = driverId,
|
||||||
ConductorId = conductorId,
|
ConductorId = conductorId,
|
||||||
ItineraryDate = itineraryDate,
|
ItineraryDate = itineraryDate,
|
||||||
|
@ -70,6 +70,7 @@
|
|||||||
dataGridViewData.Name = "dataGridViewData";
|
dataGridViewData.Name = "dataGridViewData";
|
||||||
dataGridViewData.ReadOnly = true;
|
dataGridViewData.ReadOnly = true;
|
||||||
dataGridViewData.RowHeadersVisible = false;
|
dataGridViewData.RowHeadersVisible = false;
|
||||||
|
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridViewData.Size = new Size(682, 411);
|
dataGridViewData.Size = new Size(682, 411);
|
||||||
dataGridViewData.TabIndex = 3;
|
dataGridViewData.TabIndex = 3;
|
||||||
//
|
//
|
||||||
|
@ -96,6 +96,7 @@
|
|||||||
dataGridViewData.Name = "dataGridViewData";
|
dataGridViewData.Name = "dataGridViewData";
|
||||||
dataGridViewData.ReadOnly = true;
|
dataGridViewData.ReadOnly = true;
|
||||||
dataGridViewData.RowHeadersVisible = false;
|
dataGridViewData.RowHeadersVisible = false;
|
||||||
|
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridViewData.Size = new Size(682, 411);
|
dataGridViewData.Size = new Size(682, 411);
|
||||||
dataGridViewData.TabIndex = 2;
|
dataGridViewData.TabIndex = 2;
|
||||||
//
|
//
|
||||||
|
@ -96,6 +96,7 @@
|
|||||||
dataGridViewData.Name = "dataGridViewData";
|
dataGridViewData.Name = "dataGridViewData";
|
||||||
dataGridViewData.ReadOnly = true;
|
dataGridViewData.ReadOnly = true;
|
||||||
dataGridViewData.RowHeadersVisible = false;
|
dataGridViewData.RowHeadersVisible = false;
|
||||||
|
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridViewData.Size = new Size(682, 411);
|
dataGridViewData.Size = new Size(682, 411);
|
||||||
dataGridViewData.TabIndex = 1;
|
dataGridViewData.TabIndex = 1;
|
||||||
//
|
//
|
||||||
|
@ -14,6 +14,7 @@ public partial class FormEmployees : Form
|
|||||||
_employeeRepository = employeeRepository ??
|
_employeeRepository = employeeRepository ??
|
||||||
throw new ArgumentNullException(nameof(employeeRepository));
|
throw new ArgumentNullException(nameof(employeeRepository));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FormEmployees_Load(object sender, EventArgs e)
|
private void FormEmployees_Load(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -83,6 +83,7 @@
|
|||||||
dataGridViewData.Name = "dataGridViewData";
|
dataGridViewData.Name = "dataGridViewData";
|
||||||
dataGridViewData.ReadOnly = true;
|
dataGridViewData.ReadOnly = true;
|
||||||
dataGridViewData.RowHeadersVisible = false;
|
dataGridViewData.RowHeadersVisible = false;
|
||||||
|
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridViewData.Size = new Size(682, 411);
|
dataGridViewData.Size = new Size(682, 411);
|
||||||
dataGridViewData.TabIndex = 3;
|
dataGridViewData.TabIndex = 3;
|
||||||
//
|
//
|
||||||
|
@ -52,6 +52,7 @@
|
|||||||
dataGridViewData.Name = "dataGridViewData";
|
dataGridViewData.Name = "dataGridViewData";
|
||||||
dataGridViewData.ReadOnly = true;
|
dataGridViewData.ReadOnly = true;
|
||||||
dataGridViewData.RowHeadersVisible = false;
|
dataGridViewData.RowHeadersVisible = false;
|
||||||
|
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||||
dataGridViewData.Size = new Size(681, 411);
|
dataGridViewData.Size = new Size(681, 411);
|
||||||
dataGridViewData.TabIndex = 0;
|
dataGridViewData.TabIndex = 0;
|
||||||
//
|
//
|
||||||
|
@ -13,6 +13,8 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="Npgsql" Version="9.0.1" />
|
||||||
<PackageReference Include="Serilog" Version="4.1.0" />
|
<PackageReference Include="Serilog" Version="4.1.0" />
|
||||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
using ProjectAutoenterprise.Entities;
|
using ProjectAutoenterprise.Entities;
|
||||||
|
using System.Data.SqlClient;
|
||||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||||
|
|
||||||
internal class BusRepairRepository : IBusRepairRepository
|
internal class BusRepairRepository : IBusRepairRepository
|
||||||
{
|
{
|
||||||
private readonly IConnectionString _connectionString;
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
private readonly ILogger<BusRepairRepository> _logger;
|
private readonly ILogger<BusRepairRepository> _logger;
|
||||||
|
|
||||||
public BusRepairRepository(IConnectionString connectionString, ILogger<BusRepairRepository> logger)
|
public BusRepairRepository(IConnectionString connectionString, ILogger<BusRepairRepository> logger)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
@ -17,10 +18,39 @@ internal class BusRepairRepository : IBusRepairRepository
|
|||||||
|
|
||||||
public void CreateBusRepair(BusRepair busRepair)
|
public void CreateBusRepair(BusRepair busRepair)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
public IEnumerable<BusRepair> ReadBusRepairs(DateTime? dateForm = null, DateTime? dateTo = null,
|
_logger.LogDebug("Объект: {json}",
|
||||||
int? busId = null, string? description = null)
|
JsonConvert.SerializeObject(busRepair));
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return [];
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO BusRepairs (BusId, RepairDate, Description)
|
||||||
|
VALUES (@BusId, @RepairDate, @Description)";
|
||||||
|
connection.Execute(queryInsert, busRepair);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IEnumerable<BusRepair> ReadBusRepairs(
|
||||||
|
DateTime? dateForm = null, DateTime? dateTo = null, int? busId = null, string? description = null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM BusRepairs";
|
||||||
|
var busRepairss = connection.Query<BusRepair>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(busRepairss));
|
||||||
|
return busRepairss;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +1,14 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
using ProjectAutoenterprise.Entities;
|
using ProjectAutoenterprise.Entities;
|
||||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||||
|
|
||||||
internal class BusRepository : IBusRepository
|
internal class BusRepository : IBusRepository
|
||||||
{
|
{
|
||||||
private readonly IConnectionString _connectionString;
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
private readonly ILogger<BusRepository> _logger;
|
private readonly ILogger<BusRepository> _logger;
|
||||||
|
|
||||||
public BusRepository(IConnectionString connectionString, ILogger<BusRepository> logger)
|
public BusRepository(IConnectionString connectionString, ILogger<BusRepository> logger)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
@ -16,19 +17,98 @@ internal class BusRepository : IBusRepository
|
|||||||
|
|
||||||
public void CreateBus(Bus bus)
|
public void CreateBus(Bus bus)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
public void DeleteBus(int id)
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(bus));
|
||||||
|
try
|
||||||
{
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Buses (Brand, Capacity, BusColors)
|
||||||
|
VALUES (@Brand, @Capacity, @BusColors)";
|
||||||
|
connection.Execute(queryInsert, bus);
|
||||||
}
|
}
|
||||||
public Bus ReadBusById(int id)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return Bus.CreateEntity(0, string.Empty, 0, 0);
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
public IEnumerable<Bus> ReadBuses()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
public void UpdateBus(Bus bus)
|
public void UpdateBus(Bus bus)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(bus));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Buses
|
||||||
|
SET
|
||||||
|
Brand=@Brand,
|
||||||
|
Capacity=@Capacity,
|
||||||
|
BusColors=@BusColors
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, bus);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteBus(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Buses
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Bus ReadBusById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Buses
|
||||||
|
WHERE Id=@id";
|
||||||
|
var bus = connection.QueryFirst<Bus>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(bus));
|
||||||
|
return bus;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IEnumerable<Bus> ReadBuses()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Buses";
|
||||||
|
var buses = connection.Query<Bus>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buses));
|
||||||
|
return buses;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
internal class ConnectionString : IConnectionString
|
internal class ConnectionString : IConnectionString
|
||||||
{
|
{
|
||||||
string IConnectionString.ConnectionString => "";
|
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=autoenterprise";
|
||||||
}
|
}
|
@ -1,14 +1,16 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
using ProjectAutoenterprise.Entities;
|
using ProjectAutoenterprise.Entities;
|
||||||
using ProjectAutoenterprise.Entities.Enums;
|
using ProjectAutoenterprise.Entities.Enums;
|
||||||
|
using System.Data.SqlClient;
|
||||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||||
|
|
||||||
internal class EmployeeRepository : IEmployeeRepository
|
internal class EmployeeRepository : IEmployeeRepository
|
||||||
{
|
{
|
||||||
private readonly IConnectionString _connectionString;
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
private readonly ILogger<EmployeeRepository> _logger;
|
private readonly ILogger<EmployeeRepository> _logger;
|
||||||
|
|
||||||
public EmployeeRepository(IConnectionString connectionString, ILogger<EmployeeRepository> logger)
|
public EmployeeRepository(IConnectionString connectionString, ILogger<EmployeeRepository> logger)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
@ -17,31 +19,115 @@ internal class EmployeeRepository : IEmployeeRepository
|
|||||||
|
|
||||||
public void CreateEmployee(Employee employee)
|
public void CreateEmployee(Employee employee)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
}
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
|
||||||
|
try
|
||||||
public void DeleteEmployee(int id)
|
|
||||||
{
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Employees (FirstName, LastName, BirthDate, EmployeePost)
|
||||||
|
VALUES (@FirstName, @LastName, @BirthDate, @EmployeePost)";
|
||||||
|
connection.Execute(queryInsert, employee);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
public Employee ReadEmployeeById(int id)
|
|
||||||
{
|
{
|
||||||
return Employee.CreateEntity(0, string.Empty, string.Empty, DateTime.MinValue, EmployeePost.None);
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Employee> ReadEmployees()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Employee> ReadEmployees(EmployeePost employeePost)
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateEmployee(Employee employee)
|
public void UpdateEmployee(Employee employee)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Employees
|
||||||
|
SET
|
||||||
|
FirstName=@FirstName,
|
||||||
|
LastName=@LastName,
|
||||||
|
BirthDate=@BirthDate,
|
||||||
|
EmployeePost=@EmployeePost
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, employee);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void DeleteEmployee(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Employees
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Employee ReadEmployeeById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Employees
|
||||||
|
WHERE Id=@id";
|
||||||
|
var employee = connection.QueryFirst<Employee>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee));
|
||||||
|
return employee;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IEnumerable<Employee> ReadEmployees()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Employees";
|
||||||
|
var employees = connection.Query<Employee>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees));
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public IEnumerable<Employee> ReadEmployees(EmployeePost employeePost)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Employees WHERE EmployeePost='" + employeePost.ToString()+"'";
|
||||||
|
var employees = connection.Query<Employee>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees));
|
||||||
|
return employees;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
using ProjectAutoenterprise.Entities;
|
using ProjectAutoenterprise.Entities;
|
||||||
|
|
||||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||||
|
|
||||||
internal class ItineraryRepository : IItineraryRepository
|
internal class ItineraryRepository : IItineraryRepository
|
||||||
@ -17,13 +19,70 @@ internal class ItineraryRepository : IItineraryRepository
|
|||||||
|
|
||||||
public void CreateItinerary(Itinerary itinerary)
|
public void CreateItinerary(Itinerary itinerary)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(itinerary));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Itineraries (BusId, DriverId, ConductorId, ItineraryDate, Description)
|
||||||
|
VALUES (@BusId, @DriverId, @ConductorId, @ItineraryDate, @Description);
|
||||||
|
SELECT MAX(Id) FROM Itineraries";
|
||||||
|
var itineraryId = connection.QueryFirst<int>(queryInsert, itinerary, transaction);
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO ItineraryRoutes (ItineraryId, RouteId)
|
||||||
|
VALUES (@ItineraryId, @RouteId)";
|
||||||
|
foreach (var elem in itinerary.ItineraryRoutes)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new { itineraryId, elem.RouteId }, transaction);
|
||||||
}
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void DeleteItinerary(int id)
|
public void DeleteItinerary(int id)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Удаление объекта");
|
||||||
public IEnumerable<Itinerary> ReadItinerary(DateTime? dateForm = null, DateTime? dateTo = null,
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
int? driverId = null, int? conductorId = null)
|
try
|
||||||
{
|
{
|
||||||
return [];
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Itineraries
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Itinerary> ReadItinerary(
|
||||||
|
DateTime? dateForm = null, DateTime? dateTo = null, int? driverId = null, int? conductorId = null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"SELECT * FROM Itineraries";
|
||||||
|
var itineraries = connection.Query<Itinerary>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(itineraries));
|
||||||
|
return itineraries;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,13 +1,15 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
using ProjectAutoenterprise.Entities;
|
using ProjectAutoenterprise.Entities;
|
||||||
|
using System.Data.SqlClient;
|
||||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||||
|
|
||||||
internal class RouteRepository : IRouteRepository
|
internal class RouteRepository : IRouteRepository
|
||||||
{
|
{
|
||||||
private readonly IConnectionString _connectionString;
|
private readonly IConnectionString _connectionString;
|
||||||
|
|
||||||
private readonly ILogger<RouteRepository> _logger;
|
private readonly ILogger<RouteRepository> _logger;
|
||||||
|
|
||||||
public RouteRepository(IConnectionString connectionString, ILogger<RouteRepository> logger)
|
public RouteRepository(IConnectionString connectionString, ILogger<RouteRepository> logger)
|
||||||
{
|
{
|
||||||
_connectionString = connectionString;
|
_connectionString = connectionString;
|
||||||
@ -16,19 +18,100 @@ internal class RouteRepository : IRouteRepository
|
|||||||
|
|
||||||
public void CreateRoute(Route route)
|
public void CreateRoute(Route route)
|
||||||
{
|
{
|
||||||
}
|
_logger.LogInformation("Добавление объекта");
|
||||||
public void DeleteRoute(int id)
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route));
|
||||||
|
try
|
||||||
{
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Routes (BeginRoutePoint, EndRoutePoint)
|
||||||
|
VALUES (@BeginRoutePoint, @EndRoutePoint)";
|
||||||
|
connection.Execute(queryInsert, route);
|
||||||
}
|
}
|
||||||
public Route ReadRouteById(int id)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
return Route.CreateEntity(0, string.Empty, string.Empty);
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
}
|
}
|
||||||
public IEnumerable<Route> ReadRoutes()
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateRoute(Route route)
|
public void UpdateRoute(Route route)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Routes
|
||||||
|
SET
|
||||||
|
BeginRoutePoint=@BeginRoutePoint,
|
||||||
|
EndRoutePoint=@EndRoutePoint
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, route);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteRoute(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Routes
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Route ReadRouteById(int id)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Routes
|
||||||
|
WHERE Id=@id";
|
||||||
|
var route = connection.QueryFirst<Route>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(route));
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<Route> ReadRoutes()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Routes";
|
||||||
|
var routes = connection.Query<Route>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(routes));
|
||||||
|
return routes;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user