diff --git a/ProjectGarage/Entities/Driver.cs b/ProjectGarage/Entities/Driver.cs index 02ecffc..506df4b 100644 --- a/ProjectGarage/Entities/Driver.cs +++ b/ProjectGarage/Entities/Driver.cs @@ -19,14 +19,13 @@ public class Driver public int TruckId { get;private set; } - public static Driver CreateDriver(int id, string fname, string lname ,string phone_num, int tryckid) + public static Driver CreateDriver(int id, string fname, string lname, int tryckid) { return new Driver { Id = id, First_name = fname, Last_name = lname, - Phone_Number = phone_num ?? string.Empty, TruckId = tryckid }; } diff --git a/ProjectGarage/ProjectGarage.csproj b/ProjectGarage/ProjectGarage.csproj index 19ad3ed..0e8c74c 100644 --- a/ProjectGarage/ProjectGarage.csproj +++ b/ProjectGarage/ProjectGarage.csproj @@ -13,6 +13,8 @@ + + diff --git a/ProjectGarage/Repositories/Implementations/DriverRepository.cs b/ProjectGarage/Repositories/Implementations/DriverRepository.cs index f263df5..b7d753e 100644 --- a/ProjectGarage/Repositories/Implementations/DriverRepository.cs +++ b/ProjectGarage/Repositories/Implementations/DriverRepository.cs @@ -1,6 +1,11 @@ -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; @@ -11,24 +16,117 @@ public class DriverRepository : IDriverRepository { public readonly IConnectionString _connectionString; - public DriverRepository(IConnectionString connectionString) + private readonly ILogger _logger; + + public DriverRepository(IConnectionString connectionString, ILogger 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); + connection.Open(); + var queryInsert = @" +INSERT INTO driver (driver_fname, driver_lname, id_truck) VALUES +(@First_name, @Last_name, @TruckID);"; + connection.Execute(queryInsert, driver); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteDriver(int id) - { - } - - public Driver ReadDriverByID(int id) => Driver.CreateDriver(0, string.Empty, string.Empty, string.Empty, 0); - - public IEnumerable ReadDrivers() => []; - 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 +driver_fname=@First_name, +driver_lname=@Last_name, +id_truck=@TruckID +WHERE id_driver=@Id"; + connection.Execute(queryUpdate, driver); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } + + public void DeleteDriver(int driverId) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", driverId); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM driver +WHERE id_driver=@id"; + connection.Execute(queryDelete, new { driverId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + + public Driver ReadDriverByID(int driverId) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", driverId); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM route_list +WHERE id=@routeListId"; + var driver = connection.QueryFirst(querySelect, new { driverId }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(driver)); + + return driver; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadDrivers() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM driver"; + var driver = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(driver)); + return driver; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectGarage/Resources/appsettings.json b/ProjectGarage/Resources/appsettings.json index 91d6319..e5d07ff 100644 --- a/ProjectGarage/Resources/appsettings.json +++ b/ProjectGarage/Resources/appsettings.json @@ -6,7 +6,7 @@ { "Name": "File", "Args": { - "path": "C\\Users\\dimoo\\OneDrive\\Рабочий стол\\Учеба\\2 курс\\Лабы 2 курс\\ОТП\\garage.txt", + "path": "Logs/garage.txt", "rollingInterval": "Day" } } diff --git a/garage.txt b/garage.txt deleted file mode 100644 index e69de29..0000000