From 66c30cdb8cb86cf2ae213ebac2cf69dfecb5eef6 Mon Sep 17 00:00:00 2001 From: Tonb73 Date: Thu, 14 Nov 2024 17:23:55 +0400 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D1=83=D1=8E=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=B5=D0=B2,=20=D0=BA=D1=80=D0=BE=D0=BC=D0=B5=20?= =?UTF-8?q?=D0=BC=D0=BD=D0=BE=D0=B3=D0=B8=20=D0=BA=D0=BE=20=D0=BC=D0=BD?= =?UTF-8?q?=D0=BE=D0=B3=D0=B8=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implementations/AddMoneyRepository.cs | 116 +++++++++++++++--- .../Implementations/ConnectionString.cs | 2 +- 2 files changed, 103 insertions(+), 15 deletions(-) diff --git a/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs b/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs index 02e56e7..ebea0f0 100644 --- a/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs +++ b/project/ProjectTourAgency/Implementations/AddMoneyRepository.cs @@ -1,39 +1,127 @@ -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 Dapper; +using System.Data.SqlClient; -namespace ProjectTourAgency.Implementations +namespace ProjectTourAgency.Implementations; + +public class AddMoneyRepository : IAddMoneyRepository { - internal class AddMoneyRepository : IAddMoneyRepository - + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public AddMoneyRepository(IConnectionString connectionString, ILogger logger) { - public void CreateAddMoney(AddMoney client) + _connectionString = connectionString; + _logger = logger; + } + public void CreateAddMoney(AddMoney AddMoney) + { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {jspn}", JsonConvert.SerializeObject(AddMoney)); + try { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO AddMoneys (ClientId,Date,MoneyAmount) +VALUES (@ClientId,@Date,@MoneyAmount)"; + connection.Execute(queryInsert, AddMoney); } - - public void DeleteAddMoney(int id) + catch (Exception ex) { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } + } + + public void DeleteAddMoney(int id) + { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + connection.Open(); + var queryDelete = @" +DELETE FROM AddMoneys +WHERE Id = @Id"; + connection.Execute(queryDelete, new { id }); } - - public AddMoney ReadAddMoneyById(int id) + catch (Exception ex) { - return AddMoney.CreateEntity(0,0,DateTime.Now,0); + _logger.LogError(ex, "Ошибка при ужалении объекта"); + throw; } + } - public IEnumerable ReadAddMoneys() + public AddMoney ReadAddMoneyById(int id) + { + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try { - return []; + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM AddMoneys +WHERE [Id] = @Id"; + var AddMoney = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(AddMoney)); + return AddMoney; + } - - public void UpdateAddMoney(AddMoney client) + catch (Exception ex) { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } + public IEnumerable ReadAddMoneys() + { + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM AddMoneys"; + var AddMoneys = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(AddMoneys)); + return AddMoneys; + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } + + public void UpdateAddMoney(AddMoney AddMoney) + { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(AddMoney)); + try + { + using var connection = new SqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE AddMoneys +SET + ClientId = @ClientId, + Date = @Date, + MoneyAmount = @MoneyAmount"; + connection.Execute(queryUpdate, AddMoney); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редкатировании объекта"); + throw; } } } diff --git a/project/ProjectTourAgency/Implementations/ConnectionString.cs b/project/ProjectTourAgency/Implementations/ConnectionString.cs index 1782ea2..5a08d32 100644 --- a/project/ProjectTourAgency/Implementations/ConnectionString.cs +++ b/project/ProjectTourAgency/Implementations/ConnectionString.cs @@ -9,5 +9,5 @@ namespace ProjectTourAgency.Implementations; public class ConnectionString : IConnectionString { - string IConnectionString.ConnectionString => ""; + string IConnectionString.ConnectionString => "Server=localHost;Port=...;Database=...;"; }