ЛАаба 2
This commit is contained in:
parent
a2acfd3fb1
commit
82789985ca
@ -14,10 +14,10 @@ public enum MaterialsName
|
|||||||
|
|
||||||
Wallpaper = 2, // обои
|
Wallpaper = 2, // обои
|
||||||
|
|
||||||
Paints = 3, // краски
|
Paints = 4, // краски
|
||||||
|
|
||||||
Furniture = 4, // мебель
|
Furniture = 8, // мебель
|
||||||
|
|
||||||
Windows = 5, // окна
|
Windows = 16, // окна
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ namespace ProjectRepairWork
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_container.Resolve<FormCost>().ShowDialog();
|
_container.Resolve<FormCosts>().ShowDialog();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -18,12 +18,12 @@ namespace ProjectRepairWork.Forms
|
|||||||
{
|
{
|
||||||
private readonly IUnityContainer _container;
|
private readonly IUnityContainer _container;
|
||||||
private readonly IContractorsRepository _contractorsRepository;
|
private readonly IContractorsRepository _contractorsRepository;
|
||||||
public FormContractors(IUnityContainer container, IContractorsRepository premisesRepository)
|
public FormContractors(IUnityContainer container, IContractorsRepository contractorsRepository)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
_container = container ?? throw new ArgumentNullException(nameof(container));
|
_container = container ?? throw new ArgumentNullException(nameof(container));
|
||||||
_contractorsRepository = premisesRepository ?? throw new ArgumentNullException(nameof(premisesRepository));
|
_contractorsRepository = contractorsRepository ?? throw new ArgumentNullException(nameof(contractorsRepository));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FormContractors_Load(object sender, EventArgs e)
|
private void FormContractors_Load(object sender, EventArgs e)
|
||||||
@ -59,7 +59,7 @@ namespace ProjectRepairWork.Forms
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var form = _container.Resolve<FormPremise>();
|
var form = _container.Resolve<FormContractor>();
|
||||||
form.Id = findId;
|
form.Id = findId;
|
||||||
form.ShowDialog();
|
form.ShowDialog();
|
||||||
LoadList();
|
LoadList();
|
||||||
|
1
ProjectRepairWork/Forms/FormCost.Designer.cs
generated
1
ProjectRepairWork/Forms/FormCost.Designer.cs
generated
@ -58,6 +58,7 @@
|
|||||||
// numericUpDownPrice
|
// numericUpDownPrice
|
||||||
//
|
//
|
||||||
numericUpDownPrice.Location = new Point(180, 97);
|
numericUpDownPrice.Location = new Point(180, 97);
|
||||||
|
numericUpDownPrice.Maximum = new decimal(new int[] { 100000, 0, 0, 0 });
|
||||||
numericUpDownPrice.Name = "numericUpDownPrice";
|
numericUpDownPrice.Name = "numericUpDownPrice";
|
||||||
numericUpDownPrice.Size = new Size(200, 27);
|
numericUpDownPrice.Size = new Size(200, 27);
|
||||||
numericUpDownPrice.TabIndex = 14;
|
numericUpDownPrice.TabIndex = 14;
|
||||||
|
@ -18,12 +18,12 @@ namespace ProjectRepairWork.Forms
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var premises = _costRepository.ReadCostById(value);
|
var cost = _costRepository.ReadCostById(value);
|
||||||
if (premises == null)
|
if (cost == null)
|
||||||
{
|
{
|
||||||
throw new InvalidDataException(nameof(premises));
|
throw new InvalidDataException(nameof(cost));
|
||||||
}
|
}
|
||||||
numericUpDownPrice.Value = (decimal)premises.Price;
|
numericUpDownPrice.Value = (decimal)cost.Price;
|
||||||
_costId = value;
|
_costId = value;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@ -47,7 +47,7 @@ namespace ProjectRepairWork.Forms
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var form = _container.Resolve<FormPremise>();
|
var form = _container.Resolve<FormCost>();
|
||||||
form.Id = findId;
|
form.Id = findId;
|
||||||
form.ShowDialog();
|
form.ShowDialog();
|
||||||
LoadList();
|
LoadList();
|
||||||
|
@ -25,8 +25,8 @@ namespace ProjectRepairWork.Forms
|
|||||||
textBoxClientName.Text = premises.ClientName;
|
textBoxClientName.Text = premises.ClientName;
|
||||||
textBoxAddress.Text = premises.Address;
|
textBoxAddress.Text = premises.Address;
|
||||||
numericUpDownArea.Value = (decimal)premises.Area;
|
numericUpDownArea.Value = (decimal)premises.Area;
|
||||||
dateTimePickerStart.Value = DateTime.Now;
|
dateTimePickerStart.Value = premises.DateStart;
|
||||||
dateTimePickerEnd.Value = DateTime.Now;
|
dateTimePickerEnd.Value = premises.DateEnd;
|
||||||
comboBoxPremisesType.SelectedItem = premises.PremisesType;
|
comboBoxPremisesType.SelectedItem = premises.PremisesType;
|
||||||
_premisesId = value;
|
_premisesId = value;
|
||||||
}
|
}
|
||||||
@ -81,7 +81,7 @@ namespace ProjectRepairWork.Forms
|
|||||||
textBoxClientName.Text,
|
textBoxClientName.Text,
|
||||||
textBoxAddress.Text,
|
textBoxAddress.Text,
|
||||||
Convert.ToInt32(numericUpDownArea.Value),
|
Convert.ToInt32(numericUpDownArea.Value),
|
||||||
dateTimePickerStart.Value, dateTimePickerStart.Value, (PremisesType)comboBoxPremisesType.SelectedItem!);
|
dateTimePickerStart.Value, dateTimePickerEnd.Value, (PremisesType)comboBoxPremisesType.SelectedItem!);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ namespace ProjectRepairWork.Forms
|
|||||||
{
|
{
|
||||||
private readonly IRepairRepository _repairRepository;
|
private readonly IRepairRepository _repairRepository;
|
||||||
|
|
||||||
public FormRepair(IRepairRepository repairRepository, IContractorsRepository contractorsRepository, ICostRepository costRepository, IPremisesRepository premisesRepository, IWorksRepository worksRepository)
|
public FormRepair(IRepairRepository repairRepository, IContractorsRepository contractorsRepository, IPremisesRepository premisesRepository, IWorksRepository worksRepository)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_repairRepository = repairRepository ?? throw new ArgumentNullException(nameof(repairRepository));
|
_repairRepository = repairRepository ?? throw new ArgumentNullException(nameof(repairRepository));
|
||||||
@ -22,11 +22,11 @@ namespace ProjectRepairWork.Forms
|
|||||||
comboBoxContractors.ValueMember = "Id";
|
comboBoxContractors.ValueMember = "Id";
|
||||||
|
|
||||||
comboBoxPremises.DataSource = premisesRepository.ReadPremises();
|
comboBoxPremises.DataSource = premisesRepository.ReadPremises();
|
||||||
comboBoxPremises.DisplayMember = "Name";
|
comboBoxPremises.DisplayMember = "ClientName";
|
||||||
comboBoxPremises.ValueMember = "Id";
|
comboBoxPremises.ValueMember = "Id";
|
||||||
|
|
||||||
ColumnWork.DataSource = worksRepository.ReadWorks();
|
ColumnWork.DataSource = worksRepository.ReadWorks();
|
||||||
ColumnWork.DisplayMember = "Name";
|
ColumnWork.DisplayMember = "WorkName";
|
||||||
ColumnWork.ValueMember = "Id";
|
ColumnWork.ValueMember = "Id";
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ namespace ProjectRepairWork.Forms
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var form = _container.Resolve<FormPremise>();
|
var form = _container.Resolve<FormWork>();
|
||||||
form.Id = findId;
|
form.Id = findId;
|
||||||
form.ShowDialog();
|
form.ShowDialog();
|
||||||
LoadList();
|
LoadList();
|
||||||
|
@ -2,6 +2,10 @@ using Unity;
|
|||||||
using ProjectRepairWork.Repositories;
|
using ProjectRepairWork.Repositories;
|
||||||
using ProjectRepairWork.Repositories.Implementation;
|
using ProjectRepairWork.Repositories.Implementation;
|
||||||
using Unity.Lifetime;
|
using Unity.Lifetime;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
using Unity.Microsoft.Logging;
|
||||||
namespace ProjectRepairWork
|
namespace ProjectRepairWork
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
@ -19,14 +23,31 @@ namespace ProjectRepairWork
|
|||||||
}
|
}
|
||||||
private static IUnityContainer CreateContainer()
|
private static IUnityContainer CreateContainer()
|
||||||
{
|
{
|
||||||
var container = new UnityContainer();
|
var container = new UnityContainer()
|
||||||
container.RegisterType<ICostRepository, CostRepository>(new TransientLifetimeManager());
|
.AddExtension(new Diagnostic());
|
||||||
container.RegisterType<IContractorsRepository, ContractorsRepository>(new TransientLifetimeManager());
|
|
||||||
container.RegisterType<IPremisesRepository, PremisesRepository>(new TransientLifetimeManager());
|
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||||
container.RegisterType<IRepairRepository, RepairRepository>(new TransientLifetimeManager());
|
|
||||||
container.RegisterType<IWorksRepository, WorksRepository>(new TransientLifetimeManager());
|
container.RegisterType<ICostRepository, CostRepository>();
|
||||||
|
container.RegisterType<IContractorsRepository, ContractorsRepository>();
|
||||||
|
container.RegisterType<IPremisesRepository, PremisesRepository>();
|
||||||
|
container.RegisterType<IRepairRepository, RepairRepository>();
|
||||||
|
container.RegisterType<IWorksRepository, WorksRepository>();
|
||||||
|
container.RegisterType<IConnectionString, ConnectionString>();
|
||||||
return container;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,7 +9,19 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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="Npgsql" Version="9.0.1" />
|
||||||
|
<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" Version="5.11.10" />
|
||||||
|
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -27,4 +39,10 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="appsettings.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
12
ProjectRepairWork/Repositories/IConnectionString.cs
Normal file
12
ProjectRepairWork/Repositories/IConnectionString.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectRepairWork.Repositories;
|
||||||
|
|
||||||
|
public interface IConnectionString
|
||||||
|
{
|
||||||
|
public string ConnectionString { get; }
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ProjectRepairWork.Repositories.Implementation;
|
||||||
|
|
||||||
|
public class ConnectionString : IConnectionString
|
||||||
|
{
|
||||||
|
string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=12345;Database=OTP;";
|
||||||
|
}
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectRepairWork.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectRepairWork.Entities;
|
||||||
using ProjectRepairWork.Entities.Enums;
|
using ProjectRepairWork.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,28 +14,115 @@ namespace ProjectRepairWork.Repositories.Implementation;
|
|||||||
|
|
||||||
public class ContractorsRepository : IContractorsRepository
|
public class ContractorsRepository : IContractorsRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<ContractorsRepository> _logger;
|
||||||
|
public ContractorsRepository(IConnectionString connectionString, ILogger<ContractorsRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreateContractors(Contractors contractors)
|
public void CreateContractors(Contractors contractors)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(contractors));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Contractors (companyName, Contacts)
|
||||||
|
VALUES (@CompanyName, @Contacts)";
|
||||||
|
connection.Execute(queryInsert, contractors);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void UpdateContractors(Contractors contractors)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(contractors));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Contractors
|
||||||
|
SET
|
||||||
|
CompanyName=@CompanyName,
|
||||||
|
Contacts=@Contacts
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, contractors);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeletedContractors(int id)
|
public void DeletedContractors(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 IEnumerable<Contractors> ReadContractors()
|
public IEnumerable<Contractors> ReadContractors()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Contractors";
|
||||||
|
var contractors = connection.Query<Contractors>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(contractors));
|
||||||
|
return contractors;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Contractors ReadContractorsById(int id)
|
public Contractors ReadContractorsById(int id)
|
||||||
{
|
{
|
||||||
return Contractors.CreatEntity(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 contractors = connection.QueryFirst<Contractors>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(contractors));
|
||||||
|
return contractors;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateContractors(Contractors contractors)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectRepairWork.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectRepairWork.Entities;
|
||||||
using ProjectRepairWork.Entities.Enums;
|
using ProjectRepairWork.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,28 +14,115 @@ namespace ProjectRepairWork.Repositories.Implementation;
|
|||||||
|
|
||||||
public class CostRepository : ICostRepository
|
public class CostRepository : ICostRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<CostRepository> _logger;
|
||||||
|
|
||||||
|
public CostRepository(IConnectionString connectionString, ILogger<CostRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreateCost(Cost cost)
|
public void CreateCost(Cost cost)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(cost));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Cost (Price)
|
||||||
|
VALUES (@Price)";
|
||||||
|
connection.Execute(queryInsert, cost);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeletedCost(int id)
|
public void DeletedCost(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Cost
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Cost> ReadCost()
|
public IEnumerable<Cost> ReadCost()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Cost";
|
||||||
|
var cost = connection.Query<Cost>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(cost));
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cost ReadCostById(int id)
|
public Cost ReadCostById(int id)
|
||||||
{
|
{
|
||||||
return Cost.CreatEntity(0, 0);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Cost
|
||||||
|
WHERE Id=@id";
|
||||||
|
var cost = connection.QueryFirst<Cost>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(cost));
|
||||||
|
return cost;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateCost(Cost cost)
|
public void UpdateCost(Cost cost)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(cost));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Cost
|
||||||
|
SET
|
||||||
|
Price=@Price
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, cost);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectRepairWork.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectRepairWork.Entities;
|
||||||
using ProjectRepairWork.Entities.Enums;
|
using ProjectRepairWork.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -9,27 +13,120 @@ using System.Threading.Tasks;
|
|||||||
namespace ProjectRepairWork.Repositories.Implementation;
|
namespace ProjectRepairWork.Repositories.Implementation;
|
||||||
public class PremisesRepository : IPremisesRepository
|
public class PremisesRepository : IPremisesRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<PremisesRepository> _logger;
|
||||||
|
|
||||||
|
public PremisesRepository(IConnectionString connectionString, ILogger<PremisesRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CreatePremises(Premises premises)
|
public void CreatePremises(Premises premises)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(premises));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Premises (ClientName, Address, Area, DateStart, DateEnd, PremisesType)
|
||||||
|
VALUES (@ClientName,@Address,@Area,@DateStart,@DateEnd,@PremisesType)";
|
||||||
|
connection.Execute(queryInsert, premises);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeletePremises(int id)
|
public void DeletePremises(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Premises
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Premises> ReadPremises()
|
public IEnumerable<Premises> ReadPremises()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Premises";
|
||||||
|
var premises = connection.Query<Premises>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(premises));
|
||||||
|
return premises;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Premises ReadPremisesById(int id)
|
public Premises ReadPremisesById(int id)
|
||||||
{
|
{
|
||||||
return Premises.CreatEntity(0, string.Empty, string.Empty, 0, DateTime.Now, DateTime.Now, PremisesType.None);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Premises
|
||||||
|
WHERE Id=@id";
|
||||||
|
var premises = connection.QueryFirst<Premises>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(premises));
|
||||||
|
return premises;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdatePremises(Premises premises)
|
public void UpdatePremises(Premises premises)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(premises));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Premises
|
||||||
|
SET
|
||||||
|
ClientName = @ClientName,
|
||||||
|
Address = @Address,
|
||||||
|
Area = @Area,
|
||||||
|
DateStart = @DateStart,
|
||||||
|
DateEnd = @DateEnd,
|
||||||
|
PremisesType = @PremisesType
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, premises);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectRepairWork.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectRepairWork.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,13 +13,68 @@ namespace ProjectRepairWork.Repositories.Implementation;
|
|||||||
|
|
||||||
public class RepairRepository : IRepairRepository
|
public class RepairRepository : IRepairRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<RepairRepository> _logger;
|
||||||
|
|
||||||
|
public RepairRepository(IConnectionString connectionString, ILogger<RepairRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
public void CraeteRepair(Repair repair)
|
public void CraeteRepair(Repair repair)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(repair));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new
|
||||||
|
NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
connection.Open();
|
||||||
|
using var transaction = connection.BeginTransaction();
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Repair (ContractorsId, PremisesId)
|
||||||
|
VALUES (@ContractorsId, @PremisesId);
|
||||||
|
SELECT MAX(Id) FROM Repair";
|
||||||
|
var repairId =
|
||||||
|
connection.QueryFirst<int>(queryInsert, repair, transaction);
|
||||||
|
var querySubInsert = @"
|
||||||
|
INSERT INTO RepairRepair (RepairId, WorkId, Count)
|
||||||
|
VALUES (@RepairId, @WorkId, @Count)";
|
||||||
|
foreach (var elem in repair.RepairRepair)
|
||||||
|
{
|
||||||
|
connection.Execute(querySubInsert, new
|
||||||
|
{
|
||||||
|
repairId,
|
||||||
|
elem.WorkId,
|
||||||
|
elem.Count
|
||||||
|
}, transaction);
|
||||||
|
}
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Repair> ReadRepair(int? contractorsId = null, int? costId = null, int? worksId = null, int? premisesId = null)
|
public IEnumerable<Repair> ReadRepair(int? contractorsId = null, int? costId = null, int? worksId = null, int? premisesId = null)
|
||||||
{
|
{
|
||||||
return[];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Repair";
|
||||||
|
var repair = connection.Query<Repair>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(repair));
|
||||||
|
return repair;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using ProjectRepairWork.Entities;
|
using Dapper;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Npgsql;
|
||||||
|
using ProjectRepairWork.Entities;
|
||||||
using ProjectRepairWork.Entities.Enums;
|
using ProjectRepairWork.Entities.Enums;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -10,28 +14,115 @@ namespace ProjectRepairWork.Repositories.Implementation;
|
|||||||
|
|
||||||
public class WorksRepository : IWorksRepository
|
public class WorksRepository : IWorksRepository
|
||||||
{
|
{
|
||||||
|
private readonly IConnectionString _connectionString;
|
||||||
|
private readonly ILogger<WorksRepository> _logger;
|
||||||
|
public WorksRepository(IConnectionString connectionString, ILogger<WorksRepository> logger)
|
||||||
|
{
|
||||||
|
_connectionString = connectionString;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateWorks(Works works)
|
public void CreateWorks(Works works)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Добавление объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(works));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryInsert = @"
|
||||||
|
INSERT INTO Works (WorkName, MaterialsName)
|
||||||
|
VALUES (@WorkName, @MaterialsName)";
|
||||||
|
connection.Execute(queryInsert, works);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteWorks(int id)
|
public void DeleteWorks(int id)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Удаление объекта");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryDelete = @"
|
||||||
|
DELETE FROM Works
|
||||||
|
WHERE Id=@id";
|
||||||
|
connection.Execute(queryDelete, new { id });
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<Works> ReadWorks()
|
public IEnumerable<Works> ReadWorks()
|
||||||
{
|
{
|
||||||
return [];
|
_logger.LogInformation("Получение всех объектов");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = "SELECT * FROM Works";
|
||||||
|
var works = connection.Query<Works>(querySelect);
|
||||||
|
_logger.LogDebug("Полученные объекты: {json}",
|
||||||
|
JsonConvert.SerializeObject(works));
|
||||||
|
return works;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Works ReadWorksById(int id)
|
public Works ReadWorksById(int id)
|
||||||
{
|
{
|
||||||
return Works.CreatEntity(0,string.Empty, MaterialsName.None);
|
_logger.LogInformation("Получение объекта по идентификатору");
|
||||||
|
_logger.LogDebug("Объект: {id}", id);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var querySelect = @"
|
||||||
|
SELECT * FROM Works
|
||||||
|
WHERE Id=@id";
|
||||||
|
var works = connection.QueryFirst<Works>(querySelect, new { id });
|
||||||
|
_logger.LogDebug("Найденный объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(works));
|
||||||
|
return works;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateWorks(Works works)
|
public void UpdateWorks(Works works)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("Редактирование объекта");
|
||||||
|
_logger.LogDebug("Объект: {json}",
|
||||||
|
JsonConvert.SerializeObject(works));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||||
|
var queryUpdate = @"
|
||||||
|
UPDATE Works
|
||||||
|
SET
|
||||||
|
WorkName=@WorkName,
|
||||||
|
MaterialsName=@MaterialsName
|
||||||
|
WHERE Id=@Id";
|
||||||
|
connection.Execute(queryUpdate, works);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
15
ProjectRepairWork/appsettings.json
Normal file
15
ProjectRepairWork/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Debug",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Logs.txt",
|
||||||
|
"rollingInterval": "Day"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user