ПИбд-23 Ладягин П.Д. Вторая Л.Р #2
@ -7,6 +7,8 @@ public class Route
|
||||
public string End_Point { get; private set; } = string.Empty;
|
||||
public float Route_Length { get; private set; }
|
||||
|
||||
public string fullRoute => $"{Start_Point} {End_Point}";
|
||||
|
||||
public static Route CreateEntity (int route_id, string start_point, string end_point, float length)
|
||||
{
|
||||
return new Route
|
||||
|
@ -11,9 +11,9 @@ public class Trip
|
||||
public float Fuel_Consumption { get; private set; }
|
||||
public int Car_ID { get; private set; }
|
||||
public int Driver_ID { get; private set; }
|
||||
public IEnumerable<Route> Routes { get; private set; } = [];
|
||||
public IEnumerable<Trip_Route> Routes { get; private set; } = [];
|
||||
|
||||
public static Trip CreateOperation(int trip_id, DateTime start_date, DateTime end_date, Shift shift, float consumption, int car_id, int driver_id, IEnumerable<Route> routes)
|
||||
public static Trip CreateOperation(int trip_id, DateTime start_date, DateTime end_date, Shift shift, float consumption, int car_id, int driver_id, IEnumerable<Trip_Route> routes)
|
||||
{
|
||||
return new Trip
|
||||
{
|
||||
|
22
FuelAndLubricants/FuelAndLubricants/Entities/Trip_Route.cs
Normal file
22
FuelAndLubricants/FuelAndLubricants/Entities/Trip_Route.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FuelAndLubricants.Entities;
|
||||
|
||||
public class Trip_Route
|
||||
{
|
||||
public int Trip_ID { get; private set; }
|
||||
public int Route_ID { get; private set; }
|
||||
|
||||
public static Trip_Route CreateElement(int trip_id, int route_id)
|
||||
{
|
||||
return new Trip_Route
|
||||
{
|
||||
Trip_ID = trip_id,
|
||||
Route_ID = route_id
|
||||
};
|
||||
}
|
||||
}
|
@ -148,6 +148,7 @@
|
||||
numericUpDownConsumptionRate.DecimalPlaces = 2;
|
||||
numericUpDownConsumptionRate.Location = new Point(122, 263);
|
||||
numericUpDownConsumptionRate.Margin = new Padding(3, 4, 3, 4);
|
||||
numericUpDownConsumptionRate.Maximum = new decimal(new int[] { 1000, 0, 0, 0 });
|
||||
numericUpDownConsumptionRate.Minimum = new decimal(new int[] { 1, 0, 0, 131072 });
|
||||
numericUpDownConsumptionRate.Name = "numericUpDownConsumptionRate";
|
||||
numericUpDownConsumptionRate.Size = new Size(160, 27);
|
||||
@ -183,7 +184,7 @@
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "FormCar";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "FormCar";
|
||||
Text = "Машина";
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownConsumptionRate).EndInit();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
|
@ -55,6 +55,7 @@
|
||||
numericUpDownPrice.DecimalPlaces = 2;
|
||||
numericUpDownPrice.Location = new Point(120, 51);
|
||||
numericUpDownPrice.Margin = new Padding(3, 4, 3, 4);
|
||||
numericUpDownPrice.Maximum = new decimal(new int[] { 1000, 0, 0, 0 });
|
||||
numericUpDownPrice.Minimum = new decimal(new int[] { 1, 0, 0, 131072 });
|
||||
numericUpDownPrice.Name = "numericUpDownPrice";
|
||||
numericUpDownPrice.Size = new Size(160, 27);
|
||||
@ -120,6 +121,7 @@
|
||||
numericUpDownAmount.DecimalPlaces = 2;
|
||||
numericUpDownAmount.Location = new Point(120, 89);
|
||||
numericUpDownAmount.Margin = new Padding(3, 4, 3, 4);
|
||||
numericUpDownAmount.Maximum = new decimal(new int[] { 10000, 0, 0, 0 });
|
||||
numericUpDownAmount.Minimum = new decimal(new int[] { 1, 0, 0, 131072 });
|
||||
numericUpDownAmount.Name = "numericUpDownAmount";
|
||||
numericUpDownAmount.Size = new Size(160, 27);
|
||||
@ -142,7 +144,7 @@
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "FormFuel";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "FormFuel";
|
||||
Text = "Топливо";
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownPrice).EndInit();
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownAmount).EndInit();
|
||||
ResumeLayout(false);
|
||||
|
@ -107,6 +107,7 @@
|
||||
numericUpDownRefillAmount.DecimalPlaces = 2;
|
||||
numericUpDownRefillAmount.Location = new Point(125, 52);
|
||||
numericUpDownRefillAmount.Margin = new Padding(3, 4, 3, 4);
|
||||
numericUpDownRefillAmount.Maximum = new decimal(new int[] { 1000, 0, 0, 0 });
|
||||
numericUpDownRefillAmount.Minimum = new decimal(new int[] { 1, 0, 0, 131072 });
|
||||
numericUpDownRefillAmount.Name = "numericUpDownRefillAmount";
|
||||
numericUpDownRefillAmount.Size = new Size(160, 27);
|
||||
@ -162,7 +163,7 @@
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "FormRefill";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "FormRefill";
|
||||
Text = "Заправка";
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownRefillAmount).EndInit();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
|
@ -54,6 +54,7 @@
|
||||
numericUpDownLength.DecimalPlaces = 2;
|
||||
numericUpDownLength.Location = new Point(134, 91);
|
||||
numericUpDownLength.Margin = new Padding(3, 4, 3, 4);
|
||||
numericUpDownLength.Maximum = new decimal(new int[] { 10000, 0, 0, 0 });
|
||||
numericUpDownLength.Minimum = new decimal(new int[] { 1, 0, 0, 131072 });
|
||||
numericUpDownLength.Name = "numericUpDownLength";
|
||||
numericUpDownLength.Size = new Size(160, 27);
|
||||
@ -136,7 +137,7 @@
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "FormRoute";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "FormRoute";
|
||||
Text = "Маршрут";
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownLength).EndInit();
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
|
@ -115,7 +115,7 @@
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "FormRoutes";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "FormRoutes";
|
||||
Text = "Маршруты";
|
||||
Load += FormRoutes_Load;
|
||||
panel1.ResumeLayout(false);
|
||||
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
|
||||
|
@ -45,7 +45,6 @@
|
||||
groupBox = new GroupBox();
|
||||
dataGridViewRoutes = new DataGridView();
|
||||
ColumnRoute = new DataGridViewComboBoxColumn();
|
||||
ColumnEndPoint = new DataGridViewTextBoxColumn();
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownConsumptionRate).BeginInit();
|
||||
groupBox.SuspendLayout();
|
||||
((System.ComponentModel.ISupportInitialize)dataGridViewRoutes).BeginInit();
|
||||
@ -209,7 +208,7 @@
|
||||
dataGridViewRoutes.AllowUserToResizeColumns = false;
|
||||
dataGridViewRoutes.AllowUserToResizeRows = false;
|
||||
dataGridViewRoutes.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
||||
dataGridViewRoutes.Columns.AddRange(new DataGridViewColumn[] { ColumnRoute, ColumnEndPoint });
|
||||
dataGridViewRoutes.Columns.AddRange(new DataGridViewColumn[] { ColumnRoute });
|
||||
dataGridViewRoutes.Dock = DockStyle.Fill;
|
||||
dataGridViewRoutes.Location = new Point(3, 23);
|
||||
dataGridViewRoutes.MultiSelect = false;
|
||||
@ -227,13 +226,6 @@
|
||||
ColumnRoute.Name = "ColumnRoute";
|
||||
ColumnRoute.Width = 125;
|
||||
//
|
||||
// ColumnEndPoint
|
||||
//
|
||||
ColumnEndPoint.HeaderText = "Конечная точка";
|
||||
ColumnEndPoint.MinimumWidth = 6;
|
||||
ColumnEndPoint.Name = "ColumnEndPoint";
|
||||
ColumnEndPoint.Width = 125;
|
||||
//
|
||||
// FormTrip
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
@ -257,7 +249,7 @@
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "FormTrip";
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
Text = "FormTrip";
|
||||
Text = "Поездка";
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownConsumptionRate).EndInit();
|
||||
groupBox.ResumeLayout(false);
|
||||
((System.ComponentModel.ISupportInitialize)dataGridViewRoutes).EndInit();
|
||||
@ -284,6 +276,5 @@
|
||||
private GroupBox groupBox;
|
||||
private DataGridView dataGridViewRoutes;
|
||||
private DataGridViewComboBoxColumn ColumnRoute;
|
||||
private DataGridViewTextBoxColumn ColumnEndPoint;
|
||||
}
|
||||
}
|
@ -15,17 +15,19 @@ namespace FuelAndLubricants.Forms
|
||||
_tripRepository = tripRepository ??
|
||||
throw new ArgumentNullException(nameof(tripRepository));
|
||||
|
||||
comboBoxCarID.DataSource = tripRepository.ReadTrips();
|
||||
comboBoxCarID.DataSource = carRepository.ReadCars();
|
||||
comboBoxCarID.DisplayMember = "Car_Mark";
|
||||
comboBoxCarID.ValueMember = "Car_ID";
|
||||
|
||||
comboBoxDriverID.DataSource = tripRepository.ReadTrips();
|
||||
comboBoxDriverID.DataSource = driverRepository.ReadDrivers();
|
||||
comboBoxDriverID.DisplayMember = "Firstname";
|
||||
comboBoxDriverID.ValueMember = "Driver_ID";
|
||||
|
||||
ColumnRoute.DataSource = routeRepository.ReadRoutes();
|
||||
ColumnRoute.DisplayMember = "Start_Point";
|
||||
ColumnRoute.DisplayMember = "fullRoute";
|
||||
ColumnRoute.ValueMember = "Route_ID";
|
||||
|
||||
comboBoxShift.DataSource = Enum.GetValues(typeof(Shift));
|
||||
}
|
||||
private void ButtonSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
@ -45,18 +47,17 @@ namespace FuelAndLubricants.Forms
|
||||
}
|
||||
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
||||
|
||||
private List<Route> CreateListDriversFromDataGrid()
|
||||
private List<Trip_Route> CreateListDriversFromDataGrid()
|
||||
{
|
||||
var list = new List<Route>();
|
||||
var list = new List<Trip_Route>();
|
||||
foreach (DataGridViewRow row in dataGridViewRoutes.Rows)
|
||||
{
|
||||
if (row.Cells["ColumnRoute"].Value == null || row.Cells["ColumnEndPoint"].Value == null)
|
||||
if (row.Cells["ColumnRoute"].Value == null)
|
||||
continue;
|
||||
|
||||
list.Add(Route.CreateEntity(0, (string)row.Cells["ColumnRoute"].Value, (string)row.Cells["ColumnEndPoint"].Value, 0));
|
||||
list.Add(Trip_Route.CreateElement(0, Convert.ToInt32(row.Cells["ColumnRoute"].Value)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -120,13 +120,4 @@
|
||||
<metadata name="ColumnRoute.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
<metadata name="ColumnEndPoint.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
<metadata name="ColumnRoute.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
<metadata name="ColumnEndPoint.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
</root>
|
@ -9,7 +9,20 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
|
||||
<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 +40,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -1,6 +1,10 @@
|
||||
using FuelAndLubricants.Repositories;
|
||||
using FuelAndLubricants.Repositories.Implementations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Unity;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace FuelAndLubricants
|
||||
{
|
||||
@ -21,6 +25,9 @@ namespace FuelAndLubricants
|
||||
private static UnityContainer CreateContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
|
||||
container.RegisterType<ICarRepository, CarRepository>();
|
||||
container.RegisterType<IDriverRepository, DriverRepository>();
|
||||
@ -28,8 +35,22 @@ namespace FuelAndLubricants
|
||||
container.RegisterType<IRefillRepository, RefillRepository>();
|
||||
container.RegisterType<IRouteRepository, RouteRepository>();
|
||||
container.RegisterType<ITripRepository, TripRepository>();
|
||||
container.RegisterType<IConnectionString, ConnectionString>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
namespace FuelAndLubricants.Repositories;
|
||||
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
}
|
@ -1,28 +1,123 @@
|
||||
using FuelAndLubricants.Entities;
|
||||
using System.Data.SqlClient;
|
||||
using Dapper;
|
||||
using FuelAndLubricants.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public class CarRepository : ICarRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<CarRepository> _logger;
|
||||
|
||||
public CarRepository (IConnectionString connectionString, ILogger<CarRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateCar(Car car)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(car));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"INSERT INTO Car (Car_Mark, Car_Model, Car_Type, License, Consumption_Rate)
|
||||
VALUES (@Car_Mark, @Car_Model, @Car_Type, @License, @Consumption_Rate)";
|
||||
connection.Execute(queryInsert, car);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteCar(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Car
|
||||
WHERE Car_ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Car ReadCarByID(int id)
|
||||
{
|
||||
return Car.CreateEntity(0, string.Empty, string.Empty, 0, 0, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Car
|
||||
WHERE Car_ID=@id";
|
||||
var car = connection.QueryFirst<Car>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(car));
|
||||
return car;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Car> ReadCars()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Car";
|
||||
var cars = connection.Query<Car>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(cars));
|
||||
return cars;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateCar(Car car)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(car));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Car
|
||||
SET
|
||||
Car_Mark=@Car_Mark,
|
||||
Car_Model=@Car_Model,
|
||||
Car_Type=@Car_Type,
|
||||
License=@License,
|
||||
Consumption_Rate=@Consumption_Rate
|
||||
WHERE Car_ID=@Car_ID";
|
||||
connection.Execute(queryUpdate, car);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=1111;Database=otplabs";
|
||||
}
|
@ -1,28 +1,120 @@
|
||||
using FuelAndLubricants.Entities;
|
||||
using Dapper;
|
||||
using FuelAndLubricants.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public class DriverRepository : IDriverRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<DriverRepository> _logger;
|
||||
|
||||
public DriverRepository(IConnectionString connectionString, ILogger<DriverRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateDriver(Driver driver)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(driver));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"INSERT INTO Driver (Firstname, Secondname, Driver_License)
|
||||
VALUES (@Firstname, @Secondname, @Driver_License)";
|
||||
connection.Execute(queryInsert, driver);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteDriver(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Driver
|
||||
WHERE Driver_ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Driver ReadDriverByID(int id)
|
||||
{
|
||||
return Driver.CreateEntity(0, string.Empty, string.Empty, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Driver
|
||||
WHERE Driver_ID=@id";
|
||||
var driver = connection.QueryFirst<Driver>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(driver));
|
||||
return driver;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Driver> ReadDrivers()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Driver";
|
||||
var drivers = connection.Query<Driver>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(drivers));
|
||||
return drivers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateDriver(Driver driver)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(driver));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Driver
|
||||
SET
|
||||
Firstname=@Firstname,
|
||||
Secondname=@Secondname,
|
||||
Driver_License=@Driver_License
|
||||
WHERE Driver_ID=@Driver_ID";
|
||||
connection.Execute(queryUpdate, driver);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,121 @@
|
||||
using FuelAndLubricants.Entities;
|
||||
using Dapper;
|
||||
using FuelAndLubricants.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public class FuelRepository : IFuelRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<FuelRepository> _logger;
|
||||
|
||||
public FuelRepository(IConnectionString connectionString, ILogger<FuelRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateFuel(Fuel_And_Lubricants fuel)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(fuel));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"INSERT INTO Fuel_And_Lubricants (Fuel_Type, Price_Per_Liter, Amount)
|
||||
VALUES (@Fuel_Type, @Price_Per_Liter, @Amount)";
|
||||
connection.Execute(queryInsert, fuel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void DeleteFuel(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Fuel_And_Lubricants
|
||||
WHERE Fuel_ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Fuel_And_Lubricants ReadFuelByID(int id)
|
||||
{
|
||||
return Fuel_And_Lubricants.CreateEntity(0, 0, 0, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Fuel_And_Lubricants
|
||||
WHERE Fuel_ID=@id";
|
||||
var fuel = connection.QueryFirst<Fuel_And_Lubricants>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(fuel));
|
||||
return fuel;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Fuel_And_Lubricants> ReadFuels()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Fuel_And_Lubricants";
|
||||
var fuels = connection.Query<Fuel_And_Lubricants>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(fuels));
|
||||
return fuels;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateFuel(Fuel_And_Lubricants fuel)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(fuel));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Fuel_And_Lubricants
|
||||
SET
|
||||
Fuel_Type=@Fuel_Type,
|
||||
Price_Per_Liter=@Price_Per_Liter,
|
||||
Amount=@Amount
|
||||
WHERE Fuel_ID=@Fuel_ID";
|
||||
connection.Execute(queryUpdate, fuel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,55 @@
|
||||
using FuelAndLubricants.Entities;
|
||||
using Dapper;
|
||||
using FuelAndLubricants.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public class RefillRepository : IRefillRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<RefillRepository> _logger;
|
||||
public RefillRepository(IConnectionString connectionString, ILogger<RefillRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateRefill(Refill refill)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(refill));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"INSERT INTO Refill (Refill_Date, Refill_Amount, Fuel_ID, Car_ID)
|
||||
VALUES (@Refill_Date, @Refill_Amount, @Fuel_ID, @Car_ID)";
|
||||
connection.Execute(queryInsert, refill);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Refill> ReadRefills(DateTime? dateFrom = null, DateTime? dateTo = null, int? fuelId = null, int? carId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Refill";
|
||||
var refills = connection.Query<Refill>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(refills));
|
||||
return refills;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,121 @@
|
||||
using FuelAndLubricants.Entities;
|
||||
using Dapper;
|
||||
using FuelAndLubricants.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public 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 Route (Start_Point, End_Point, Route_Length)
|
||||
VALUES (@Start_Point, @End_Point, @Route_Length)";
|
||||
connection.Execute(queryInsert, 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 Route
|
||||
WHERE Route_ID=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Route ReadRouteByID(int id)
|
||||
{
|
||||
return Route.CreateEntity(0, string.Empty, string.Empty, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Route
|
||||
WHERE Route_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()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Route";
|
||||
var routes = connection.Query<Route>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(routes));
|
||||
return routes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateRoute(Route route)
|
||||
{
|
||||
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Route
|
||||
SET
|
||||
Start_Point=@Start_Point,
|
||||
End_Point=@End_Point,
|
||||
Route_Length=@Route_Length
|
||||
WHERE Route_ID=@Route_ID";
|
||||
connection.Execute(queryUpdate, route);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,74 @@
|
||||
using FuelAndLubricants.Entities;
|
||||
using Dapper;
|
||||
using FuelAndLubricants.Entities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
|
||||
namespace FuelAndLubricants.Repositories.Implementations;
|
||||
|
||||
public class TripRepository : ITripRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<TripRepository> _logger;
|
||||
|
||||
public TripRepository(IConnectionString connectionString, ILogger<TripRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateTrip(Trip trip)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(trip));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Trip (Start_Date, End_Date, Shift, Fuel_Consumption, Car_ID, Driver_ID)
|
||||
VALUES (@Start_Date, @End_Date, @Shift, @Fuel_Consumption, @Car_ID, @Driver_ID);
|
||||
SELECT MAX(Trip_ID) FROM Trip";
|
||||
var tripId = connection.QueryFirst<int>(queryInsert, trip, transaction);
|
||||
var querySubInsert = @"
|
||||
INSERT INTO Trip_Route (Trip_ID, Route_ID)
|
||||
VALUES (@tripId, @Route_ID)";
|
||||
foreach (var elem in trip.Routes)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
tripId,
|
||||
elem.Route_ID
|
||||
}, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Trip> ReadTrips(DateTime? dateFrom = null, DateTime? dateTo = null, int? carId = null, int? driverId = null, int? routeId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Trip";
|
||||
var trips = connection.Query<Trip>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(trips));
|
||||
return trips;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
FuelAndLubricants/FuelAndLubricants/appsettings.json
Normal file
15
FuelAndLubricants/FuelAndLubricants/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/fuel_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user