diff --git a/ITServiceManager/ITServiceManager/ITServiceManager.csproj b/ITServiceManager/ITServiceManager/ITServiceManager.csproj
index 4ccf359..6470a09 100644
--- a/ITServiceManager/ITServiceManager/ITServiceManager.csproj
+++ b/ITServiceManager/ITServiceManager/ITServiceManager.csproj
@@ -10,7 +10,18 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/ITServiceManager/ITServiceManager/Program.cs b/ITServiceManager/ITServiceManager/Program.cs
index 81423f3..85816b3 100644
--- a/ITServiceManager/ITServiceManager/Program.cs
+++ b/ITServiceManager/ITServiceManager/Program.cs
@@ -1,5 +1,9 @@
using ITServiceManager.Repositories.Implementations;
using ITServiceManager.Repositories;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
+using Unity.Microsoft.Logging;
+using Serilog;
using Unity;
namespace ITServiceManager
@@ -22,6 +26,9 @@ namespace ITServiceManager
private static IUnityContainer CreateContainer()
{
var container = new UnityContainer();
+
+ container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
+
container.RegisterType();
container.RegisterType();
container.RegisterType();
@@ -32,5 +39,17 @@ namespace ITServiceManager
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/ITServiceManager/ITServiceManager/Repositories/Implementations/AppointmentRepository.cs b/ITServiceManager/ITServiceManager/Repositories/Implementations/AppointmentRepository.cs
index 0127623..8e895bc 100644
--- a/ITServiceManager/ITServiceManager/Repositories/Implementations/AppointmentRepository.cs
+++ b/ITServiceManager/ITServiceManager/Repositories/Implementations/AppointmentRepository.cs
@@ -1,12 +1,36 @@
-using ITServiceManager.Entities;
+using Dapper;
+using ITServiceManager.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace ITServiceManager.Repositories.Implementations;
public class AppointmentRepository : IAppointmentRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+ public AppointmentRepository(IConnectionString connectionString, ILogger logger)
+ {
+ _connectionString = connectionString;
+ _logger = logger;
+ }
public IEnumerable ReadAppointments()
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = @"SELECT * FROM Appointments";
+ var order = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(order));
+ return order;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при чтении объектов");
+ throw;
+ }
}
public Appointment ReadAppointmentById(int id)
{
diff --git a/ITServiceManager/ITServiceManager/Repositories/Implementations/CompanyRepository.cs b/ITServiceManager/ITServiceManager/Repositories/Implementations/CompanyRepository.cs
index f4df14c..a5f9f12 100644
--- a/ITServiceManager/ITServiceManager/Repositories/Implementations/CompanyRepository.cs
+++ b/ITServiceManager/ITServiceManager/Repositories/Implementations/CompanyRepository.cs
@@ -1,33 +1,116 @@
-using ITServiceManager.Entities;
+using Dapper;
+using ITServiceManager.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace ITServiceManager.Repositories.Implementations;
public class CompanyRepository : ICompanyRepository
{
private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
- public CompanyRepository(IConnectionString connectionString)
+ public CompanyRepository(IConnectionString connectionString, ILogger logger)
{
_connectionString = connectionString;
+ _logger = logger;
}
public IEnumerable ReadCompanies()
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = "SELECT * FROM Companies";
+ var companies = connection.Query(querySelect);
+ _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(companies));
+ return companies;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при чтении объектов");
+ throw;
+ }
}
public Company ReadCompanyById(int id)
{
- return null;
+ _logger.LogInformation("Получение объекта по идентификатору");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var querySelect = @"
+ SELECT * FROM Companies
+ WHERE Id=@id";
+ var company = connection.QueryFirst(querySelect, new
+ {
+ id
+ });
+ _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(company));
+ return company;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при поиске объекта");
+ throw;
+ }
}
public void CreateCompany(Company company)
{
-
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(company));
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryInsert = @"
+ INSERT INTO Companies (Name, Address)
+ VALUES (@Name, @Address)";
+ connection.Execute(queryInsert, company);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при добавлении объекта");
+ throw;
+ }
}
public void UpdateCompany(Company company)
{
-
+ _logger.LogInformation("Редактирование объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(company));
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryUpdate = @"
+ UPDATE Companies
+ SET
+ Name=@Name,
+ Address=@Address,
+ WHERE Id=@Id";
+ connection.Execute(queryUpdate, company);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при редактировании объекта");
+ throw;
+ }
}
public void DeleteCompany(int id)
{
-
+ _logger.LogInformation("Удаление объекта");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryDelete = @"
+ DELETE FROM Companies
+ WHERE Id=@id";
+ connection.Execute(queryDelete, new { id });
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
}
diff --git a/ITServiceManager/ITServiceManager/Repositories/Implementations/ConnectionString.cs b/ITServiceManager/ITServiceManager/Repositories/Implementations/ConnectionString.cs
index 04eb28a..b4140ca 100644
--- a/ITServiceManager/ITServiceManager/Repositories/Implementations/ConnectionString.cs
+++ b/ITServiceManager/ITServiceManager/Repositories/Implementations/ConnectionString.cs
@@ -2,5 +2,5 @@
internal class ConnectionString : IConnectionString
{
- string IConnectionString.ConnectionString => "";
+ string IConnectionString.ConnectionString => "Server=localhost;Port=5432;Database=itcompany;";
}
diff --git a/ITServiceManager/ITServiceManager/Repositories/Implementations/EmployeeRepository.cs b/ITServiceManager/ITServiceManager/Repositories/Implementations/EmployeeRepository.cs
index 3cc552d..13a063c 100644
--- a/ITServiceManager/ITServiceManager/Repositories/Implementations/EmployeeRepository.cs
+++ b/ITServiceManager/ITServiceManager/Repositories/Implementations/EmployeeRepository.cs
@@ -1,27 +1,118 @@
-using ITServiceManager.Entities;
+using Dapper;
+using ITServiceManager.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace ITServiceManager.Repositories.Implementations;
public class EmployeeRepository : IEmployeeRepository
{
+ private readonly IConnectionString _connectionString;
+ private readonly ILogger _logger;
+ public EmployeeRepository(IConnectionString connectionString,
+ ILogger logger)
+ {
+ _connectionString = connectionString;
+ _logger = logger;
+ }
+
public IEnumerable ReadEmployees()
{
- return [];
+ _logger.LogInformation("Получение всех объектов");
+ try
+ {
+ using var connection = new NpgsqlConnection(_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 Employee ReadEmployeeById(int id)
{
- return null;
+ _logger.LogInformation("Получение объекта по идентификатору");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_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 void CreateEmployee(Employee employee)
{
-
+ _logger.LogInformation("Добавление объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryInsert = @"
+ INSERT INTO Employees (FirstName, LastName, Position)
+ VALUES (@FirstName, @LastName, @Position)";
+ connection.Execute(queryInsert, employee);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при добавлении объекта");
+ throw;
+ }
}
public void UpdateEmployee(Employee employee)
{
-
+ _logger.LogInformation("Редактирование объекта");
+ _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(employee));
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryUpdate = @"
+ UPDATE Employees
+ SET
+ FirstName=@FirstName,
+ LastName=@LastName,
+ Position=@Position
+ WHERE Id=@Id";
+ connection.Execute(queryUpdate, employee);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при редактировании объекта");
+ throw;
+ }
}
public void DeleteEmployee(int id)
{
-
+ _logger.LogInformation("Удаление объекта");
+ _logger.LogDebug("Объект: {id}", id);
+ try
+ {
+ using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
+ var queryDelete = @"
+ DELETE FROM Employees
+ WHERE Id=@id";
+ connection.Execute(queryDelete, new { id });
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Ошибка при удалении объекта");
+ throw;
+ }
}
}
diff --git a/ITServiceManager/ITServiceManager/Repositories/Implementations/ServiceRepository.cs b/ITServiceManager/ITServiceManager/Repositories/Implementations/ServiceRepository.cs
index 296eabf..af28da3 100644
--- a/ITServiceManager/ITServiceManager/Repositories/Implementations/ServiceRepository.cs
+++ b/ITServiceManager/ITServiceManager/Repositories/Implementations/ServiceRepository.cs
@@ -1,27 +1,116 @@
-using ITServiceManager.Entities;
+using Dapper;
+using ITServiceManager.Entities;
+using Microsoft.Extensions.Logging;
+using Newtonsoft.Json;
+using Npgsql;
namespace ITServiceManager.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 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 Service ReadServiceById(int id)
{
- return null;
+ _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 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, ServiceType, Description)
+ VALUES (@Name, @ServiceType, @Description)";
+ connection.Execute(queryInsert, service);
+ }
+ 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
+ ServiceType=@ServiceType,
+ Description=@Description,
+ WHERE Id=@Id";
+ connection.Execute(queryUpdate, 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;
+ }
}
}
diff --git a/ITServiceManager/ITServiceManager/appsettings.json b/ITServiceManager/ITServiceManager/appsettings.json
index 077404a..540cd04 100644
--- a/ITServiceManager/ITServiceManager/appsettings.json
+++ b/ITServiceManager/ITServiceManager/appsettings.json
@@ -1,3 +1,15 @@
{
-
+ "Serilog": {
+ "Using": [ "Serilog.Sinks.File" ],
+ "MinimumLevel": "Debug",
+ "WriteTo": [
+ {
+ "Name": "File",
+ "Args": {
+ "path": "Logs.txt",
+ "rollingInterval": "Day"
+ }
+ }
+ ]
+ }
}
\ No newline at end of file