ПИбд-22 Хасянов А.Н. Лаб 2 #2
@ -3,9 +3,10 @@
|
||||
[Flags]
|
||||
public enum BusColors
|
||||
{
|
||||
White = 0,
|
||||
Yellow = 1,
|
||||
Black = 2,
|
||||
Blue = 4,
|
||||
Green = 8
|
||||
None = 0,
|
||||
White = 1,
|
||||
Yellow = 2,
|
||||
Black = 4,
|
||||
Blue = 8,
|
||||
Green = 16
|
||||
}
|
@ -7,20 +7,20 @@ public class Itinerary
|
||||
{
|
||||
public int Id { 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 ConductorId { get; private set; }
|
||||
public DateTime ItineraryDate { get; private set; }
|
||||
public string Description { get; private set; } = string.Empty;
|
||||
|
||||
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
|
||||
{
|
||||
Id = id,
|
||||
BusId = busId,
|
||||
ItineraryRoute = itineraryRoute,
|
||||
ItineraryRoutes = itineraryRoutes,
|
||||
DriverId = driverId,
|
||||
ConductorId = conductorId,
|
||||
ItineraryDate = itineraryDate,
|
||||
|
@ -74,7 +74,7 @@ public partial class FormBus : Form
|
||||
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
||||
private Bus CreateBus(int id)
|
||||
{
|
||||
BusColors busColors = BusColors.White;
|
||||
BusColors busColors = BusColors.None;
|
||||
foreach (var elem in checkedListBoxBusColors.CheckedItems)
|
||||
{
|
||||
busColors |= (BusColors)elem;
|
||||
|
@ -145,7 +145,6 @@
|
||||
private Label label1;
|
||||
private Label label2;
|
||||
private Label label3;
|
||||
private NumericUpDown numericUpDown1;
|
||||
private DateTimePicker dateTimePickerRepairDate;
|
||||
private TextBox textBoxDescription;
|
||||
private Button buttonCancel;
|
||||
|
@ -70,6 +70,7 @@
|
||||
dataGridViewData.Name = "dataGridViewData";
|
||||
dataGridViewData.ReadOnly = true;
|
||||
dataGridViewData.RowHeadersVisible = false;
|
||||
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewData.Size = new Size(682, 411);
|
||||
dataGridViewData.TabIndex = 3;
|
||||
//
|
||||
|
@ -96,6 +96,7 @@
|
||||
dataGridViewData.Name = "dataGridViewData";
|
||||
dataGridViewData.ReadOnly = true;
|
||||
dataGridViewData.RowHeadersVisible = false;
|
||||
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewData.Size = new Size(682, 411);
|
||||
dataGridViewData.TabIndex = 2;
|
||||
//
|
||||
|
@ -96,6 +96,7 @@
|
||||
dataGridViewData.Name = "dataGridViewData";
|
||||
dataGridViewData.ReadOnly = true;
|
||||
dataGridViewData.RowHeadersVisible = false;
|
||||
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewData.Size = new Size(682, 411);
|
||||
dataGridViewData.TabIndex = 1;
|
||||
//
|
||||
|
@ -14,6 +14,7 @@ public partial class FormEmployees : Form
|
||||
_employeeRepository = employeeRepository ??
|
||||
throw new ArgumentNullException(nameof(employeeRepository));
|
||||
}
|
||||
|
||||
private void FormEmployees_Load(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
|
@ -83,6 +83,7 @@
|
||||
dataGridViewData.Name = "dataGridViewData";
|
||||
dataGridViewData.ReadOnly = true;
|
||||
dataGridViewData.RowHeadersVisible = false;
|
||||
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewData.Size = new Size(682, 411);
|
||||
dataGridViewData.TabIndex = 3;
|
||||
//
|
||||
|
@ -52,6 +52,7 @@
|
||||
dataGridViewData.Name = "dataGridViewData";
|
||||
dataGridViewData.ReadOnly = true;
|
||||
dataGridViewData.RowHeadersVisible = false;
|
||||
dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridViewData.Size = new Size(681, 411);
|
||||
dataGridViewData.TabIndex = 0;
|
||||
//
|
||||
|
@ -2,6 +2,10 @@ using ProjectAutoenterprise.Repositories.Implementations;
|
||||
using ProjectAutoenterprise.Repositories;
|
||||
using Unity;
|
||||
using Unity.Lifetime;
|
||||
using Unity.Microsoft.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
|
||||
namespace ProjectAutoenterprise
|
||||
{
|
||||
@ -21,12 +25,28 @@ namespace ProjectAutoenterprise
|
||||
private static IUnityContainer CreateContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new TransientLifetimeManager());
|
||||
container.RegisterType<IBusRepairRepository, BusRepairRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IBusRepository, BusRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IEmployeeRepository, EmployeeRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IItineraryRepository, ItineraryRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IRouteRepository, RouteRepository>(new TransientLifetimeManager());
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
private static LoggerFactory CreateLoggerFactory()
|
||||
{
|
||||
var loggerFactory = new LoggerFactory();
|
||||
loggerFactory.AddSerilog(new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build())
|
||||
.CreateLogger());
|
||||
return loggerFactory;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,19 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" 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="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql" Version="9.0.1" />
|
||||
<PackageReference Include="Serilog" Version="4.1.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -27,4 +39,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,6 @@
|
||||
namespace ProjectAutoenterprise.Repositories;
|
||||
|
||||
internal interface IConnectionString
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
@ -1,15 +1,56 @@
|
||||
using ProjectAutoenterprise.Entities;
|
||||
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAutoenterprise.Entities;
|
||||
using System.Data.SqlClient;
|
||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||
|
||||
internal class BusRepairRepository : IBusRepairRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<BusRepairRepository> _logger;
|
||||
public BusRepairRepository(IConnectionString connectionString, ILogger<BusRepairRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateBusRepair(BusRepair busRepair)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(busRepair));
|
||||
try
|
||||
{
|
||||
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)
|
||||
public IEnumerable<BusRepair> ReadBusRepairs(
|
||||
DateTime? dateForm = null, DateTime? dateTo = null, int? busId = null, string? description = null)
|
||||
{
|
||||
return [];
|
||||
_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,23 +1,114 @@
|
||||
using ProjectAutoenterprise.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAutoenterprise.Entities;
|
||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||
|
||||
internal class BusRepository : IBusRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<BusRepository> _logger;
|
||||
public BusRepository(IConnectionString connectionString, ILogger<BusRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateBus(Bus bus)
|
||||
{
|
||||
}
|
||||
public void DeleteBus(int id)
|
||||
{
|
||||
}
|
||||
public Bus ReadBusById(int id)
|
||||
{
|
||||
return Bus.CreateEntity(0, string.Empty, 0, 0);
|
||||
}
|
||||
public IEnumerable<Bus> ReadBuses()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_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);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=autoenterprise";
|
||||
}
|
@ -1,36 +1,133 @@
|
||||
using ProjectAutoenterprise.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAutoenterprise.Entities;
|
||||
using ProjectAutoenterprise.Entities.Enums;
|
||||
using System.Data.SqlClient;
|
||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||
|
||||
internal class EmployeeRepository : IEmployeeRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<EmployeeRepository> _logger;
|
||||
public EmployeeRepository(IConnectionString connectionString, ILogger<EmployeeRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateEmployee(Employee employee)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
|
||||
try
|
||||
{
|
||||
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)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteEmployee(int id)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Employee ReadEmployeeById(int id)
|
||||
{
|
||||
return Employee.CreateEntity(0, string.Empty, string.Empty, DateTime.MinValue, EmployeePost.None);
|
||||
}
|
||||
|
||||
public IEnumerable<Employee> ReadEmployees()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public IEnumerable<Employee> ReadEmployees(EmployeePost employeePost)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
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,18 +1,88 @@
|
||||
using ProjectAutoenterprise.Entities;
|
||||
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAutoenterprise.Entities;
|
||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||
|
||||
internal class ItineraryRepository : IItineraryRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<ItineraryRepository> _logger;
|
||||
|
||||
public ItineraryRepository(IConnectionString connectionString, ILogger<ItineraryRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
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)
|
||||
|
||||
public IEnumerable<Itinerary> ReadItinerary(
|
||||
DateTime? dateForm = null, DateTime? dateTo = null, int? driverId = null, int? conductorId = null)
|
||||
{
|
||||
return [];
|
||||
_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,23 +1,117 @@
|
||||
using ProjectAutoenterprise.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAutoenterprise.Entities;
|
||||
using System.Data.SqlClient;
|
||||
namespace ProjectAutoenterprise.Repositories.Implementations;
|
||||
|
||||
internal class RouteRepository : IRouteRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<RouteRepository> _logger;
|
||||
public RouteRepository(IConnectionString connectionString, ILogger<RouteRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateRoute(Route route)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_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);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteRoute(int id)
|
||||
{
|
||||
}
|
||||
public Route ReadRouteById(int id)
|
||||
{
|
||||
return Route.CreateEntity(0, string.Empty, string.Empty);
|
||||
}
|
||||
public IEnumerable<Route> ReadRoutes()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
15
ProjectAutoenterprise/ProjectAutoenterprise/appsettings.json
Normal file
15
ProjectAutoenterprise/ProjectAutoenterprise/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/autoenterprise_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user