diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company.csproj b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company.csproj index f6eaf8a..7ab26d8 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company.csproj +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company.csproj @@ -1,16 +1,34 @@  - - WinExe - net8.0-windows - ISEbd_22_Vasin_E.D._It_Company - enable - true - enable - + + WinExe + net8.0-windows + ISEbd_22_Vasin_E.D._It_Company + enable + true + enable + - - - + + + + + + + + + + + + + + + + + + + Always + + \ No newline at end of file diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Program.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Program.cs index e4dfb74..cd6b9c4 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Program.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Program.cs @@ -1,7 +1,10 @@ -using ISEbd_22_Vasin_E.D._It_Company; using ISEbd_22_Vasin_E.D._It_Company.Repositories; using ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; using Unity; +using Unity.Microsoft.Logging; namespace ISEbd_22_Vasin_E.D._It_Company { @@ -23,6 +26,8 @@ namespace ISEbd_22_Vasin_E.D._It_Company { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); @@ -30,7 +35,20 @@ namespace ISEbd_22_Vasin_E.D._It_Company 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/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/IConnectionString.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/IConnectionString.cs new file mode 100644 index 0000000..637c4c9 --- /dev/null +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/IConnectionString.cs @@ -0,0 +1,6 @@ +namespace ISEbd_22_Vasin_E.D._It_Company.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get; } +} diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ClientRepository.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ClientRepository.cs index c2297f6..3fdd72c 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ClientRepository.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ClientRepository.cs @@ -1,28 +1,125 @@ -using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Dapper; +using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.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("Объект: {json}", JsonConvert.SerializeObject(client)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Clients (Name, LegalForm) +VALUES (@Name, @LegalForm); + "; + 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 NpgsqlConnection(_connectionString.ConnectionString); + 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, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_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 NpgsqlConnection(_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 NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Clients +SET Name = @Name, LegalForm = @LegalForm +WHERE Id = @Id; + "; + connection.Execute(queryUpdate, client); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ConnectionString.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..ad4ee61 --- /dev/null +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ConnectionString.cs @@ -0,0 +1,7 @@ +namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; + +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=dbotp"; +} + diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractRepository.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractRepository.cs index 92d46ed..5ec4d16 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractRepository.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractRepository.cs @@ -1,18 +1,82 @@ -using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Dapper; +using ISEbd_22_Vasin_E.D._It_Company.Entities; using ISEbd_22_Vasin_E.D._It_Company.Entities.Enums; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; public class ContractRepository : IContractRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ContractRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateContract(Contract contract) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contract)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + + var queryInsert = @" +INSERT INTO Contracts (ClientId, ContractorId, +Amount, StartDate, Deadline, ContractType) +VALUES (@ClientId, @ContractorId, +@Amount, @StartDate, @Deadline, @ContractType); +SELECT MAX(Id) FROM Contracts; + "; + var contractId = connection.QueryFirst(queryInsert, contract, transaction); + + var querySubInsert = @" +INSERT INTO ServiceContracts (ContractId, ServiceId) +VALUES (@ContractId, @ServiceId); + "; + foreach (var elem in contract.ServiceContracts) + { + var serviceId = elem.ServiceId; + connection.Execute(querySubInsert, new { contractId, serviceId }, transaction); + } + + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadContracts(int? clientId = null, int? contractorId = null, int? amountFrom = null, int? amountTo = null, DateTime? startDateFrom = null, DateTime? startDateTo = null, DateTime? deadlineFrom = null, DateTime? deadlineTo = null, ContractType? contractType = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Contracts + "; + var contracts = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts)); + return contracts; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorRepository.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorRepository.cs index 5afaa75..bf3ece4 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorRepository.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorRepository.cs @@ -1,28 +1,125 @@ -using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Dapper; +using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; public class ContractorRepository : IContractorRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ContractorRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateContractor(Contractor contractor) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractor)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Contractors (Name, Surname) +VALUES (@Name, @Surname); + "; + connection.Execute(queryInsert, contractor); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteContractor(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Contractors WHERE Id = @Id; + "; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Contractor ReadContractorById(int id) { - return Contractor.CreateEntity(0, string.Empty, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Contractors WHERE Id = @Id; + "; + var contractor = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(contractor)); + return contractor; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadContractors() { - return []; + _logger.LogInformation("Получение всех объектов"); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Contractors; + "; + var contractors = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contractors)); + return contractors; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateContractor(Contractor contractor) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractor)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Contractors +SET Name = @Name, Surname = @Surname +WHERE Id = @Id; + "; + connection.Execute(queryUpdate, contractor); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorVacationRepository.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorVacationRepository.cs index 87bfeda..057a1ba 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorVacationRepository.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ContractorVacationRepository.cs @@ -1,20 +1,83 @@ -using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Dapper; +using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; public class ContractorVacationRepository : IContractorVacationRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ContractorVacationRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateContractorVacation(ContractorVacation contractorVacation) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractorVacation)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO ContractorVacations (ContractorId, VacationId) +VALUES (@ContractorId, @VacationId); + "; + connection.Execute(queryInsert, contractorVacation); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public IEnumerable ReadContractorVacations(int? contractorId = null, int? vacationId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM ContractorVacations; + "; + var contractorVacations = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contractorVacations)); + return contractorVacations; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void DeleteContractorVacation(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM ContractorVacations +WHERE Id = @id + "; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } } diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ServiceRepository.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ServiceRepository.cs index d9da758..7f6f3b9 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ServiceRepository.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/ServiceRepository.cs @@ -1,28 +1,125 @@ -using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Dapper; +using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; public class ServiceRepository : IServiceRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ServiceRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateService(Service service) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Services (Name) +VALUES (@Name); + "; + connection.Execute(queryInsert, service); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteService(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Services WHERE Id = @Id; + "; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Service ReadServiceById(int id) { - return Service.CreateEntity(0, string.Empty); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Services WHERE Id = @Id; + "; + var service = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(service)); + return service; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadServices() { - return []; + _logger.LogInformation("Получение всех объектов"); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Services; + "; + var services = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(services)); + return services; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateService(Service service) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(service)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Services +SET Name = @Name +WHERE Id = @Id; + "; + connection.Execute(queryUpdate, service); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } \ No newline at end of file diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/VacationRepository.cs b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/VacationRepository.cs index 9cbfbba..88de17a 100644 --- a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/VacationRepository.cs +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/Repositories/Implementations/VacationRepository.cs @@ -1,29 +1,125 @@ -using ISEbd_22_Vasin_E.D._It_Company.Entities; -using ISEbd_22_Vasin_E.D._It_Company.Entities.Enums; +using Dapper; +using ISEbd_22_Vasin_E.D._It_Company.Entities; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; namespace ISEbd_22_Vasin_E.D._It_Company.Repositories.Implementations; public class VacationRepository : IVacationRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public VacationRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateVacation(Vacation vacation) { + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(vacation)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Vacations (StartDate, EndDate, VacationType) +VALUES (@StartDate, @EndDate, @VacationType); + "; + connection.Execute(queryInsert, vacation); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteVacation(int id) { + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM Vacations WHERE Id = @Id; + "; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Vacation ReadVacationById(int id) { - return Vacation.CreateEntity(0, DateTime.Now, DateTime.Now, VacationType.None); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Vacations WHERE Id = @Id; + "; + var vacation = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(vacation)); + return vacation; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadVacations() { - return []; + _logger.LogInformation("Получение всех объектов"); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM Vacations; + "; + var vacations = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(vacations)); + return vacations; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateVacation(Vacation vacation) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(vacation)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE Vacations +SET StartDate = @StartDate, EndDate = @EndDate, VacationType = @VacationType +WHERE Id = @Id; + "; + connection.Execute(queryUpdate, vacation); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } \ No newline at end of file diff --git a/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/appsettings.json b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/appsettings.json new file mode 100644 index 0000000..1e5517d --- /dev/null +++ b/ISEbd-22_Vasin_E.D._It_Company/ISEbd-22_Vasin_E.D._It_Company/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/it_company_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file