Сделали реализацию логики для классов EmployeeRepo, RouteRepo

This commit is contained in:
Tonb73 2024-11-14 16:35:24 +04:00
parent 4102f2cb32
commit e0e624f0ea
2 changed files with 196 additions and 15 deletions

View File

@ -1,35 +1,123 @@
using ProjectEmployeeAgency.Repositories;
using Microsoft.Extensions.Logging;
using ProjectTourAgency.Enities;
using ProjectTourAgency.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Dapper;
using ProjectEmployeeAgency.Repositories;
using System.Data.SqlClient;
namespace ProjectTourAgency.Implementations;
public class EmployeeRepository : IEmployeeRepository
{
public void CreateEmployee(Employee employee)
private readonly IConnectionString _connectionString;
private readonly ILogger<EmployeeRepository> _logger;
public EmployeeRepository(IConnectionString connectionString, ILogger<EmployeeRepository> logger )
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateEmployee(Employee Employee)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {jspn}", JsonConvert.SerializeObject(Employee));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO Employees (FullName, EmployeeJob)
VALUES (@FullName, @EmployeeJob)";
connection.Execute(queryInsert, Employee);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteEmployee(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Employees
WHERE Id = @Id";
connection.Execute(queryDelete, new {id});
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при ужалении объекта");
throw;
}
}
public Employee ReadEmployeeById(int id)
{
return Employee.CreateEntity(0, string.Empty,0);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM Employees
WHERE [Id] = @Id";
var Employee = connection.QueryFirst<Employee>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(Employee));
return Employee;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Employee> ReadEmployees()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Employees";
var Employees = connection.Query<Employee>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(Employees));
return Employees;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public void UpdateEmployee(Employee employee)
public void UpdateEmployee(Employee Employee)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(Employee));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"
UPDATE Employees
SET
FullName = @FullName,
EmployeeJob = @EmployeeHob
WHERE Id = @Id";
connection.Execute(queryUpdate, Employee);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редкатировании объекта");
throw;
}
}
}

View File

@ -1,37 +1,130 @@
using ProjectRouteAgency.Repositories;
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;
using ProjectRouteAgency.Repositories;
namespace ProjectTourAgency.Implementations;
public class RouteRepository : IRouteRepository
{
public void CreateRoute(Route route)
private readonly IConnectionString _connectionString;
private readonly ILogger<RouteRepository> _logger;
public RouteRepository(IConnectionString connectionString, ILogger<RouteRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateRoute(Route Route)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {jspn}", JsonConvert.SerializeObject(Route));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO Routes (TourId, Destination, Departure, Duration)
VALUES (@TourId, @Destination, @Departure, @Duration)";
connection.Execute(queryInsert, Route);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteRoute(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
connection.Open();
var queryDelete = @"
DELETE FROM Routes
WHERE Id = @Id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при ужалении объекта");
throw;
}
}
public Route ReadRouteById(int id)
{
return Route.CreateEntity(0,0, string.Empty,string.Empty, 0);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM Routes
WHERE [Id] = @Id";
var Route = connection.QueryFirst<Route>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(Route));
return Route;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Route> ReadRoutes()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Routes";
var Routes = connection.Query<Route>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(Routes));
return Routes;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
public void UpdateRoute(Route route)
public void UpdateRoute(Route Route)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(Route));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"
UPDATE Routes
SET
TourId = @TourId,
Destination = @Destination,
Departure = @Departure,
Duration = @Duration
WHERE Id = @Id";
connection.Execute(queryUpdate, Route);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редкатировании объекта");
throw;
}
}
}