From 4102f2cb3204940d92abbc6d6c66b89c12a35db0 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Thu, 14 Nov 2024 16:13:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=D0=BA=D0=B8,=20=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B8=D0=BB=D0=B8=20=D0=BB=D0=BE=D0=B3=D0=B5=D0=B5=D1=80,=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D1=83=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20CLientRepository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implementations/ClientRepository.cs | 101 +++++++++++++++++- .../Implementations/ConnectionString.cs | 13 +++ project/ProjectTourAgency/Program.cs | 20 ++++ .../ProjectTourAgency.csproj | 17 +++ .../Repositories/IConnectionString.cs | 12 +++ project/ProjectTourAgency/appsettings.json | 15 +++ 6 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 project/ProjectTourAgency/Implementations/ConnectionString.cs create mode 100644 project/ProjectTourAgency/Repositories/IConnectionString.cs create mode 100644 project/ProjectTourAgency/appsettings.json diff --git a/project/ProjectTourAgency/Implementations/ClientRepository.cs b/project/ProjectTourAgency/Implementations/ClientRepository.cs index 9ec7f12..ebfadce 100644 --- a/project/ProjectTourAgency/Implementations/ClientRepository.cs +++ b/project/ProjectTourAgency/Implementations/ClientRepository.cs @@ -1,34 +1,129 @@ -using ProjectTourAgency.Enities; +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 System.Data.SqlClient; +using Dapper; namespace ProjectTourAgency.Implementations; public class ClientRepository : IClientRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public ClientRepository(IConnectionString connectionString, ILogger logger ) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateClient(Client client) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {jspn}", JsonConvert.SerializeObject(client)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Clients (FullName, BirthDate, PhoneNumber, Money) +VALUES (@FullName, @BirthDate, @PhoneNumber, @Money)"; + connection.Execute(queryInsert, client); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteClient(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryDelete = @" +DELETE FROM CLients +WHERE Id = @Id"; + connection.Execute(queryDelete, new {id}); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при ужалении объекта"); + throw; + } } public Client ReadClientById(int id) { - return Client.CreateEntity(0, string.Empty, DateTime.Now, string.Empty, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM CLients +WHERE [Id] = @Id"; + var client = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(client)); + return client; + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadClients() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM CLients"; + var clients = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(clients)); + return clients; + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateClient(Client client) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE CLients +SET + FullName = @FullName, + BirthDate = @BirthDate, + PhoneNumber = @PhoneNumber, + Money = @Money +WHERE Id = @Id"; + connection.Execute(queryUpdate, client); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редкатировании объекта"); + throw; + } } } diff --git a/project/ProjectTourAgency/Implementations/ConnectionString.cs b/project/ProjectTourAgency/Implementations/ConnectionString.cs new file mode 100644 index 0000000..1782ea2 --- /dev/null +++ b/project/ProjectTourAgency/Implementations/ConnectionString.cs @@ -0,0 +1,13 @@ +using ProjectTourAgency.Repositories; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTourAgency.Implementations; + +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => ""; +} diff --git a/project/ProjectTourAgency/Program.cs b/project/ProjectTourAgency/Program.cs index 676f643..4244420 100644 --- a/project/ProjectTourAgency/Program.cs +++ b/project/ProjectTourAgency/Program.cs @@ -1,8 +1,12 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using ProjectEmployeeAgency.Repositories; using ProjectRouteAgency.Repositories; using ProjectTourAgency.Implementations; using ProjectTourAgency.Repositories; +using Serilog; using Unity; +using Unity.Microsoft.Logging; namespace ProjectTourAgency { @@ -24,6 +28,8 @@ namespace ProjectTourAgency { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); @@ -31,7 +37,21 @@ namespace ProjectTourAgency container.RegisterType(); container.RegisterType(); + container.RegisterType(); + 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/project/ProjectTourAgency/ProjectTourAgency.csproj b/project/ProjectTourAgency/ProjectTourAgency.csproj index accbdf0..f7fd4ad 100644 --- a/project/ProjectTourAgency/ProjectTourAgency.csproj +++ b/project/ProjectTourAgency/ProjectTourAgency.csproj @@ -9,7 +9,18 @@ + + + + + + + + + + + @@ -27,4 +38,10 @@ + + + Always + + + \ No newline at end of file diff --git a/project/ProjectTourAgency/Repositories/IConnectionString.cs b/project/ProjectTourAgency/Repositories/IConnectionString.cs new file mode 100644 index 0000000..6489d8b --- /dev/null +++ b/project/ProjectTourAgency/Repositories/IConnectionString.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTourAgency.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get; } +} diff --git a/project/ProjectTourAgency/appsettings.json b/project/ProjectTourAgency/appsettings.json new file mode 100644 index 0000000..1fa134b --- /dev/null +++ b/project/ProjectTourAgency/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/zoo_log.txt", + "rollingInterval": "Day" + } + } + ] + } +}