diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs index aed0daa..10a71d3 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Enums/BusColors.cs @@ -3,9 +3,10 @@ [Flags] public enum BusColors { - White = 0, - Yellow = 1, - Black = 2, - Blue = 4, - Green = 8 + None = 0, + White = 1, + Yellow = 2, + Black = 4, + Blue = 8, + Green = 16 } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs index f39a68b..dcf4aac 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Entities/Itinerary.cs @@ -7,20 +7,20 @@ public class Itinerary { public int Id { get; private set; } public int BusId { get; private set; } - public IEnumerable ItineraryRoute { get; private set; } = []; + public IEnumerable ItineraryRoutes { get; private set; } = []; public int DriverId { get; private set; } public int ConductorId { get; private set; } public DateTime ItineraryDate { get; private set; } public string Description { get; private set; } = string.Empty; public static Itinerary CreateOperation(int id, int busId, int driverId, int conductorId, DateTime itineraryDate, string description, - IEnumerable itineraryRoute) + IEnumerable itineraryRoutes) { return new Itinerary { Id = id, BusId = busId, - ItineraryRoute = itineraryRoute, + ItineraryRoutes = itineraryRoutes, DriverId = driverId, ConductorId = conductorId, ItineraryDate = itineraryDate, diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBus.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBus.cs index a11287e..5070e7f 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBus.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBus.cs @@ -74,7 +74,7 @@ public partial class FormBus : Form private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private Bus CreateBus(int id) { - BusColors busColors = BusColors.White; + BusColors busColors = BusColors.None; foreach (var elem in checkedListBoxBusColors.CheckedItems) { busColors |= (BusColors)elem; diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepair.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepair.Designer.cs index 8423dd5..dd7762b 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepair.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepair.Designer.cs @@ -145,7 +145,6 @@ private Label label1; private Label label2; private Label label3; - private NumericUpDown numericUpDown1; private DateTimePicker dateTimePickerRepairDate; private TextBox textBoxDescription; private Button buttonCancel; diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs index eb71e7f..e8b8e86 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBusRepairs.Designer.cs @@ -70,6 +70,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 3; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs index a55c9e3..56bdd3d 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormBuses.Designer.cs @@ -96,6 +96,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 2; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs index 112e9c5..e13357e 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.Designer.cs @@ -96,6 +96,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 1; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs index 339e587..81b956b 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormEmployees.cs @@ -14,6 +14,7 @@ public partial class FormEmployees : Form _employeeRepository = employeeRepository ?? throw new ArgumentNullException(nameof(employeeRepository)); } + private void FormEmployees_Load(object sender, EventArgs e) { try diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs index 5d64ac1..45d6d9b 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormItineraries.Designer.cs @@ -83,6 +83,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(682, 411); dataGridViewData.TabIndex = 3; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs index 9d776cc..2d22552 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Forms/FormRoutes.Designer.cs @@ -52,6 +52,7 @@ dataGridViewData.Name = "dataGridViewData"; dataGridViewData.ReadOnly = true; dataGridViewData.RowHeadersVisible = false; + dataGridViewData.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewData.Size = new Size(681, 411); dataGridViewData.TabIndex = 0; // diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Program.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Program.cs index 7ddbc3e..d8bac2a 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Program.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Program.cs @@ -2,6 +2,10 @@ using ProjectAutoenterprise.Repositories.Implementations; using ProjectAutoenterprise.Repositories; using Unity; using Unity.Lifetime; +using Unity.Microsoft.Logging; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; namespace ProjectAutoenterprise { @@ -21,12 +25,28 @@ namespace ProjectAutoenterprise private static IUnityContainer CreateContainer() { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + + container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); + 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; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj b/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj index accbdf0..ebed082 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj +++ b/ProjectAutoenterprise/ProjectAutoenterprise/ProjectAutoenterprise.csproj @@ -9,7 +9,19 @@ + + + + + + + + + + + + @@ -27,4 +39,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/IConnectionString.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/IConnectionString.cs new file mode 100644 index 0000000..fe0a905 --- /dev/null +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/IConnectionString.cs @@ -0,0 +1,6 @@ +namespace ProjectAutoenterprise.Repositories; + +internal interface IConnectionString +{ + string ConnectionString { get; } +} \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs index b62c689..f8facd2 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepairRepository.cs @@ -1,15 +1,56 @@ -using ProjectAutoenterprise.Entities; - +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectAutoenterprise.Entities; +using System.Data.SqlClient; namespace ProjectAutoenterprise.Repositories.Implementations; internal class BusRepairRepository : IBusRepairRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public BusRepairRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateBusRepair(BusRepair busRepair) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(busRepair)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO BusRepairs (BusId, RepairDate, Description) +VALUES (@BusId, @RepairDate, @Description)"; + connection.Execute(queryInsert, busRepair); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public IEnumerable ReadBusRepairs(DateTime? dateForm = null, DateTime? dateTo = null, - int? busId = null, string? description = null) + public IEnumerable ReadBusRepairs( + DateTime? dateForm = null, DateTime? dateTo = null, int? busId = null, string? description = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM BusRepairs"; + var busRepairss = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(busRepairss)); + return busRepairss; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } -} +} \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs index 471f8cd..0198a34 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/BusRepository.cs @@ -1,23 +1,114 @@ -using ProjectAutoenterprise.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectAutoenterprise.Entities; namespace ProjectAutoenterprise.Repositories.Implementations; internal class BusRepository : IBusRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public BusRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateBus(Bus bus) { - } - public void DeleteBus(int id) - { - } - public Bus ReadBusById(int id) - { - return Bus.CreateEntity(0, string.Empty, 0, 0); - } - public IEnumerable ReadBuses() - { - return []; + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(bus)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Buses (Brand, Capacity, BusColors) +VALUES (@Brand, @Capacity, @BusColors)"; + connection.Execute(queryInsert, bus); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void UpdateBus(Bus bus) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(bus)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Buses +SET +Brand=@Brand, +Capacity=@Capacity, +BusColors=@BusColors +WHERE Id=@Id"; + connection.Execute(queryUpdate, bus); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } -} + public void DeleteBus(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Buses +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Bus ReadBusById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Buses +WHERE Id=@id"; + var bus = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(bus)); + return bus; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadBuses() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Buses"; + var buses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buses)); + return buses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } +} \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..86c8ccf --- /dev/null +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ConnectionString.cs @@ -0,0 +1,6 @@ +namespace ProjectAutoenterprise.Repositories.Implementations; + +internal class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=autoenterprise"; +} \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs index c9bc573..2907bdb 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/EmployeeRepository.cs @@ -1,36 +1,133 @@ -using ProjectAutoenterprise.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectAutoenterprise.Entities; using ProjectAutoenterprise.Entities.Enums; +using System.Data.SqlClient; namespace ProjectAutoenterprise.Repositories.Implementations; internal class EmployeeRepository : IEmployeeRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public EmployeeRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateEmployee(Employee employee) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Employees (FirstName, LastName, BirthDate, EmployeePost) +VALUES (@FirstName, @LastName, @BirthDate, @EmployeePost)"; + connection.Execute(queryInsert, employee); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - - public void DeleteEmployee(int id) - { - - } - - public Employee ReadEmployeeById(int id) - { - return Employee.CreateEntity(0, string.Empty, string.Empty, DateTime.MinValue, EmployeePost.None); - } - - public IEnumerable ReadEmployees() - { - return []; - } - - public IEnumerable ReadEmployees(EmployeePost employeePost) - { - return []; - } - public void UpdateEmployee(Employee employee) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Employees +SET +FirstName=@FirstName, +LastName=@LastName, +BirthDate=@BirthDate, +EmployeePost=@EmployeePost +WHERE Id=@Id"; + connection.Execute(queryUpdate, employee); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } + public void DeleteEmployee(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Employees +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + public Employee ReadEmployeeById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Employees +WHERE Id=@id"; + var employee = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee)); + return employee; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadEmployees() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Employees"; + var employees = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees)); + return employees; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + public IEnumerable ReadEmployees(EmployeePost employeePost) + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Employees WHERE EmployeePost='" + employeePost.ToString()+"'"; + var employees = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees)); + return employees; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs index 3686668..d90d2fe 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/ItineraryRepository.cs @@ -1,18 +1,88 @@ -using ProjectAutoenterprise.Entities; - +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectAutoenterprise.Entities; namespace ProjectAutoenterprise.Repositories.Implementations; internal class ItineraryRepository : IItineraryRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public ItineraryRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateItinerary(Itinerary itinerary) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(itinerary)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" +INSERT INTO Itineraries (BusId, DriverId, ConductorId, ItineraryDate, Description) +VALUES (@BusId, @DriverId, @ConductorId, @ItineraryDate, @Description); +SELECT MAX(Id) FROM Itineraries"; + var itineraryId = connection.QueryFirst(queryInsert, itinerary, transaction); + var querySubInsert = @" +INSERT INTO ItineraryRoutes (ItineraryId, RouteId) +VALUES (@ItineraryId, @RouteId)"; + foreach (var elem in itinerary.ItineraryRoutes) + { + connection.Execute(querySubInsert, new { itineraryId, elem.RouteId }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } + public void DeleteItinerary(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Itineraries +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - public IEnumerable ReadItinerary(DateTime? dateForm = null, DateTime? dateTo = null, - int? driverId = null, int? conductorId = null) + + public IEnumerable ReadItinerary( + DateTime? dateForm = null, DateTime? dateTo = null, int? driverId = null, int? conductorId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM Itineraries"; + var itineraries = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(itineraries)); + return itineraries; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } -} +} \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs index 2a4bb07..4db1c21 100644 --- a/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs +++ b/ProjectAutoenterprise/ProjectAutoenterprise/Repositories/Implementations/RouteRepository.cs @@ -1,23 +1,117 @@ -using ProjectAutoenterprise.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectAutoenterprise.Entities; +using System.Data.SqlClient; namespace ProjectAutoenterprise.Repositories.Implementations; internal class RouteRepository : IRouteRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public RouteRepository(IConnectionString connectionString, ILogger 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 Routes (BeginRoutePoint, EndRoutePoint) +VALUES (@BeginRoutePoint, @EndRoutePoint)"; + connection.Execute(queryInsert, route); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteRoute(int id) - { - } - public Route ReadRouteById(int id) - { - return Route.CreateEntity(0, string.Empty, string.Empty); - } - public IEnumerable ReadRoutes() - { - return []; - } + public void UpdateRoute(Route route) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(route)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Routes +SET +BeginRoutePoint=@BeginRoutePoint, +EndRoutePoint=@EndRoutePoint +WHERE Id=@Id"; + connection.Execute(queryUpdate, 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 Routes +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } + + public Route ReadRouteById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Routes +WHERE Id=@id"; + var route = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(route)); + return route; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + + public IEnumerable ReadRoutes() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Routes"; + var routes = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(routes)); + return routes; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } \ No newline at end of file diff --git a/ProjectAutoenterprise/ProjectAutoenterprise/appsettings.json b/ProjectAutoenterprise/ProjectAutoenterprise/appsettings.json new file mode 100644 index 0000000..e9d90f9 --- /dev/null +++ b/ProjectAutoenterprise/ProjectAutoenterprise/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/autoenterprise_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file