This commit is contained in:
Baryshev Dmitry 2024-12-17 19:58:30 +04:00
parent d243c03fc8
commit 2ef7829897
13 changed files with 124 additions and 35 deletions

View File

@ -17,14 +17,14 @@ public class Driver
public int TruckId { get;private set; }
public static Driver CreateDriver(int id, string fname, string lname, int tryckid)
public static Driver CreateDriver(int id, string fname, string lname, int truckid)
{
return new Driver
{
Id = id,
Fname = fname,
Lname = lname,
TruckId = tryckid
TruckId = truckid
};
}
}

View File

@ -10,7 +10,7 @@ public class FuelReplenishment
{
public int Id { get; private set; }
public int DriverId { get; private set; }
public DateTime Date { get; private set; }
public DateTime ReplenishmentDate { get; private set; }
public IEnumerable<FuelFuelReplenishment> FuelFuelReplenishments { get; private set;} = [];
public static FuelReplenishment CreateOpeartion(int id, int driverId, IEnumerable<FuelFuelReplenishment> fuelFuelReplenishments)
@ -19,7 +19,7 @@ public class FuelReplenishment
{
Id = id,
DriverId = driverId,
Date = DateTime.Now,
ReplenishmentDate = DateTime.Now,
FuelFuelReplenishments = fuelFuelReplenishments
};
}

View File

@ -16,6 +16,7 @@ namespace ProjectGarage.Forms
public partial class FormDriver : Form
{
private readonly IDriverRepository _driverRepository;
private int? _driverId;
public int Id
@ -32,7 +33,7 @@ namespace ProjectGarage.Forms
}
textBoxFirstName.Text = driver.Fname;
textBoxLastName.Text = driver.Lname;
//comboBoxTruckID.SelectedItem = driver.TruckId;
comboBoxTruckID.SelectedItem = driver.TruckId;
_driverId = value;
}
catch (Exception ex)
@ -81,6 +82,6 @@ namespace ProjectGarage.Forms
private void ButtonCancelDriver_Click(object sender, EventArgs e) => Close();
private Driver CreateDriver(int id) => Driver.CreateDriver(id, textBoxFirstName.Text,
textBoxLastName.Text, (int)comboBoxTruckID.SelectedIndex!);
textBoxLastName.Text, (int)comboBoxTruckID.SelectedIndex);
}
}

View File

@ -52,7 +52,7 @@
dataGridViewDrivers.RowHeadersVisible = false;
dataGridViewDrivers.RowHeadersWidth = 51;
dataGridViewDrivers.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridViewDrivers.Size = new Size(639, 367);
dataGridViewDrivers.Size = new Size(648, 367);
dataGridViewDrivers.TabIndex = 3;
//
// panelFormDriversButtons
@ -61,7 +61,7 @@
panelFormDriversButtons.Controls.Add(buttonDeleteDriver);
panelFormDriversButtons.Controls.Add(buttonAddDriver);
panelFormDriversButtons.Dock = DockStyle.Right;
panelFormDriversButtons.Location = new Point(639, 0);
panelFormDriversButtons.Location = new Point(648, 0);
panelFormDriversButtons.Name = "panelFormDriversButtons";
panelFormDriversButtons.Size = new Size(161, 367);
panelFormDriversButtons.TabIndex = 2;
@ -103,7 +103,7 @@
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 367);
ClientSize = new Size(809, 367);
Controls.Add(dataGridViewDrivers);
Controls.Add(panelFormDriversButtons);
Name = "FormDrivers";

View File

@ -105,8 +105,7 @@ namespace ProjectGarage.Forms
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
id =
Convert.ToInt32(dataGridViewDrivers.SelectedRows[0].Cells["Id"].Value);
id = Convert.ToInt32(dataGridViewDrivers.SelectedRows[0].Cells["Id"].Value);
return true;
}
}

View File

@ -18,6 +18,7 @@ namespace ProjectGarage.Forms
public partial class FormFuel : Form
{
private readonly IFuelRepository _fuelRepository;
private int? _fuelId;
public int Id
@ -39,6 +40,7 @@ namespace ProjectGarage.Forms
}
}
textBoxFuelName.Text = fuel.FuelName;
numericUpDownFuelPrice.Value = fuel.Price;
_fuelId = value;
}
catch (Exception ex)

View File

@ -25,10 +25,10 @@ namespace ProjectGarage.Forms
_replenishmentRepository = replenishmentRepository ??
throw new ArgumentNullException(nameof(replenishmentRepository));
comboBoxReplenishmentDriver.DataSource = driverRepository.ReadDrivers();
comboBoxReplenishmentDriver.DisplayMember = "First_name";
comboBoxReplenishmentDriver.DisplayMember = "Fname";
comboBoxReplenishmentDriver.ValueMember = "Id";
ColumnFuel.DataSource = fuelRepository.ReadFuels();
ColumnFuel.DisplayMember = "Name";
ColumnFuel.DisplayMember = "FuelName";
ColumnFuel.ValueMember = "Id";
}
@ -58,13 +58,13 @@ namespace ProjectGarage.Forms
var list = new List<FuelFuelReplenishment>();
foreach (DataGridViewRow row in dataGridViewReplenishment.Rows)
{
if (row.Cells["ColumnFeed"].Value == null ||
row.Cells["ColumnCount"].Value == null)
if (row.Cells["ColumnFuel"].Value == null ||
row.Cells["ColumnAmount"].Value == null)
{
continue;
}
list.Add(FuelFuelReplenishment.CreateElement(0, Convert.ToInt32(row.Cells["ColumnFeed"].Value),
Convert.ToInt32(row.Cells["ColumnCount"].Value)));
list.Add(FuelFuelReplenishment.CreateElement(0, Convert.ToInt32(row.Cells["ColumnFuel"].Value),
Convert.ToInt32(row.Cells["ColumnAmount"].Value)));
}
return list;
}

View File

@ -31,6 +31,7 @@ namespace ProjectGarage.Forms
throw new InvalidDataException(nameof(route));
}
textBoxRouteName.Text = route.RouteName;
textBoxRouteStart.Text = route.StartP;
textBoxRouteFinal.Text = route.FinalP;
numericUpDownRouteLen.Value = route.Length;

View File

@ -53,21 +53,30 @@ namespace ProjectGarage.Forms
}
private void ButtonUpdateRoute_Click(object sender, EventArgs e)
private void ButtonDeleteRoute_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{
return;
}
if (MessageBox.Show("Удалить запись?", "Удаление",
MessageBoxButtons.YesNo) != DialogResult.Yes)
{
return;
}
try
{
_container.Resolve<FormRoute>().ShowDialog();
_routeRepository.DeleteRoute(findId);
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при добавлении",
MessageBox.Show(ex.Message, "Ошибка при удалении",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonDeleteRoute_Click(object sender, EventArgs e)
private void ButtonUpdateRoute_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{

View File

@ -33,7 +33,7 @@ namespace ProjectGarage
container.RegisterType<IRouteRepository, RouteRepository>();
container.RegisterType<IDriverRepository, DriverRepository>();
container.RegisterType<ITransportationRepository, TransportationRepository>();
container.RegisterType<IReplenishmentRepository, FuelReplishmentRepository>();
container.RegisterType<IReplenishmentRepository, ReplenishmentRepository>();
container.RegisterType<IConnectionString, ConnectionString>();

View File

@ -47,17 +47,17 @@ INSERT INTO driver (Fname, Lname, TruckId) VALUES
public void UpdateDriver(Driver driver)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}",
JsonConvert.SerializeObject(driver));
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(driver));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"
UPDATE driver
SET
Fname=@Fname,
Lname=@Lname,
TruckId=@TruckId
Fname=@Fname,
Lname=@Lname,
TruckId=@TruckId
WHERE Id=@Id";
connection.Execute(queryUpdate, driver);
}

View File

@ -53,9 +53,9 @@ INSERT INTO fuel (FuelName, FuelType, Price) VALUES
var queryUpdate = @"
UPDATE fuel
SET
FuelName=@FuelName,
FuelType=@Startp,
Price=@Price
FuelName=@FuelName,
FuelType=@FuelType,
Price=@Price
WHERE Id=@Id";
connection.Execute(queryUpdate, fuel);
}

View File

@ -1,24 +1,101 @@
using ProjectGarage.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectGarage.Entities;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectGarage.Repositories.Implementations;
public class FuelReplishmentRepository : IReplenishmentRepository
public class ReplenishmentRepository : IReplenishmentRepository
{
public void CreateFuelReplenishment(FuelReplenishment fuelReplenishment)
private readonly IConnectionString _connectionString;
private readonly ILogger<ReplenishmentRepository> _logger;
public ReplenishmentRepository(IConnectionString connectionString, ILogger<ReplenishmentRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateFuelReplenishment(FuelReplenishment replenishment)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}",
JsonConvert.SerializeObject(replenishment));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
using var transaction = connection.BeginTransaction();
var queryInsert = @"
INSERT INTO fuelreplenishment (DriverId, ReplenishmentDate)
VALUES (@DriverId, @ReplenishmentDate);
SELECT MAX(Id) FROM fuelreplenishment";
var ReplenishmentId = connection.QueryFirst<int>(queryInsert, replenishment, transaction);
var querySubInsert = @"
INSERT INTO fuel_fuelreplenishment (ReplenishmentId, FuelId, Amount)
VALUES (@ReplenishmentId, @FuelId, @Amount)";
foreach (var elem in replenishment.FuelFuelReplenishments)
{
connection.Execute(querySubInsert, new
{
ReplenishmentId,
elem.FuelId,
elem.Amount
}, transaction);
}
transaction.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteFuelReplenishment(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM fuelreplenishment
WHERE Id=@id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public IEnumerable<FuelReplenishment> ReadFuelReplenishment(DateTime? dateForm = null, DateTime? dateTo = null, int? fuelId = null, int? driverId = null, int? routeId = null)
public IEnumerable<FuelReplenishment> ReadFuelReplenishment(DateTime? dateForm = null,
DateTime? dateTo = null, int? fuelId = null, int? driverId = null, int? routeId = null)
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new
NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM fuelreplenishment";
var replenishments = connection.Query<FuelReplenishment>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(replenishments));
return replenishments;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}