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=...;"; }