diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Forms/FormInvoice.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Forms/FormInvoice.cs index ab54160..4433057 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Forms/FormInvoice.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Forms/FormInvoice.cs @@ -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) diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj b/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj index 36b824f..68c312c 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/ProjectCompanyFurniture.csproj @@ -45,6 +45,9 @@ + + Always + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs index ccc3216..36ec3c1 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/ConnectionString.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace ProjectCompanyFurniture.Repositories.Implementations; -public class ConnectionString : IConnectionString +public class ConnectionString : IConnectionString { - string IConnectionString.ConnectionString => ""; + string IConnectionString.ConnectionString => "Driver={PostgreSQL};Server=localhost;Port=5432;Database=furnitureotp;Uid=postgres;Pwd=postgres"; } diff --git a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs index 2ca394b..451e062 100644 --- a/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs +++ b/ProjectCompanyFurniture/ProjectCompanyFurniture/Repositories/Implementations/InvoiceRepository.cs @@ -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(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 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(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(invoices)); + return invoices; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } }