Пол лабораторной работы №2

This commit is contained in:
Anitonchik 2024-11-13 22:51:58 +04:00
parent 33659c851e
commit 38f054099a
11 changed files with 442 additions and 56 deletions

View File

@ -13,17 +13,17 @@ public class ProductMovement
public int ProductID { get; private set; }
public Movement MovementProduct { get; private set; }
public Movement MovementType { get; private set; }
public DateTime Date { get; private set; }
public static ProductMovement CreateOperation (int it, int productId, Movement movement)
public static ProductMovement CreateOperation (int it, int productId, Movement movementType)
{
return new ProductMovement
{
ID = it,
ProductID = productId,
MovementProduct = movement,
MovementType = movementType,
Date = DateTime.Now
};
}

View File

@ -1,6 +1,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using ProjectCompanyFurniture.Repositories;
using ProjectCompanyFurniture.Repositories.Implementations;
using Serilog;
using Unity;
using Unity.Microsoft.Logging;
namespace ProjectCompanyFurniture
{
@ -21,12 +25,27 @@ namespace ProjectCompanyFurniture
private static IUnityContainer CreateContainer()
{
var container = new UnityContainer();
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
container.RegisterType<IClientRepository, ClientRepository>();
container.RegisterType<IInvoiceRepository, InvoiceRepository>();
container.RegisterType<IManufacturerRepository, ManufacturerRepository>();
container.RegisterType<IProductMovementRepository, ProductMovementRepository>();
container.RegisterType<IProductRepository, ProductRepository>();
container.RegisterType<IConnectionString, ConnectionString>();
return container;
}
private static LoggerFactory CreateLoggerFactory()
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build())
.CreateLogger());
return loggerFactory;
}
}
}

View File

@ -9,8 +9,19 @@
</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.Container" Version="5.11.11" />
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
</ItemGroup>
<ItemGroup>

View File

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

View File

@ -1,7 +1,11 @@
using ProjectCompanyFurniture.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ProjectCompanyFurniture.Entities;
using ProjectCompanyFurniture.Entities.Enums;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -10,30 +14,111 @@ namespace ProjectCompanyFurniture.Repositories.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)
{
}
public void DeleteClient(int id)
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"INSERT INTO Clients (Name, ClientType, Optovik)
VALUES (@Name, @ClientType, @Optovik)";
connection.Execute(queryInsert, client);
}
public IEnumerable<Client> ReadClient()
catch (Exception ex)
{
return [];
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
public Client ReadClientById(int id)
{
return Client.CreateEntity(0, string.Empty, ClientType.None, false);
}
public IEnumerable<Client> ReadClients()
{
return [];
}
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
Name=@Name,
ClientType=@ClientType,
Optovik=@Optovik
WHERE ID=@id";
connection.Execute(queryUpdate, 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);
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)
{
_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()
{
_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;
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectCompanyFurniture.Repositories.Implementations;
public class ConnectionString : IConnectionString
{
string IConnectionString.ConnectionString => "";
}

View File

@ -1,4 +1,5 @@
using ProjectCompanyFurniture.Entities;
using Microsoft.Extensions.Logging;
using ProjectCompanyFurniture.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,6 +10,15 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
public class InvoiceRepository : IInvoiceRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<InvoiceRepository> _logger;
public InvoiceRepository(IConnectionString connectionString, ILogger<InvoiceRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateInvoice(Invoice invoice)
{
}

View File

@ -1,6 +1,10 @@
using ProjectCompanyFurniture.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ProjectCompanyFurniture.Entities;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -9,25 +13,111 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
public class ManufacturerRepository : IManufacturerRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<ClientRepository> _logger;
public ManufacturerRepository(IConnectionString connectionString, ILogger<ClientRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateManufacturer(Manufacturer manufacturer)
{
}
public void DeleteManufacturer(int id)
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(manufacturer));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"INSERT INTO Manufacturers (Name)
VALUES (@Name)";
connection.Execute(queryInsert, manufacturer);
}
public IEnumerable<Manufacturer> ReadManufacturers()
catch (Exception ex)
{
return [];
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
public Manufacturer ReadManufacturerById(int id)
{
return Manufacturer.CreateEntity(0, string.Empty);
}
public void UpdateManufacturer(Manufacturer manufacturer)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}",
JsonConvert.SerializeObject(manufacturer));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"UPDATE Manufacturers SET
Name=@Name
WHERE ID=@id";
connection.Execute(queryUpdate, manufacturer);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteManufacturer(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryDelete = @"DELETE FROM Manufacturers WHERE ID=@id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Manufacturer ReadManufacturerById(int id)
{
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Manufacturers WHERE [ID]=@id";
var manufacturer = connection.QueryFirst<Manufacturer>(querySelect, new
{
id
});
_logger.LogDebug("Найденный объект: {json}",
JsonConvert.SerializeObject(manufacturer));
return manufacturer;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Manufacturer> ReadManufacturers()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM Manufacturers";
var manufacturers = connection.Query<Manufacturer>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(manufacturers));
return manufacturers;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}

View File

@ -1,7 +1,11 @@
using ProjectCompanyFurniture.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ProjectCompanyFurniture.Entities;
using ProjectCompanyFurniture.Entities.Enums;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -10,12 +14,49 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
public class ProductMovementRepository : IProductMovementRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<ProductMovementRepository> _logger;
public ProductMovementRepository(IConnectionString connectionString, ILogger<ProductMovementRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateProductMovement(ProductMovement productMovement)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(productMovement));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"INSERT INTO ProductMovements (ProductID, MovementType, Date)
VALUES (@ProductID, @MovementType, @Date)";
connection.Execute(queryInsert, productMovement);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public IEnumerable<ProductMovement> ReadProductMovements(DateTime? dateForm = null, DateTime? dateTo = null, int? clientId = null)
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM ProductMovements";
var productMovements = connection.Query<ProductMovement>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(productMovements));
return productMovements;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}

View File

@ -1,6 +1,10 @@
using ProjectCompanyFurniture.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ProjectCompanyFurniture.Entities;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -9,25 +13,112 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
public class ProductRepository : IProductRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<ProductRepository> _logger;
public ProductRepository(IConnectionString connectionString, ILogger<ProductRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateProduct(Product product)
{
}
public void DeleteProduct(int id)
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(product));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryInsert = @"INSERT INTO Products (ManufacturerID, Name, Category, StartingPrice)
VALUES (@ManufacturerID, @Name, @Category, @StartingPrice)";
connection.Execute(queryInsert, product);
}
public Product ReadProductById(int id)
catch (Exception ex)
{
return Product.CreateEntity(0, 0, string.Empty, string.Empty, 0);
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
public IEnumerable<Product> ReadProducts()
{
return [];
}
public void UpdateProduct(Product product)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}",
JsonConvert.SerializeObject(product));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"UPDATE Products SET
ManufacturerID=@ManufacturerID,
Name=@Name,
Category=@Category,
StartingPrice=@StartingPrice
WHERE ID=@id";
connection.Execute(queryUpdate, product);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteProduct(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryDelete = @"DELETE FROM Productss WHERE ID=@id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Product ReadProductById(int id)
{
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Products WHERE [ID]=@id";
var product = connection.QueryFirst<Product>(querySelect, new
{
id
});
_logger.LogDebug("Найденный объект: {json}",
JsonConvert.SerializeObject(product));
return product;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Product> ReadProducts()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM Products";
var products = connection.Query<Product>(querySelect);
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(products));
return products;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}

View File

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