Лабораторная работа №2

This commit is contained in:
Anitonchik 2024-11-15 11:36:49 +04:00
parent 38f054099a
commit 6acb10c427
4 changed files with 70 additions and 6 deletions

View File

@ -25,11 +25,11 @@ namespace ProjectCompanyFurniture.Forms
comboBoxClient.DataSource = clientRepository.ReadClients();
comboBoxClient.DisplayMember = "FirstName";
comboBoxClient.ValueMember = "Id";
comboBoxClient.ValueMember = "ID";
ColumnProduct.DataSource = productRepository.ReadProducts();
ColumnProduct.DisplayMember = "Name";
ColumnProduct.ValueMember = "Id";
ColumnProduct.ValueMember = "ID";
}
private void ButtonSave_Click(object sender, EventArgs e)

View File

@ -45,6 +45,9 @@
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@ -8,5 +8,5 @@ namespace ProjectCompanyFurniture.Repositories.Implementations;
public class ConnectionString : IConnectionString
{
string IConnectionString.ConnectionString => "";
string IConnectionString.ConnectionString => "Driver={PostgreSQL};Server=localhost;Port=5432;Database=furnitureotp;Uid=postgres;Pwd=postgres";
}

View File

@ -1,7 +1,10 @@
using Microsoft.Extensions.Logging;
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;
@ -21,14 +24,72 @@ public class InvoiceRepository : IInvoiceRepository
public void CreateInvoice(Invoice invoice)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(invoice));
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
connection.Open();
using var transaction = connection.BeginTransaction();
var queryInsert = @"INSERT INTO Invoices (ClientID, AvailabilityOfPromotionalCode,
DiscountPercentage, SellingPrice, Data)
VALUES (@ClientID, @AvailabilityOfPromotionalCode,
@DiscountPercentage, @SellingPrice, @Data);
SELECT MAX(Id) FROM FeedReplenishments";
var invoiceID = connection.QueryFirst<int>(queryInsert, invoice, transaction);
var querySubInsert = @"INSERT INTO InvoiceProducts (InvoiceId, ProductId, Count, SellingPrice)
VALUES (@InvoiceId, @ProductId, @Count, @SellingPrice)";
foreach (var elem in invoice.Products)
{
connection.Execute(querySubInsert, new
{
invoiceID,
elem.ProductID,
elem.Count
}, transaction);
}
transaction.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteInvoice(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var queryDelete = @"DELETE FROM Invoices
WHERE ID=@id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public IEnumerable<Invoice> ReadInvoices(DateTime? dateForm = null, DateTime? dateTo = null, int? clientId = null)
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new SqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM Invoices";
var invoices = connection.Query<Invoice>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices));
return invoices;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}