diff --git a/ProjectGarage/Entities/Driver.cs b/ProjectGarage/Entities/Driver.cs index c8d2711..f5a5131 100644 --- a/ProjectGarage/Entities/Driver.cs +++ b/ProjectGarage/Entities/Driver.cs @@ -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 }; } } diff --git a/ProjectGarage/Entities/FuelReplenishment.cs b/ProjectGarage/Entities/FuelReplenishment.cs index ed5fdca..fbc4b34 100644 --- a/ProjectGarage/Entities/FuelReplenishment.cs +++ b/ProjectGarage/Entities/FuelReplenishment.cs @@ -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 FuelFuelReplenishments { get; private set;} = []; public static FuelReplenishment CreateOpeartion(int id, int driverId, IEnumerable fuelFuelReplenishments) @@ -19,7 +19,7 @@ public class FuelReplenishment { Id = id, DriverId = driverId, - Date = DateTime.Now, + ReplenishmentDate = DateTime.Now, FuelFuelReplenishments = fuelFuelReplenishments }; } diff --git a/ProjectGarage/Forms/FormDriver.cs b/ProjectGarage/Forms/FormDriver.cs index 6d5a6f8..75dae1f 100644 --- a/ProjectGarage/Forms/FormDriver.cs +++ b/ProjectGarage/Forms/FormDriver.cs @@ -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); } } diff --git a/ProjectGarage/Forms/FormDrivers.Designer.cs b/ProjectGarage/Forms/FormDrivers.Designer.cs index 6bc5f62..362351c 100644 --- a/ProjectGarage/Forms/FormDrivers.Designer.cs +++ b/ProjectGarage/Forms/FormDrivers.Designer.cs @@ -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"; diff --git a/ProjectGarage/Forms/FormDrivers.cs b/ProjectGarage/Forms/FormDrivers.cs index cf1853d..e5fdf57 100644 --- a/ProjectGarage/Forms/FormDrivers.cs +++ b/ProjectGarage/Forms/FormDrivers.cs @@ -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; } } diff --git a/ProjectGarage/Forms/FormFuel.cs b/ProjectGarage/Forms/FormFuel.cs index 519935b..2442df5 100644 --- a/ProjectGarage/Forms/FormFuel.cs +++ b/ProjectGarage/Forms/FormFuel.cs @@ -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) diff --git a/ProjectGarage/Forms/FormReplenishment.cs b/ProjectGarage/Forms/FormReplenishment.cs index 8e0ba2a..f928f0a 100644 --- a/ProjectGarage/Forms/FormReplenishment.cs +++ b/ProjectGarage/Forms/FormReplenishment.cs @@ -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(); 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; } diff --git a/ProjectGarage/Forms/FormRoute.cs b/ProjectGarage/Forms/FormRoute.cs index a3207c8..52ea502 100644 --- a/ProjectGarage/Forms/FormRoute.cs +++ b/ProjectGarage/Forms/FormRoute.cs @@ -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; diff --git a/ProjectGarage/Forms/FormRoutes.cs b/ProjectGarage/Forms/FormRoutes.cs index fb519e1..7f7a62a 100644 --- a/ProjectGarage/Forms/FormRoutes.cs +++ b/ProjectGarage/Forms/FormRoutes.cs @@ -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().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)) { diff --git a/ProjectGarage/Program.cs b/ProjectGarage/Program.cs index b6f110f..7d32e28 100644 --- a/ProjectGarage/Program.cs +++ b/ProjectGarage/Program.cs @@ -33,7 +33,7 @@ namespace ProjectGarage container.RegisterType(); container.RegisterType(); container.RegisterType(); - container.RegisterType(); + container.RegisterType(); container.RegisterType(); diff --git a/ProjectGarage/Repositories/Implementations/DriverRepository.cs b/ProjectGarage/Repositories/Implementations/DriverRepository.cs index 034c2c4..ac99747 100644 --- a/ProjectGarage/Repositories/Implementations/DriverRepository.cs +++ b/ProjectGarage/Repositories/Implementations/DriverRepository.cs @@ -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); } diff --git a/ProjectGarage/Repositories/Implementations/FuelRepository.cs b/ProjectGarage/Repositories/Implementations/FuelRepository.cs index bdb6ae1..5dedbee 100644 --- a/ProjectGarage/Repositories/Implementations/FuelRepository.cs +++ b/ProjectGarage/Repositories/Implementations/FuelRepository.cs @@ -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); } diff --git a/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs b/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs index 09a74d5..6bd66ab 100644 --- a/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs +++ b/ProjectGarage/Repositories/Implementations/ReplenishmentRepository.cs @@ -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 _logger; + + public ReplenishmentRepository(IConnectionString connectionString, ILogger 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(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 ReadFuelReplenishment(DateTime? dateForm = null, DateTime? dateTo = null, int? fuelId = null, int? driverId = null, int? routeId = null) +public IEnumerable 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(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(replenishments)); + return replenishments; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } }