Compare commits

...

5 Commits

11 changed files with 542 additions and 43 deletions

View File

@ -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
{
internal class AddMoneyRepository : IAddMoneyRepository
namespace ProjectTourAgency.Implementations;
public class AddMoneyRepository : IAddMoneyRepository
{
public void CreateAddMoney(AddMoney client)
private readonly IConnectionString _connectionString;
private readonly ILogger<AddMoneyRepository> _logger;
public AddMoneyRepository(IConnectionString connectionString, ILogger<AddMoneyRepository> logger)
{
_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);
}
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 });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при ужалении объекта");
throw;
}
}
public AddMoney ReadAddMoneyById(int id)
{
return AddMoney.CreateEntity(0,0,DateTime.Now,0);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM AddMoneys
WHERE [Id] = @Id";
var AddMoney = connection.QueryFirst<AddMoney>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(AddMoney));
return AddMoney;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<AddMoney> ReadAddMoneys()
{
return [];
}
public void UpdateAddMoney(AddMoney client)
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM AddMoneys";
var AddMoneys = connection.Query<AddMoney>(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;
}
}
}

View File

@ -1,34 +1,129 @@
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 System.Data.SqlClient;
using Dapper;
namespace ProjectTourAgency.Implementations;
public class ClientRepository : IClientRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<ClientRepository> _logger;
public ClientRepository(IConnectionString connectionString, ILogger<ClientRepository> logger )
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateClient(Client client)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {jspn}", JsonConvert.SerializeObject(client));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO Clients (FullName, BirthDate, PhoneNumber, Money)
VALUES (@FullName, @BirthDate, @PhoneNumber, @Money)";
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 SqlConnection(_connectionString.ConnectionString);
connection.Open();
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, DateTime.Now, string.Empty, 0);
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM CLients
WHERE [Id] = @Id";
var client = connection.QueryFirst<Client>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(client));
return client;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Client> ReadClients()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM CLients";
var clients = connection.Query<Client>(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 SqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"
UPDATE CLients
SET
FullName = @FullName,
BirthDate = @BirthDate,
PhoneNumber = @PhoneNumber,
Money = @Money
WHERE Id = @Id";
connection.Execute(queryUpdate, client);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редкатировании объекта");
throw;
}
}
}

View File

@ -0,0 +1,13 @@
using ProjectTourAgency.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTourAgency.Implementations;
public class ConnectionString : IConnectionString
{
string IConnectionString.ConnectionString => "Server=localHost;Port=...;Database=...;";
}

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;
}
}
}

View File

@ -1,7 +1,11 @@
using ProjectTourAgency.Enities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ProjectTourAgency.Enities;
using ProjectTourAgency.Repositories;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -10,26 +14,82 @@ namespace ProjectTourAgency.Implementations;
public class TourRepository : ITourRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<TourRepository> _logger;
public TourRepository(IConnectionString connectionString, ILogger<TourRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateTour(Tour tour)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(tour));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
connection.Open();
using var transaction = connection.BeginTransaction();
var queryInsert = @"
INSERT INTO Tour(EmployeeId, RouteId, DepartureDate, ClientTours)
VALUES (@EmployeeId, @RouteId, @DepartureDate, @ClientTours)
SELECT MAX(Id) FROM Tour
";
var tourID = connection.QueryFirst<int>(queryInsert, tour, transaction);
var querySubInsert = @"
INSERT INTO ClientTour(ClientId, TourId, Cost)
VALUES (@ClientId, @TourId, @Cost)";
foreach (var elem in tour.ClientTours)
{
connection.Execute(querySubInsert, new { tourID, elem.ClientId, elem.Cost });
}
transaction.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteTour(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объекьт: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM Tour
WHERE Id = @id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Tour ReadTourById(int id)
{
return Tour.CreateEntity(0,0, 0,DateTime.Now, []);
}
public IEnumerable<Tour> ReadTours()
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Tour";
var tour = connection.Query<Tour>(querySelect);
_logger.LogDebug("Получение объектов {json}", JsonConvert.SerializeObject(tour));
return tour;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении");
throw;
}
public void UpdateTour(Tour tour)
{
}
}

View File

@ -1,8 +1,12 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ProjectEmployeeAgency.Repositories;
using ProjectRouteAgency.Repositories;
using ProjectTourAgency.Implementations;
using ProjectTourAgency.Repositories;
using Serilog;
using Unity;
using Unity.Microsoft.Logging;
namespace ProjectTourAgency
{
@ -24,6 +28,8 @@ namespace ProjectTourAgency
{
var container = new UnityContainer();
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
container.RegisterType<ITourRepository, TourRepository>();
container.RegisterType<IAddMoneyRepository, AddMoneyRepository>();
container.RegisterType<IClientRepository, ClientRepository>();
@ -31,7 +37,21 @@ namespace ProjectTourAgency
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
container.RegisterType<ITourRepository, TourRepository>();
container.RegisterType<IConnectionString, ConnectionString>();
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;
}
}
}

View File

@ -9,7 +9,18 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="4.1.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
<PackageReference Include="Unity" Version="5.11.10" />
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
</ItemGroup>
<ItemGroup>
@ -27,4 +38,10 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTourAgency.Repositories;
public interface IConnectionString
{
public string ConnectionString { get; }
}

View File

@ -11,11 +11,9 @@ public interface ITourRepository
{
IEnumerable<Tour> ReadTours();
Tour ReadTourById(int id);
void CreateTour(Tour tour);
void UpdateTour(Tour tour);
void DeleteTour(int id);
}

View File

@ -0,0 +1,15 @@
{
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/zoo_log.txt",
"rollingInterval": "Day"
}
}
]
}
}