ПИбд-22 Морозов Д.В. Лабораторная 2 #7
@ -91,6 +91,7 @@
|
||||
//
|
||||
// dateTimePickerOrderDate
|
||||
//
|
||||
dateTimePickerOrderDate.CausesValidation = false;
|
||||
dateTimePickerOrderDate.Enabled = false;
|
||||
dateTimePickerOrderDate.Location = new Point(190, 12);
|
||||
dateTimePickerOrderDate.Name = "dateTimePickerOrderDate";
|
||||
@ -110,6 +111,7 @@
|
||||
dateTimeIssue.Name = "dateTimeIssue";
|
||||
dateTimeIssue.Size = new Size(200, 25);
|
||||
dateTimeIssue.TabIndex = 6;
|
||||
dateTimeIssue.Value = new DateTime(2024, 12, 15, 0, 0, 0, 0);
|
||||
//
|
||||
// label3
|
||||
//
|
||||
@ -186,7 +188,7 @@
|
||||
//
|
||||
// Detail
|
||||
//
|
||||
Detail.HeaderText = "Деталь";
|
||||
Detail.HeaderText = "Название детали";
|
||||
Detail.Name = "Detail";
|
||||
//
|
||||
// DetailCount
|
||||
|
@ -1,7 +1,10 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Entities.Enums;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -30,6 +33,7 @@ namespace ProjectRepairCompany.Forms
|
||||
dateTimeCompletion.Value = order.DateCompletion;
|
||||
dateTimeIssue.Value = order.DateIssue;
|
||||
|
||||
dataGridView.Rows.Clear();
|
||||
foreach (var detail in order.OrderDetails)
|
||||
{
|
||||
dataGridView.Rows.Add(detail.DetailId, detail.DetailCount);
|
||||
@ -59,26 +63,20 @@ namespace ProjectRepairCompany.Forms
|
||||
Detail.DataSource = detailRepository.ReadDetails();
|
||||
Detail.DisplayMember = "NameDetail";
|
||||
Detail.ValueMember = "Id";
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonSave_Click(object sender, EventArgs e)
|
||||
private void ButtonSave_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
if (dataGridView.RowCount < 1 || comboBoxMaster.SelectedIndex < 0)
|
||||
{
|
||||
if (dataGridView.RowCount < 1 || comboBoxMaster.SelectedIndex < 0)
|
||||
{
|
||||
throw new Exception("Имеются незаполненные поля.");
|
||||
}
|
||||
throw new Exception("Имеются незаполненные поля.");
|
||||
}
|
||||
|
||||
var order = CreateOrder();
|
||||
_orderRepository.CreateOrder(order);
|
||||
|
||||
var order = CreateOrder();
|
||||
if (_orderId.HasValue)
|
||||
{
|
||||
_orderRepository.UpdateOrder(order);
|
||||
}
|
||||
else
|
||||
{
|
||||
_orderRepository.CreateOrder(order);
|
||||
}
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
@ -96,13 +94,15 @@ namespace ProjectRepairCompany.Forms
|
||||
foreach (DataGridViewRow row in dataGridView.Rows)
|
||||
{
|
||||
if (row.Cells["Detail"].Value == null ||
|
||||
row.Cells["DetailCount"].Value == null)
|
||||
row.Cells["DetailCount"].Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
list.Add(OrderDetail.CreateOperation(0,
|
||||
Convert.ToInt32(row.Cells["Detail"].Value),
|
||||
Convert.ToInt32(row.Cells["DetailCount"].Value)));
|
||||
list.Add(OrderDetail.CreateOperation(
|
||||
0,
|
||||
Convert.ToInt32(row.Cells["Detail"].Value),
|
||||
Convert.ToInt32(row.Cells["DetailCount"].Value)
|
||||
));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@ -111,7 +111,7 @@ namespace ProjectRepairCompany.Forms
|
||||
var orderDetails = CreateListOrderDetailsFromDataGrid();
|
||||
|
||||
return Order.CreateOperation(
|
||||
_orderId ?? 0,
|
||||
0,
|
||||
Convert.ToInt32(numericUpDownFullPrice.Value),
|
||||
(int)comboBoxMaster.SelectedValue,
|
||||
dateTimeCompletion.Value,
|
||||
@ -121,4 +121,7 @@ namespace ProjectRepairCompany.Forms
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -123,4 +123,10 @@
|
||||
<metadata name="DetailCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
<metadata name="Detail.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
<metadata name="DetailCount.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>True</value>
|
||||
</metadata>
|
||||
</root>
|
@ -29,7 +29,6 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
panel1 = new Panel();
|
||||
ButtonUp = new Button();
|
||||
ButtonDel = new Button();
|
||||
ButtonAdd = new Button();
|
||||
dataGridView = new DataGridView();
|
||||
@ -39,26 +38,14 @@
|
||||
//
|
||||
// panel1
|
||||
//
|
||||
panel1.Controls.Add(ButtonUp);
|
||||
panel1.Controls.Add(ButtonDel);
|
||||
panel1.Controls.Add(ButtonAdd);
|
||||
panel1.Dock = DockStyle.Right;
|
||||
panel1.Location = new Point(607, 0);
|
||||
panel1.Location = new Point(871, 0);
|
||||
panel1.Name = "panel1";
|
||||
panel1.Size = new Size(193, 450);
|
||||
panel1.TabIndex = 2;
|
||||
//
|
||||
// ButtonUp
|
||||
//
|
||||
ButtonUp.BackgroundImage = Properties.Resources.Feedbin_Icon_home_edit_svg;
|
||||
ButtonUp.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
ButtonUp.Location = new Point(42, 113);
|
||||
ButtonUp.Name = "ButtonUp";
|
||||
ButtonUp.Size = new Size(117, 77);
|
||||
ButtonUp.TabIndex = 2;
|
||||
ButtonUp.UseVisualStyleBackColor = true;
|
||||
ButtonUp.Click += ButtonUp_Click;
|
||||
//
|
||||
// ButtonDel
|
||||
//
|
||||
ButtonDel.BackgroundImage = Properties.Resources.Ic_remove_circle_48px_svg;
|
||||
@ -96,14 +83,14 @@
|
||||
dataGridView.ReadOnly = true;
|
||||
dataGridView.RowHeadersVisible = false;
|
||||
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridView.Size = new Size(607, 450);
|
||||
dataGridView.Size = new Size(871, 450);
|
||||
dataGridView.TabIndex = 3;
|
||||
//
|
||||
// FormOrders
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 17F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
ClientSize = new Size(1064, 450);
|
||||
Controls.Add(dataGridView);
|
||||
Controls.Add(panel1);
|
||||
Name = "FormOrders";
|
||||
@ -117,7 +104,6 @@
|
||||
#endregion
|
||||
|
||||
private Panel panel1;
|
||||
private Button ButtonUp;
|
||||
private Button ButtonDel;
|
||||
private Button ButtonAdd;
|
||||
private DataGridView dataGridView;
|
||||
|
@ -1,4 +1,5 @@
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -52,27 +53,6 @@ namespace ProjectRepairCompany.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonUp_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!TryGetIdentifierFromSelectRow(out var findId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
var form = _container.Resolve<FormOrder>();
|
||||
form.Id = findId;
|
||||
if (form.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
LoadList();
|
||||
dataGridView.Refresh();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка изменения", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonDel_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
@ -1,13 +1,8 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Repositories.Implementations;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace ProjectRepairCompany.Forms
|
||||
@ -15,13 +10,19 @@ namespace ProjectRepairCompany.Forms
|
||||
public partial class FormStorageDetail : Form
|
||||
{
|
||||
private readonly IStorageDetailRepository _storageDetailRepository;
|
||||
private readonly IDetailRepository _detailRepository;
|
||||
|
||||
public FormStorageDetail(IStorageDetailRepository storageDetailRepository, IDetailRepository detailRepository)
|
||||
public FormStorageDetail(IStorageDetailRepository storageDetailRepository, IDetailRepository detailRepository, IStorageRepository storageRepository)
|
||||
{
|
||||
InitializeComponent();
|
||||
_storageDetailRepository = storageDetailRepository ?? throw new ArgumentNullException(nameof(storageDetailRepository));
|
||||
_detailRepository = detailRepository ?? throw new ArgumentNullException(nameof(detailRepository));
|
||||
|
||||
comboBoxAddress.DataSource = storageRepository.ReadStorages();
|
||||
comboBoxAddress.DisplayMember = "StorageAddress";
|
||||
comboBoxAddress.ValueMember = "Id";
|
||||
|
||||
comboBoxDetail.DataSource = detailRepository.ReadDetails();
|
||||
comboBoxDetail.DisplayMember = "NameDetail";
|
||||
comboBoxDetail.ValueMember = "Id";
|
||||
}
|
||||
|
||||
private void ButtonSave_Click(object sender, EventArgs e)
|
||||
@ -42,6 +43,5 @@ namespace ProjectRepairCompany.Forms
|
||||
}
|
||||
|
||||
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ namespace ProjectRepairCompany.Forms
|
||||
{
|
||||
try
|
||||
{
|
||||
LoadList();
|
||||
LoadList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -1,6 +1,10 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectRepairCompany.Repositories;
|
||||
using ProjectRepairCompany.Repositories.Implementations;
|
||||
using Serilog;
|
||||
using Unity;
|
||||
using Unity.Microsoft.Logging;
|
||||
|
||||
namespace ProjectRepairCompany
|
||||
{
|
||||
@ -21,12 +25,26 @@ namespace ProjectRepairCompany
|
||||
private static IUnityContainer CreateContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
container.RegisterType<IDetailRepository, DetailRepository>();
|
||||
container.RegisterType<IMasterRepository, MasterRepository>();
|
||||
container.RegisterType<IStorageRepository, StorageRepository>();
|
||||
container.RegisterType<IStorageDetailRepository, StorageDetailRepository>();
|
||||
container.RegisterType<IOrderRepository, OrderRepository>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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="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="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>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRepairCompany.Repositories;
|
||||
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
}
|
@ -12,6 +12,5 @@ public interface IOrderRepository
|
||||
IEnumerable<Order> ReadOrders();
|
||||
Order ReadOrderById(int id);
|
||||
void CreateOrder(Order order);
|
||||
void UpdateOrder(Order order);
|
||||
void DeleteOrder(int id);
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString =>
|
||||
"Host=localhost;Port=5432;Database=otp;Username=postgres;Password=postgres";
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Entities.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -10,25 +14,115 @@ namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class DetailRepository : IDetailRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<DetailRepository> _logger;
|
||||
|
||||
public DetailRepository(IConnectionString connectionString, ILogger<DetailRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateDetail(Detail detail)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteDetail(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Detail ReadDetailById(int id)
|
||||
{
|
||||
return Detail.CreateEntity(0, string.Empty, 0, 0, DetailProperties.None);
|
||||
}
|
||||
|
||||
public IEnumerable<Detail> ReadDetails()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(detail));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Detail (NameDetail, NumberDetail, PriceDetail, DetailProperties)
|
||||
VALUES (@NameDetail, @NumberDetail, @PriceDetail, @DetailProperties)";
|
||||
connection.Execute(queryInsert, detail);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка добавления");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateDetail(Detail detail)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(detail));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Detail
|
||||
SET
|
||||
NameDetail = @NameDetail,
|
||||
NumberDetail = @NumberDetail,
|
||||
PriceDetail = @PriceDetail,
|
||||
DetailProperties = @DetailProperties
|
||||
WHERE Id = @Id";
|
||||
connection.Execute(queryUpdate, detail);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка редактирования");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteDetail(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Detail
|
||||
WHERE Id = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка удаления");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Detail ReadDetailById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение по id");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Detail
|
||||
WHERE Id = @id";
|
||||
var detail = connection.QueryFirst<Detail>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(detail));
|
||||
return detail;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка поиска");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Detail> ReadDetails()
|
||||
{
|
||||
_logger.LogInformation("Получение объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Detail";
|
||||
var details = connection.Query<Detail>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(details));
|
||||
return details;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,12 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Entities.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Metrics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -10,25 +15,112 @@ namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class MasterRepository : IMasterRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<MasterRepository> _logger;
|
||||
public MasterRepository(IConnectionString connectionString, ILogger<MasterRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateMaster(Master master)
|
||||
{
|
||||
}
|
||||
|
||||
public void DeleteMaster(int id)
|
||||
{
|
||||
}
|
||||
|
||||
public Master ReadMasterById(int id)
|
||||
{
|
||||
return Master.CreateEntity(0, string.Empty, DateTime.MinValue, MasterPost.None);
|
||||
}
|
||||
|
||||
public IEnumerable<Master> ReadMasters()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Добавление обьекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(master));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Master (MasterName, StartDate, MasterPost)
|
||||
VALUES (@MasterName, @StartDate, @MasterPost)";
|
||||
connection.Execute(queryInsert, master);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка добавления");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateMaster(Master master)
|
||||
{
|
||||
_logger.LogInformation("Редактирование обьекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(master));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Master
|
||||
SET
|
||||
MasterName = @MasterName,
|
||||
StartDate = @StartDate,
|
||||
MasterPost = @MasterPost
|
||||
WHERE Id = @Id";
|
||||
connection.Execute(queryUpdate, master);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка Редактирования");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteMaster(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление обьекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Master
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка Удаления");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Master ReadMasterById(int id)
|
||||
{
|
||||
_logger.LogInformation("Получение по id");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Master
|
||||
WHERE Id=@id";
|
||||
var master = connection.QueryFirst<Master>(querySelect, new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(master));
|
||||
return master;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка поиска");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Master> ReadMasters()
|
||||
{
|
||||
_logger.LogInformation("Получение объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Master";
|
||||
var master = connection.Query<Master>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(master));
|
||||
return master;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,35 +1,123 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using ProjectRepairCompany.Entities.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
|
||||
|
||||
namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class OrderRepository : IOrderRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<OrderRepository> _logger;
|
||||
|
||||
|
||||
public OrderRepository(IConnectionString connectionString, ILogger<OrderRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateOrder(Order order)
|
||||
{
|
||||
_logger.LogInformation("Добавление нового заказа.");
|
||||
_logger.LogDebug("Объект заказа: {json}", JsonConvert.SerializeObject(order));
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
|
||||
var queryInsert = @"
|
||||
INSERT INTO ""Order"" (OrderDate, DateCompletion, DateIssue, FullPrice, MasterId)
|
||||
VALUES (@OrderDate, @DateCompletion, @DateIssue, @FullPrice, @MasterId)
|
||||
RETURNING Id";
|
||||
|
||||
var orderId = connection.QueryFirst<int>(queryInsert, order, transaction);
|
||||
_logger.LogInformation("Новый заказ создан с Id {OrderId}", orderId);
|
||||
|
||||
var querySubInsert = @"
|
||||
INSERT INTO OrderDetail (OrderId, DetailId, DetailCount)
|
||||
VALUES (@OrderId, @DetailId, @DetailCount)";
|
||||
|
||||
foreach (var elem in order.OrderDetails)
|
||||
{
|
||||
_logger.LogDebug("Добавление детали: DetailId = {DetailId}, DetailCount = {DetailCount}",
|
||||
elem.DetailId, elem.DetailCount);
|
||||
connection.Execute(querySubInsert, new { orderId, elem.DetailId, elem.DetailCount }, transaction);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
_logger.LogInformation("Заказ с деталями успешно добавлен.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении заказа.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void DeleteOrder(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта с Id {OrderId}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
|
||||
var queryDeleteDetails = "DELETE FROM OrderDetail WHERE OrderId = @OrderId";
|
||||
connection.Execute(queryDeleteDetails, new { OrderId = id }, transaction);
|
||||
|
||||
var queryDeleteOrder = "DELETE FROM \"Order\" WHERE Id = @Id";
|
||||
connection.Execute(queryDeleteOrder, new { Id = id }, transaction);
|
||||
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Order ReadOrderById(int id)
|
||||
{
|
||||
return Order.CreateOperation(0, 0, 0, DateTime.MinValue, DateTime.MinValue, Enumerable.Empty<OrderDetail>());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public IEnumerable<Order> ReadOrders()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
|
||||
public void UpdateOrder(Order order)
|
||||
{
|
||||
var querySelect = @"SELECT * FROM ""Order""";
|
||||
var orders = connection.Query<Order>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orders));
|
||||
return orders;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,20 +1,62 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
|
||||
|
||||
namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class StorageDetailRepository : IStorageDetailRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<StorageDetailRepository> _logger;
|
||||
|
||||
public StorageDetailRepository(IConnectionString connectionString, ILogger<StorageDetailRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateStorageDetail(StorageDetail storageDetail)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(storageDetail));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO StorageDetail (StorageId, DetailId, DetailCount, SupplyDate)
|
||||
VALUES (@StorageId, @DetailId, @DetailCount, @SupplyDate)";
|
||||
connection.Execute(queryInsert, storageDetail);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка добавления");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IEnumerable<StorageDetail> ReadStorageDetails(DateTime? dateFrom = null, DateTime? dateTo = null, int? storageId = null, int? detailId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM StorageDetail";
|
||||
var storageDetails = connection.Query<StorageDetail>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(storageDetails));
|
||||
return storageDetails;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,10 @@
|
||||
using ProjectRepairCompany.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using ProjectRepairCompany.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Npgsql;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,25 +13,110 @@ namespace ProjectRepairCompany.Repositories.Implementations;
|
||||
|
||||
public class StorageRepository : IStorageRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<StorageRepository> _logger;
|
||||
public StorageRepository(IConnectionString connectionString, ILogger<StorageRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateStorage(Storage storage)
|
||||
{
|
||||
_logger.LogInformation("Добавление обьекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(storage));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Storage (StorageAddress, Capacity)
|
||||
VALUES (@StorageAddress, @Capacity)";
|
||||
connection.Execute(queryInsert, storage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка добавления");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void UpdateStorage(Storage storage)
|
||||
{
|
||||
_logger.LogInformation("Редактирование обьекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(storage));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Storage
|
||||
SET
|
||||
StorageAddress = @StorageAddress,
|
||||
Capacity = @Capacity
|
||||
WHERE Id = @Id";
|
||||
connection.Execute(queryUpdate, storage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка Редактирования");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteStorage(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление обьекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Storage
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new {id});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка Удаления");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Storage ReadStorageById(int id)
|
||||
{
|
||||
return Storage.CreateEntity(0, string.Empty, 0);
|
||||
_logger.LogInformation("Получение по id");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Storage
|
||||
WHERE Id=@id";
|
||||
var storage = connection.QueryFirst<Storage>(querySelect, new {id});
|
||||
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(storage));
|
||||
return storage;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка поиска");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Storage> ReadStorages()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Storage";
|
||||
var storage = connection.Query<Storage>(querySelect).ToList();
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(storage));
|
||||
return storage;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStorage(Storage storage)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
16
ProjectRepairCompany/ProjectRepairCompany/appsettings.json
Normal file
16
ProjectRepairCompany/ProjectRepairCompany/appsettings.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logo/repairCompany_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user