From ed6b1152c629b9473848c010b58b2c89626995fe Mon Sep 17 00:00:00 2001 From: cyxaruk Date: Wed, 27 Nov 2024 12:53:02 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BA=D1=80=D0=B8=D0=B2=D0=BE,=20=D0=BF=D0=BB?= =?UTF-8?q?=D0=BE=D1=85=D0=BE,=20=D0=BD=D0=B0=D0=B4=D0=BE=20=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Forms/FormBusChecks.Designer.cs | 2 +- .../ProjectPeopleTransportation.csproj | 3 + .../Implementations/BusCheckRepository.cs | 77 ++++++++++++- .../Implementations/BusRepository.cs | 89 ++++++++++++++- .../Implementations/ConnectionString.cs | 2 +- .../Implementations/EmployeeRepository.cs | 102 +++++++++++++++++- .../Implementations/RouteListRepository.cs | 101 ++++++++++++++++- .../StartingShiftRepository.cs | 42 +++++++- 8 files changed, 405 insertions(+), 13 deletions(-) diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Forms/FormBusChecks.Designer.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Forms/FormBusChecks.Designer.cs index 9b3ee8d..7829bfb 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Forms/FormBusChecks.Designer.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Forms/FormBusChecks.Designer.cs @@ -88,7 +88,7 @@ Controls.Add(dataGridViewCheck); Controls.Add(panel1); Name = "FormBusChecks"; - Text = "Раздачи маршрутов"; + Text = "Проверки деталей автобуса"; panel1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)dataGridViewCheck).EndInit(); ResumeLayout(false); diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/ProjectPeopleTransportation.csproj b/ProjectPeopleTransportation/ProjectPeopleTransportation/ProjectPeopleTransportation.csproj index ef96adf..ebed082 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/ProjectPeopleTransportation.csproj +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/ProjectPeopleTransportation.csproj @@ -9,14 +9,17 @@ + + + diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusCheckRepository.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusCheckRepository.cs index edfb4b5..c8aa71d 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusCheckRepository.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusCheckRepository.cs @@ -1,19 +1,92 @@ -using ProjectPeopleTransportation.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPeopleTransportation.Entities; +using System.Transactions; namespace ProjectPeopleTransportation.Repositories.Implementations; public class BusCheckRepository : IBusCheckRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public BusCheckRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateBusCheck(BusCheck busCheck) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(busCheck)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + + connection.Open(); + using var transaction = connection.BeginTransaction(); + + var queryInsert = @" +INSERT INTO bus_check (date, employee_id) +VALUES (@Datetime, @EmployeeID); +SELECT MAX(Id) FROM bus_check"; + + + var busCheckId = connection.QueryFirst(queryInsert, busCheck, transaction); + + var querySubInsert = @" +INSERT INTO bus_bus_check (bus_id, count) +VALUES (@BusID, @Count)"; + foreach (var elem in busCheck.BusBusCheck) + { + connection.Execute(querySubInsert, new {busCheckId, elem.BusID, elem.Count}, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteBusCheck(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM bus_check +WHERE Id=@ID"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadBusCheck(DateTime? dateForm = null, DateTime? dateTo = null, int? employeeId = null, int? routelistId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM bus_check"; + var busChecks = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(busChecks)); + return busChecks; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusRepository.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusRepository.cs index 35bfe27..35b0f71 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusRepository.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/BusRepository.cs @@ -2,6 +2,10 @@ using ProjectPeopleTransportation.Entities.Enums; using Microsoft.Extensions.Logging; using Newtonsoft.Json; +using System.Data.SqlClient; +using Npgsql; +using Dapper; + namespace ProjectPeopleTransportation.Repositories.Implementations; @@ -21,23 +25,104 @@ public class BusRepository : IBusRepository { _logger.LogInformation("Добавление объекта"); _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(bus)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO bus (detail_name, licence_plate, bus_name) +VALUES (@BusElementType, @LicensePlate, @BusName)"; + connection.Execute(queryInsert, 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 bus +WHERE id=@ID"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Bus ReadBusByID(int id) { - return Bus.CreateEntity(0, string.Empty, string.Empty, BusElementType.None); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM bus +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() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM bus"; + var buses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buses)); + return buses; + } + 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 bus +SET + detail_name=@BusElementType, + licence_plate=@LicensePlate, + bus_name=@BusName +WHERE id=@ID"; + connection.Execute(queryUpdate, bus); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/ConnectionString.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/ConnectionString.cs index 54bea12..8c64a50 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/ConnectionString.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/ConnectionString.cs @@ -8,5 +8,5 @@ namespace ProjectPeopleTransportation.Repositories.Implementations; internal class ConnectionString : IConnectionString { - string IConnectionString.ConnectionString => ""; + string IConnectionString.ConnectionString => "Server=localhost;Port=5432;Database=lab3;User Id=postgres;Password=postgres;"; } diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/EmployeeRepository.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/EmployeeRepository.cs index 742fe45..e386364 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/EmployeeRepository.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/EmployeeRepository.cs @@ -1,29 +1,125 @@ -using ProjectPeopleTransportation.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPeopleTransportation.Entities; using ProjectPeopleTransportation.Entities.Enums; namespace ProjectPeopleTransportation.Repositories.Implementations; public 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 employee (first_name, last_name, post) +VALUES (@FirstName, @LastName, @EmployeePost)"; + connection.Execute(queryInsert, 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 employee +WHERE id=@ID"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Employee ReadEmployeeByID(int id) { - return Employee.CreateEntity(0, string.Empty, string.Empty, EmployeePost.None); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM employee +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() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM employee"; + var employees = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(employees)); + return employees; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateEmployee(Employee employee) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE employee +SET + first_name=@FirstName, + last_name=@LastName, + post=@EmployeePost +WHERE id=@ID"; + connection.Execute(queryUpdate, employee); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/RouteListRepository.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/RouteListRepository.cs index 5388d99..3ef880f 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/RouteListRepository.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/RouteListRepository.cs @@ -1,29 +1,124 @@ -using ProjectPeopleTransportation.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectPeopleTransportation.Entities; using ProjectPeopleTransportation.Entities.Enums; namespace ProjectPeopleTransportation.Repositories.Implementations; public class RouteListRepository : IRouteListRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public RouteListRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateRouteList(RouteList routeList) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(routeList)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO route_list (route_name, route_description) +VALUES (@Name, @Description)"; + connection.Execute(queryInsert, routeList); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteRouteList(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM route_list +WHERE id=@ID"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public RouteList ReadRouteListByID(int id) { - return RouteList.CreateEntity(0, string.Empty, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM route_list +WHERE [id]=@ID"; + var routeList = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(routeList)); + + return routeList; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadRouteLists() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM route_list"; + var routeLists = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(routeLists)); + return routeLists; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateRouteList(RouteList routeList) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(routeList)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE route_list +SET + route_name=@Name, + route_description=@Description +WHERE id=@ID"; + connection.Execute(queryUpdate, routeList); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/StartingShiftRepository.cs b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/StartingShiftRepository.cs index b15b98c..baf6092 100644 --- a/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/StartingShiftRepository.cs +++ b/ProjectPeopleTransportation/ProjectPeopleTransportation/Repositories/Implementations/StartingShiftRepository.cs @@ -1,16 +1,56 @@  +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; using ProjectPeopleTransportation.Entities; namespace ProjectPeopleTransportation.Repositories.Implementations; public class StartingShiftRepository : IStartingShiftRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public StartingShiftRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateStartingShift(StartingShift startingShift) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(startingShift)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO starting_shift (route_list_id, employee_id, bus_id, shift_durarion) +VALUES (@RouteListID, @EmployeeID, @BusID, @ShiftDuration)"; + connection.Execute(queryInsert, startingShift); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadStartingShifts(DateTime? dateForm = null, DateTime? dateTo = null, int? routeId = null, int? employeeId = null, int? busId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM starting_shift"; + var startingShifts = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(startingShifts)); + return startingShifts; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } }