diff --git a/FuelAndLubricants/FuelAndLubricants/Forms/FormFuel.Designer.cs b/FuelAndLubricants/FuelAndLubricants/Forms/FormFuel.Designer.cs index 607abe8..68ec4b5 100644 --- a/FuelAndLubricants/FuelAndLubricants/Forms/FormFuel.Designer.cs +++ b/FuelAndLubricants/FuelAndLubricants/Forms/FormFuel.Designer.cs @@ -120,6 +120,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); diff --git a/FuelAndLubricants/FuelAndLubricants/Repositories/Implementations/FuelRepository.cs b/FuelAndLubricants/FuelAndLubricants/Repositories/Implementations/FuelRepository.cs index 54b793b..f045eff 100644 --- a/FuelAndLubricants/FuelAndLubricants/Repositories/Implementations/FuelRepository.cs +++ b/FuelAndLubricants/FuelAndLubricants/Repositories/Implementations/FuelRepository.cs @@ -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 _logger; + + public FuelRepository(IConnectionString connectionString, ILogger 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(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(fuel)); + return fuel; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadFuels() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Fuel_And_Lubricants"; + var fuels = connection.Query(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; + } } }