From e0e624f0ea424b227d8bcf2560895d3c403ebc20 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Thu, 14 Nov 2024 16:35:24 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=D0=B8=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20EmployeeRepo,=20Ro?= =?UTF-8?q?uteRepo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implementations/EmployeeRepository.cs | 108 ++++++++++++++++-- .../Implementations/RouteRepository.cs | 103 ++++++++++++++++- 2 files changed, 196 insertions(+), 15 deletions(-) diff --git a/project/ProjectTourAgency/Implementations/EmployeeRepository.cs b/project/ProjectTourAgency/Implementations/EmployeeRepository.cs index d6c71e5..629058c 100644 --- a/project/ProjectTourAgency/Implementations/EmployeeRepository.cs +++ b/project/ProjectTourAgency/Implementations/EmployeeRepository.cs @@ -1,35 +1,123 @@ -using ProjectEmployeeAgency.Repositories; +using Microsoft.Extensions.Logging; using ProjectTourAgency.Enities; using ProjectTourAgency.Repositories; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Newtonsoft.Json; +using Dapper; +using ProjectEmployeeAgency.Repositories; +using System.Data.SqlClient; namespace ProjectTourAgency.Implementations; public class EmployeeRepository : IEmployeeRepository { - public void CreateEmployee(Employee employee) + 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("Объект: {jspn}", JsonConvert.SerializeObject(Employee)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Employees (FullName, EmployeeJob) +VALUES (@FullName, @EmployeeJob)"; + 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 SqlConnection(_connectionString.ConnectionString); + connection.Open(); + 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) { - return Employee.CreateEntity(0, string.Empty,0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new SqlConnection(_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() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_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 void UpdateEmployee(Employee employee) + public void UpdateEmployee(Employee Employee) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(Employee)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Employees +SET + FullName = @FullName, + EmployeeJob = @EmployeeHob +WHERE Id = @Id"; + connection.Execute(queryUpdate, Employee); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редкатировании объекта"); + throw; + } } } diff --git a/project/ProjectTourAgency/Implementations/RouteRepository.cs b/project/ProjectTourAgency/Implementations/RouteRepository.cs index e2ee252..2ec0f35 100644 --- a/project/ProjectTourAgency/Implementations/RouteRepository.cs +++ b/project/ProjectTourAgency/Implementations/RouteRepository.cs @@ -1,37 +1,130 @@ -using ProjectRouteAgency.Repositories; +using Microsoft.Extensions.Logging; using ProjectTourAgency.Enities; using ProjectTourAgency.Repositories; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Dapper; +using System.Data.SqlClient; +using ProjectRouteAgency.Repositories; namespace ProjectTourAgency.Implementations; public class RouteRepository : IRouteRepository { - public void CreateRoute(Route route) + 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("Объект: {jspn}", JsonConvert.SerializeObject(Route)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Routes (TourId, Destination, Departure, Duration) +VALUES (@TourId, @Destination, @Departure, @Duration)"; + connection.Execute(queryInsert, Route); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteRoute(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + connection.Open(); + 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) { - return Route.CreateEntity(0,0, string.Empty,string.Empty, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new SqlConnection(_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() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_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; + } } - public void UpdateRoute(Route route) + public void UpdateRoute(Route Route) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(Route)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Routes +SET + TourId = @TourId, + Destination = @Destination, + Departure = @Departure, + Duration = @Duration +WHERE Id = @Id"; + connection.Execute(queryUpdate, Route); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редкатировании объекта"); + throw; + } } }