From 5232e9fc33df1dd5ed9a6f9158b274525752e3a3 Mon Sep 17 00:00:00 2001 From: zw1st <144824777+zw1st@users.noreply.github.com> Date: Tue, 17 Dec 2024 18:42:55 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D0=93=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E?= =?UTF-8?q?=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E?= =?UTF-8?q?=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9E=D0=9B=20?= =?UTF-8?q?=E2=84=962?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectTourismCompany/Entities/Check.cs | 4 +- .../Forms/FormCheck.Designer.cs | 44 ++---- .../ProjectTourismCompany/Forms/FormCheck.cs | 18 +-- .../ProjectTourismCompany/Forms/FormClient.cs | 2 +- .../Forms/FormClients.Designer.cs | 4 + .../Forms/FormCompany.Designer.cs | 34 ++--- .../Forms/FormCompany.cs | 4 +- .../Forms/FormCountries.Designer.cs | 1 + .../Forms/FormCountry.cs | 2 +- .../ProjectTourismCompany/Forms/FormTrip.cs | 2 +- .../Forms/FormTrips.Designer.cs | 12 +- .../ProjectTourismCompany/Program.cs | 22 ++- .../ProjectTourismCompany.csproj | 17 +++ .../Repositories/ICheckRepository.cs | 4 +- .../Repositories/IConnection.cs | 12 ++ .../Implementations/CheckRepository.cs | 80 ++++++++++- .../Implementations/ClientRepository.cs | 102 ++++++++++++- .../Implementations/CompanyRepository.cs | 134 +++++++++++++++++- .../Implementations/ConnectionString.cs | 12 ++ .../Implementations/CountryRepository.cs | 102 ++++++++++++- .../Implementations/TripRepository.cs | 66 ++++++++- .../ProjectTourismCompany/appsettings.json | 15 ++ 22 files changed, 590 insertions(+), 103 deletions(-) create mode 100644 ProjectTourismCompany/ProjectTourismCompany/Repositories/IConnection.cs create mode 100644 ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ConnectionString.cs create mode 100644 ProjectTourismCompany/ProjectTourismCompany/appsettings.json diff --git a/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs b/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs index 942eab8..61642fa 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs @@ -14,8 +14,8 @@ public class Check public int Price { get; private set; } public int TripId { get; private set; } public int ClientId { get; private set; } - public static Check CreateCheck(int id, string title, DateTime purchaseDate, int price, int tripId, int clientId) + public static Check CreateCheck(int id, DateTime purchaseDate, int price, int tripId, int clientId) { - return new Check() { Id = id, Title = title, PurchaseDate = purchaseDate, Price = price, TripId = tripId, ClientId = clientId }; + return new Check() { Id = id, PurchaseDate = purchaseDate, Price = price, TripId = tripId, ClientId = clientId }; } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs index 345125b..44ed840 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs @@ -35,8 +35,6 @@ label1 = new Label(); numericUpDownPrice = new NumericUpDown(); label2 = new Label(); - textBoxTitle = new TextBox(); - label3 = new Label(); label4 = new Label(); comboBoxClient = new ComboBox(); comboBoxTrip = new ComboBox(); @@ -46,7 +44,7 @@ // buttonCancel // buttonCancel.BackColor = Color.IndianRed; - buttonCancel.Location = new Point(221, 260); + buttonCancel.Location = new Point(220, 207); buttonCancel.Name = "buttonCancel"; buttonCancel.Size = new Size(135, 51); buttonCancel.TabIndex = 31; @@ -57,7 +55,7 @@ // buttonSave // buttonSave.BackColor = Color.LimeGreen; - buttonSave.Location = new Point(367, 260); + buttonSave.Location = new Point(366, 207); buttonSave.Name = "buttonSave"; buttonSave.Size = new Size(135, 51); buttonSave.TabIndex = 30; @@ -67,7 +65,7 @@ // // dateTimePickerPurchaseDate // - dateTimePickerPurchaseDate.Location = new Point(282, 66); + dateTimePickerPurchaseDate.Location = new Point(281, 13); dateTimePickerPurchaseDate.Name = "dateTimePickerPurchaseDate"; dateTimePickerPurchaseDate.Size = new Size(220, 27); dateTimePickerPurchaseDate.TabIndex = 25; @@ -75,7 +73,7 @@ // labelStartDate // labelStartDate.AutoSize = true; - labelStartDate.Location = new Point(45, 71); + labelStartDate.Location = new Point(44, 18); labelStartDate.Name = "labelStartDate"; labelStartDate.Size = new Size(92, 20); labelStartDate.TabIndex = 18; @@ -84,7 +82,7 @@ // label1 // label1.AutoSize = true; - label1.Location = new Point(64, 117); + label1.Location = new Point(63, 64); label1.Name = "label1"; label1.Size = new Size(45, 20); label1.TabIndex = 34; @@ -92,7 +90,7 @@ // // numericUpDownPrice // - numericUpDownPrice.Location = new Point(284, 110); + numericUpDownPrice.Location = new Point(283, 57); numericUpDownPrice.Maximum = new decimal(new int[] { 1000000, 0, 0, 0 }); numericUpDownPrice.Minimum = new decimal(new int[] { 1, 0, 0, 0 }); numericUpDownPrice.Name = "numericUpDownPrice"; @@ -103,32 +101,16 @@ // label2 // label2.AutoSize = true; - label2.Location = new Point(45, 163); + label2.Location = new Point(44, 110); label2.Name = "label2"; label2.Size = new Size(101, 20); label2.TabIndex = 36; label2.Text = "ФИО клиента"; // - // textBoxTitle - // - textBoxTitle.Location = new Point(282, 22); - textBoxTitle.Name = "textBoxTitle"; - textBoxTitle.Size = new Size(220, 27); - textBoxTitle.TabIndex = 38; - // - // label3 - // - label3.AutoSize = true; - label3.Location = new Point(45, 22); - label3.Name = "label3"; - label3.Size = new Size(135, 20); - label3.TabIndex = 37; - label3.Text = "Название путевки"; - // // label4 // label4.AutoSize = true; - label4.Location = new Point(45, 208); + label4.Location = new Point(44, 155); label4.Name = "label4"; label4.Size = new Size(64, 20); label4.TabIndex = 39; @@ -138,7 +120,7 @@ // comboBoxClient.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxClient.FormattingEnabled = true; - comboBoxClient.Location = new Point(282, 163); + comboBoxClient.Location = new Point(281, 110); comboBoxClient.Name = "comboBoxClient"; comboBoxClient.Size = new Size(217, 28); comboBoxClient.TabIndex = 41; @@ -147,7 +129,7 @@ // comboBoxTrip.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxTrip.FormattingEnabled = true; - comboBoxTrip.Location = new Point(282, 208); + comboBoxTrip.Location = new Point(281, 155); comboBoxTrip.Name = "comboBoxTrip"; comboBoxTrip.Size = new Size(217, 28); comboBoxTrip.TabIndex = 42; @@ -156,12 +138,10 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(513, 322); + ClientSize = new Size(513, 272); Controls.Add(comboBoxTrip); Controls.Add(comboBoxClient); Controls.Add(label4); - Controls.Add(textBoxTitle); - Controls.Add(label3); Controls.Add(label2); Controls.Add(numericUpDownPrice); Controls.Add(label1); @@ -186,8 +166,6 @@ private Label label1; private NumericUpDown numericUpDownPrice; private Label label2; - private TextBox textBoxTitle; - private Label label3; private Label label4; private ComboBox comboBoxClient; private ComboBox comboBoxTrip; diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs index 29ca619..3f46743 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs @@ -1,6 +1,7 @@ using Microsoft.VisualBasic.FileIO; using ProjectTourismCompany.Entities; using ProjectTourismCompany.Repositories; +using ProjectTourismCompany.Repositories.Implementations; using System; using System.Collections.Generic; using System.ComponentModel; @@ -29,11 +30,11 @@ public partial class FormCheck : Form throw new InvalidDataException(nameof(check)); } - textBoxTitle.Text = check.Title; dateTimePickerPurchaseDate.Value = check.PurchaseDate; numericUpDownPrice.Value = check.Price; - comboBoxClient.SelectedItem = check.ClientId; + comboBoxTrip.SelectedItem = check.Title; + comboBoxTrip.SelectedItem = check.TripId; _checkId = value; @@ -45,7 +46,7 @@ public partial class FormCheck : Form } } } - public FormCheck(ICheckRepository checkRepository, IClientRepository clientRepository) + public FormCheck(ICheckRepository checkRepository, IClientRepository clientRepository, ITripRepository tripRepository) { InitializeComponent(); _checkRepository = checkRepository ?? throw new ArgumentNullException(nameof(checkRepository)); @@ -56,20 +57,21 @@ public partial class FormCheck : Form comboBoxTrip.DisplayMember = "Title"; comboBoxTrip.ValueMember = "Id"; + comboBoxTrip.DataSource = tripRepository.ReadTrips(); + } private void buttonSave_Click(object sender, EventArgs e) { try { - if (string.IsNullOrWhiteSpace(textBoxTitle.Text) || - comboBoxTrip.SelectedIndex < 0 || + if (comboBoxTrip.SelectedIndex < 0 || comboBoxClient.SelectedIndex < 0) { throw new Exception("Имеются незаполненные поля"); } - _checkRepository.CreateCheck(Check.CreateCheck(_checkId, textBoxTitle.Text, dateTimePickerPurchaseDate.Value, - (int)numericUpDownPrice.Value, (int)comboBoxTrip.SelectedItem!, - (int)comboBoxClient.SelectedItem!)); + _checkRepository.CreateCheck(Check.CreateCheck(_checkId, dateTimePickerPurchaseDate.Value, + (int)numericUpDownPrice.Value, (int)comboBoxTrip.SelectedIndex!, + (int)comboBoxClient.SelectedIndex!)); Close(); } catch (Exception ex) diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClient.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClient.cs index a6663d1..b5869ba 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClient.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClient.cs @@ -71,7 +71,7 @@ Enum.GetValues(typeof(ClientStatus)); } else { - _clientRepository.UpdateClient(CreateClient(0)); + _clientRepository.CreateClient(CreateClient(0)); } Close(); } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClients.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClients.Designer.cs index 2f4f961..a05284c 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClients.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormClients.Designer.cs @@ -83,6 +83,10 @@ // // dataGridViewClient // + dataGridViewClient.AllowUserToAddRows = false; + dataGridViewClient.AllowUserToDeleteRows = false; + dataGridViewClient.AllowUserToResizeColumns = false; + dataGridViewClient.AllowUserToResizeRows = false; dataGridViewClient.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridViewClient.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridViewClient.Dock = DockStyle.Fill; diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.Designer.cs index a44dd5a..0969b20 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.Designer.cs @@ -34,10 +34,10 @@ buttonSave = new Button(); groupBox1 = new GroupBox(); dataGridView1 = new DataGridView(); - ColumnTitle = new DataGridViewComboBoxColumn(); - ColumnAdditionalPrice = new DataGridViewButtonColumn(); comboBoxCountry = new ComboBox(); label2 = new Label(); + ColumnTitle = new DataGridViewComboBoxColumn(); + ColumnAdditionalPrice = new DataGridViewTextBoxColumn(); groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); SuspendLayout(); @@ -107,19 +107,6 @@ dataGridView1.Size = new Size(292, 280); dataGridView1.TabIndex = 0; // - // ColumnTitle - // - ColumnTitle.HeaderText = "Название путевки"; - ColumnTitle.MinimumWidth = 6; - ColumnTitle.Name = "ColumnTitle"; - // - // ColumnAdditionalPrice - // - ColumnAdditionalPrice.HeaderText = "Надбавка фирмы"; - ColumnAdditionalPrice.MinimumWidth = 6; - ColumnAdditionalPrice.Name = "ColumnAdditionalPrice"; - ColumnAdditionalPrice.Resizable = DataGridViewTriState.True; - // // comboBoxCountry // comboBoxCountry.FormattingEnabled = true; @@ -137,6 +124,19 @@ label2.TabIndex = 21; label2.Text = "Страна"; // + // ColumnTitle + // + ColumnTitle.HeaderText = "Название путевки"; + ColumnTitle.MinimumWidth = 6; + ColumnTitle.Name = "ColumnTitle"; + // + // ColumnAdditionalPrice + // + ColumnAdditionalPrice.HeaderText = "Надбавка фирмы"; + ColumnAdditionalPrice.MinimumWidth = 6; + ColumnAdditionalPrice.Name = "ColumnAdditionalPrice"; + ColumnAdditionalPrice.SortMode = DataGridViewColumnSortMode.NotSortable; + // // FormCompany // AutoScaleDimensions = new SizeF(8F, 20F); @@ -166,9 +166,9 @@ private Button buttonSave; private GroupBox groupBox1; private DataGridView dataGridView1; - private DataGridViewComboBoxColumn ColumnTitle; - private DataGridViewButtonColumn ColumnAdditionalPrice; private ComboBox comboBoxCountry; private Label label2; + private DataGridViewComboBoxColumn ColumnTitle; + private DataGridViewTextBoxColumn ColumnAdditionalPrice; } } \ No newline at end of file diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs index d756f51..9fa9cb5 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs @@ -55,7 +55,7 @@ public partial class FormCompany : Form _companyRepository = companyRepository ?? throw new ArgumentNullException(nameof(companyRepository)); comboBoxCountry.DataSource = countryRepository.ReadCountries(); - comboBoxCountry.DisplayMember = "Name"; + comboBoxCountry.DisplayMember = "CountryName"; comboBoxCountry.ValueMember = "Id"; ColumnTitle.DataSource = tripRepository.ReadTrips(); @@ -73,7 +73,7 @@ public partial class FormCompany : Form { throw new Exception("Имеются незаполненные поля"); } - _companyRepository.CreateCompany(Company.CreateCompany(0, textBoxName.Text, (int)comboBoxCountry.SelectedItem!, CreateListCompanyTripFromDataGrid())); + _companyRepository.CreateCompany(Company.CreateCompany(0, textBoxName.Text, (int)comboBoxCountry.SelectedIndex!, CreateListCompanyTripFromDataGrid())); Close(); } catch (Exception ex) diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountries.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountries.Designer.cs index 8d9df93..9e6ebca 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountries.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountries.Designer.cs @@ -51,6 +51,7 @@ dataGridView1.MultiSelect = false; dataGridView1.Name = "dataGridView1"; dataGridView1.ReadOnly = true; + dataGridView1.RowHeadersVisible = false; dataGridView1.RowHeadersWidth = 51; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.Size = new Size(800, 450); diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountry.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountry.cs index 33731b3..c431483 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountry.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCountry.cs @@ -61,7 +61,7 @@ public partial class FormCountry : Form } else { - _countryRepository.UpdateCountry(CreateCountry(0)); + _countryRepository.CreateCountry(CreateCountry(0)); } Close(); } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs index 0735bfe..5d086e3 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs @@ -103,7 +103,7 @@ Enum.GetValues(typeof(TravelType))) { tr |= (TravelType)elem; } - return Trip.CreateOpeartion(0, textBoxTitle.Text, (int)numericUpDownPrice.Value, + return Trip.CreateOpeartion(id, textBoxTitle.Text, (int)numericUpDownPrice.Value, (int)numericUpDownHumanCapacity.Value, dateTimePickerStartDate.Value, dateTimePickerEndDate.Value, textBoxStartCity.Text, textBoxEndCity.Text, tr); } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs index f62873e..87dc442 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs @@ -43,31 +43,31 @@ dataGridView1.AllowUserToResizeRows = false; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView1.ColumnHeadersVisible = false; dataGridView1.Dock = DockStyle.Fill; dataGridView1.Location = new Point(0, 0); dataGridView1.MultiSelect = false; dataGridView1.Name = "dataGridView1"; dataGridView1.ReadOnly = true; + dataGridView1.RowHeadersVisible = false; dataGridView1.RowHeadersWidth = 51; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridView1.Size = new Size(800, 450); + dataGridView1.Size = new Size(1098, 538); dataGridView1.TabIndex = 0; // // panel1 // panel1.Controls.Add(buttonAddTrip); panel1.Dock = DockStyle.Right; - panel1.Location = new Point(645, 0); + panel1.Location = new Point(970, 0); panel1.Name = "panel1"; - panel1.Size = new Size(155, 450); + panel1.Size = new Size(128, 538); panel1.TabIndex = 1; // // buttonAddTrip // buttonAddTrip.BackgroundImage = Properties.Resources.plus; buttonAddTrip.BackgroundImageLayout = ImageLayout.Stretch; - buttonAddTrip.Location = new Point(34, 31); + buttonAddTrip.Location = new Point(23, 29); buttonAddTrip.Name = "buttonAddTrip"; buttonAddTrip.Size = new Size(94, 80); buttonAddTrip.TabIndex = 5; @@ -78,7 +78,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(1098, 538); Controls.Add(panel1); Controls.Add(dataGridView1); Name = "FormTrips"; diff --git a/ProjectTourismCompany/ProjectTourismCompany/Program.cs b/ProjectTourismCompany/ProjectTourismCompany/Program.cs index aeeef48..01abe29 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Program.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Program.cs @@ -3,6 +3,10 @@ using Unity; using ProjectTourismCompany.Repositories; using ProjectTourismCompany.Repositories.Implementations; using ProjectTourismCompany; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Serilog; +using Unity.Microsoft.Logging; internal static class Program { @@ -19,12 +23,28 @@ internal static class Program private static IUnityContainer CreateContainer() { var container = new UnityContainer(); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); container.RegisterType(new TransientLifetimeManager()); - container.RegisterType(new TransientLifetimeManager()); + container.RegisterType(); + + container.RegisterType(); 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; + } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/ProjectTourismCompany.csproj b/ProjectTourismCompany/ProjectTourismCompany/ProjectTourismCompany.csproj index accbdf0..a6536a5 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/ProjectTourismCompany.csproj +++ b/ProjectTourismCompany/ProjectTourismCompany/ProjectTourismCompany.csproj @@ -9,7 +9,18 @@ + + + + + + + + + + + @@ -27,4 +38,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/ICheckRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/ICheckRepository.cs index 957364f..7caf8e7 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/ICheckRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/ICheckRepository.cs @@ -9,9 +9,7 @@ namespace ProjectTourismCompany.Repositories; public interface ICheckRepository { - IEnumerable ReadChecks(DateTime? dateFrom = -null, DateTime? dateTo = null, -int? tripId = null, int? clientId = null); + IEnumerable ReadChecks(DateTime? dateFrom = null, DateTime? dateTo = null, int? tripId = null, int? clientId = null); Check ReadCheckById(int id); void CreateCheck(Check check); void DeleteCheck(int id); diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/IConnection.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/IConnection.cs new file mode 100644 index 0000000..62ea274 --- /dev/null +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/IConnection.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTourismCompany.Repositories; + +public interface IConnectionString +{ + public string ConnectionString { get;} +} diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs index 4d1dd34..59fcf97 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs @@ -1,4 +1,8 @@ -using Microsoft.VisualBasic.FileIO; +using Dapper; +using Microsoft.Extensions.Logging; +using Microsoft.VisualBasic.FileIO; +using Newtonsoft.Json; +using Npgsql; using ProjectTourismCompany.Entities; using System; using System.Collections.Generic; @@ -10,25 +14,91 @@ namespace ProjectTourismCompany.Repositories.Implementations; internal class CheckRepository : ICheckRepository { + private readonly IConnectionString _connectionString; + public ILogger _logger; + public CheckRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateCheck(Check check) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(check)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Receipt (Title, PurchaseDate, Price, TripId, ClientId) + VALUES (@Title, @PurchaseDate, @Price, @TripId, @ClientId)"; + connection.Execute(queryInsert, check); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteCheck(int id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Receipt + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadChecks(DateTime? dateFrom = null, DateTime? dateTo = null, int? tripId = null, int? clientId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Receipt"; + var expenses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(expenses)); + return expenses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Check ReadCheckById(int id) { - return Check.CreateCheck(0, "title", DateTime.Now, 500, 1, 2); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Receipt + WHERE Id=@id"; + var expense = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(expense)); + return expense; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ClientRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ClientRepository.cs index 086a418..a4e420e 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ClientRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ClientRepository.cs @@ -1,4 +1,8 @@ -using ProjectTourismCompany.Entities.Enums; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTourismCompany.Entities.Enums; using System; using System.Collections.Generic; using System.Linq; @@ -9,28 +13,114 @@ namespace ProjectTourismCompany.Repositories.Implementations; internal class ClientRepository : IClientRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public ClientRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + public void CreateClient(Client client) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Client (Name, BirthDate, Status, Phone) + VALUES (@Name, @BirthDate, @Status, @Phone)"; + connection.Execute(queryInsert, client); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteClient(int id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Client + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public Client ReadClientById(int id) { - return Client.CreateClient(0, string.Empty, DateTime.Now, ClientStatus.None, "1") ; + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Client + WHERE Id=@id"; + var expense = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(expense)); + return expense; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadClients() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Client"; + var expenses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(expenses)); + return expenses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateClient(Client client) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(client)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Client + SET + Name=@Name, + BirthDate=@BirthDate, + Status=@Status, + Phone=@Phone + WHERE Id=@Id"; + connection.Execute(queryUpdate, client); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs index 1c7f6c2..8b42691 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs @@ -1,4 +1,9 @@ -using ProjectTourismCompany.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTourismCompany.Entities; +using ProjectTourismCompany.Entities.Enums; using System; using System.Collections.Generic; using System.Linq; @@ -9,28 +14,145 @@ namespace ProjectTourismCompany.Repositories.Implementations; internal class CompanyRepository : ICompanyRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + + public CompanyRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + + public void CreateCompany(Company company) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(company)); + + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" + INSERT INTO Company (Name, CountryId) + VALUES (@Name, @CountryId); + SELECT MAX(Id) FROM Company"; + var companyId = + connection.QueryFirst(queryInsert, company, transaction); + var querySubInsert = @" + INSERT INTO CompanyTrip (CompanyId, TripId, AdditionalPrice) + VALUES (@CompanyId, @TripId, @AdditionalPrice)"; + foreach (var elem in company.CompanyTrip) + { + connection.Execute(querySubInsert, new + { + companyId, + elem.TripId, + elem.AdditionalPrice + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteCompany(int id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryDeleteSub = @" + DELETE FROM CompanyTrip + WHERE CompanyId = @id"; + connection.Execute(queryDeleteSub, new { id }, transaction); + + var queryDelete = @" + DELETE FROM Company + WHERE Id = @id"; + connection.Execute(queryDelete, new { id }, transaction); + + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } + + public IEnumerable ReadCompanies() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Company"; + var peopleExpenses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(peopleExpenses)); + return peopleExpenses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Company ReadCompanyById(int id) { - return Company.CreateCompany(1, "randomName", 73, []); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Company + WHERE Id=@id"; + var expense = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(expense)); + return expense; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public void UpdateCompany(Company company) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(company)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Company + SET + Name=@Name, + CountryId=@CountryId + WHERE Id=@Id"; + connection.Execute(queryUpdate, company); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ConnectionString.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ConnectionString.cs new file mode 100644 index 0000000..0311aed --- /dev/null +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/ConnectionString.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectTourismCompany.Repositories.Implementations; + +public class ConnectionString : IConnectionString +{ + string IConnectionString.ConnectionString => "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=postgres1"; +} diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CountryRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CountryRepository.cs index bfc7d47..33ab0e0 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CountryRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CountryRepository.cs @@ -1,4 +1,8 @@ -using ProjectTourismCompany.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTourismCompany.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -7,30 +11,114 @@ using System.Threading.Tasks; namespace ProjectTourismCompany.Repositories.Implementations; -internal class CountryRepository : ICountryRepository +public class CountryRepository : ICountryRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public CountryRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + + public void CreateCountry(Country country) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(country)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" + INSERT INTO Country (CountryName) + VALUES (@CountryName)"; + connection.Execute(queryInsert, country); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteCountry(int id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" + DELETE FROM Country + WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable ReadCountries() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Country"; + var expenses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(expenses)); + return expenses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public Country ReadCountryById(int id) { - return Country.CreateCountry(73, "Russia"); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Country + WHERE Id=@id"; + var expense = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(expense)); + return expense; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public void UpdateCountry(Country country) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(country)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Country + SET + CountryName=@CountryName + WHERE Id=@Id"; + connection.Execute(queryUpdate, country); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs index 6ec44e3..a5e5a47 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs @@ -1,4 +1,8 @@ -using ProjectTourismCompany.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProjectTourismCompany.Entities; using System; using System.Collections.Generic; using System.Linq; @@ -9,18 +13,72 @@ namespace ProjectTourismCompany.Repositories.Implementations; internal class TripRepository : ITripRepository { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + + public TripRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } + + public void CreateTrip(Trip trip) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(trip)); + + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryInsert = @" +INSERT INTO Trip (Title, Price, HumanCapacity, StartDate, EndDate, StartCity, EndCity, CompanyId, TravelType) +VALUES (@Title, @Price, @HumanCapacity, @StartDate, @EndDate, @StartCity, @EndCity, @CompanyId, @TravelType)"; + connection.Execute(queryInsert, trip); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public Trip ReadTripById(int id) { - return Trip.CreateOpeartion(0, "a", 0, 0, DateTime.Now, DateTime.Now, "Moscow", "Piter", Entities.Enums.TravelType.None); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" + SELECT * FROM Trip + WHERE Id=@id"; + var expense = connection.QueryFirst(querySelect, new { id }); + _logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(expense)); + return expense; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable ReadTrips(DateTime? dateForm = null, DateTime? dateTo = null, int? tripId = null, int? companyId = null, int? countryId = null) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM Trip"; + var expenses = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(expenses)); + return expenses; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/appsettings.json b/ProjectTourismCompany/ProjectTourismCompany/appsettings.json new file mode 100644 index 0000000..f5e2fe4 --- /dev/null +++ b/ProjectTourismCompany/ProjectTourismCompany/appsettings.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "Logs/tourism_log.txt", + "rollingInterval": "Day" + } + } + ] + } +} \ No newline at end of file -- 2.25.1 From 69d4f156d57901756926ad83102b65c1f867ea0d Mon Sep 17 00:00:00 2001 From: zw1st <144824777+zw1st@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:13:21 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=B0=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=20(?= =?UTF-8?q?=D0=B3=D0=BE=D0=BB)(=D0=BE=D0=BA=D0=BE=D0=BD=D1=87=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ProjectTourismCompany/Entities/Check.cs | 6 ++-- .../Entities/CompanyTrip.cs | 6 ++-- .../ProjectTourismCompany/Entities/Trip.cs | 1 - .../Forms/FormCheck.Designer.cs | 22 +++++++-------- .../ProjectTourismCompany/Forms/FormCheck.cs | 22 ++++++--------- .../Forms/FormChecks.Designer.cs | 4 +-- .../Forms/FormCompany.cs | 24 +++++++++++++--- .../Forms/FormTrip.Designer.cs | 3 -- .../ProjectTourismCompany/Forms/FormTrip.cs | 16 +++-------- .../Forms/FormTrips.Designer.cs | 1 + .../Implementations/CheckRepository.cs | 4 +-- .../Implementations/CompanyRepository.cs | 26 ++++------------- .../Implementations/TripRepository.cs | 28 +++++++++++++++---- 13 files changed, 83 insertions(+), 80 deletions(-) diff --git a/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs b/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs index 61642fa..3c7a9f8 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Entities/Check.cs @@ -9,13 +9,13 @@ namespace ProjectTourismCompany.Entities; public class Check { public int Id { get; private set; } - public string Title { get; private set; } = string.Empty; + //public string TripTitle { get; private set; } = string.Empty; public DateTime PurchaseDate { get; private set; } public int Price { get; private set; } public int TripId { get; private set; } public int ClientId { get; private set; } - public static Check CreateCheck(int id, DateTime purchaseDate, int price, int tripId, int clientId) + public static Check CreateCheck(int id, DateTime purchaseDate, int price, int tripId, int clientId)//string triptitle, { - return new Check() { Id = id, PurchaseDate = purchaseDate, Price = price, TripId = tripId, ClientId = clientId }; + return new Check() { Id = id, PurchaseDate = purchaseDate, Price = price, TripId = tripId, ClientId = clientId };//TripTitle = triptitle, } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Entities/CompanyTrip.cs b/ProjectTourismCompany/ProjectTourismCompany/Entities/CompanyTrip.cs index ac8d1dc..07b7d7d 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Entities/CompanyTrip.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Entities/CompanyTrip.cs @@ -10,15 +10,15 @@ public class CompanyTrip { public int Id { get; private set; } public int CompanyId { get; private set; } - public int TripId { get; private set; } + //public int TripId { get; private set; } public int AdditionalPrice { get; private set; } - public static CompanyTrip CreateCompanyTrip(int id, int companyId, int tripId, int additionalPrice) + public static CompanyTrip CreateCompanyTrip(int id, int companyId , int additionalPrice)//int tripId, { return new CompanyTrip() { Id = id, CompanyId = companyId, - TripId = tripId, + //TripId = tripId, AdditionalPrice = additionalPrice }; } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Entities/Trip.cs b/ProjectTourismCompany/ProjectTourismCompany/Entities/Trip.cs index e0d65ad..c17f65e 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Entities/Trip.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Entities/Trip.cs @@ -17,7 +17,6 @@ public class Trip public DateTime EndDate { get; private set;} public string StartCity { get; private set; }=string.Empty; public string EndCity { get; private set;} =string.Empty; - public int CompanyId { get; private set; } public TravelType TravelType { get; private set; } public IEnumerable CompanyTrip diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs index 44ed840..7fe3240 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.Designer.cs @@ -37,7 +37,7 @@ label2 = new Label(); label4 = new Label(); comboBoxClient = new ComboBox(); - comboBoxTrip = new ComboBox(); + comboBoxTripId = new ComboBox(); ((System.ComponentModel.ISupportInitialize)numericUpDownPrice).BeginInit(); SuspendLayout(); // @@ -125,21 +125,21 @@ comboBoxClient.Size = new Size(217, 28); comboBoxClient.TabIndex = 41; // - // comboBoxTrip + // comboBoxTripId // - comboBoxTrip.DropDownStyle = ComboBoxStyle.DropDownList; - comboBoxTrip.FormattingEnabled = true; - comboBoxTrip.Location = new Point(281, 155); - comboBoxTrip.Name = "comboBoxTrip"; - comboBoxTrip.Size = new Size(217, 28); - comboBoxTrip.TabIndex = 42; + comboBoxTripId.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxTripId.FormattingEnabled = true; + comboBoxTripId.Location = new Point(281, 155); + comboBoxTripId.Name = "comboBoxTripId"; + comboBoxTripId.Size = new Size(217, 28); + comboBoxTripId.TabIndex = 42; // // FormCheck // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(513, 272); - Controls.Add(comboBoxTrip); + ClientSize = new Size(516, 281); + Controls.Add(comboBoxTripId); Controls.Add(comboBoxClient); Controls.Add(label4); Controls.Add(label2); @@ -168,6 +168,6 @@ private Label label2; private Label label4; private ComboBox comboBoxClient; - private ComboBox comboBoxTrip; + private ComboBox comboBoxTripId; } } \ No newline at end of file diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs index 3f46743..68864ac 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCheck.cs @@ -32,11 +32,6 @@ public partial class FormCheck : Form } dateTimePickerPurchaseDate.Value = check.PurchaseDate; numericUpDownPrice.Value = check.Price; - - comboBoxTrip.SelectedItem = check.Title; - - - comboBoxTrip.SelectedItem = check.TripId; _checkId = value; } catch (Exception ex) @@ -54,24 +49,25 @@ public partial class FormCheck : Form comboBoxClient.DataSource = clientRepository.ReadClients(); comboBoxClient.DisplayMember = "Name"; comboBoxClient.ValueMember = "Id"; - - comboBoxTrip.DisplayMember = "Title"; - comboBoxTrip.ValueMember = "Id"; - comboBoxTrip.DataSource = tripRepository.ReadTrips(); + + comboBoxTripId.DataSource = tripRepository.ReadTrips(); + comboBoxTripId.DisplayMember = "Title"; + comboBoxTripId.ValueMember = "Id"; + } private void buttonSave_Click(object sender, EventArgs e) { try { - if (comboBoxTrip.SelectedIndex < 0 || + if (comboBoxTripId.SelectedIndex < 0 || comboBoxClient.SelectedIndex < 0) { throw new Exception("Имеются незаполненные поля"); } - _checkRepository.CreateCheck(Check.CreateCheck(_checkId, dateTimePickerPurchaseDate.Value, - (int)numericUpDownPrice.Value, (int)comboBoxTrip.SelectedIndex!, - (int)comboBoxClient.SelectedIndex!)); + _checkRepository.CreateCheck(Check.CreateCheck(_checkId , dateTimePickerPurchaseDate.Value, + (int)numericUpDownPrice.Value, (int)comboBoxTripId.SelectedValue!, + (int)comboBoxClient.SelectedValue!)); Close(); } catch (Exception ex) diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormChecks.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormChecks.Designer.cs index 711e716..6fe647c 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormChecks.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormChecks.Designer.cs @@ -52,7 +52,7 @@ dataGridViewChecks.RowHeadersVisible = false; dataGridViewChecks.RowHeadersWidth = 51; dataGridViewChecks.SelectionMode = DataGridViewSelectionMode.FullRowSelect; - dataGridViewChecks.Size = new Size(800, 450); + dataGridViewChecks.Size = new Size(643, 450); dataGridViewChecks.TabIndex = 0; // // panel1 @@ -92,8 +92,8 @@ AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); - Controls.Add(panel1); Controls.Add(dataGridViewChecks); + Controls.Add(panel1); Name = "FormChecks"; Text = "Чеки"; Load += FormChecks_Load; diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs index cd50c86..228e0d8 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormCompany.cs @@ -33,6 +33,7 @@ public partial class FormCompany : Form textBoxName.Text = client.Name; comboBoxCountry.SelectedItem = client.CountryId; + comboBoxCountry.SelectedValue = client.CountryId; _companyId = value; } catch (Exception ex) @@ -57,6 +58,7 @@ public partial class FormCompany : Form comboBoxCountry.DataSource = countryRepository.ReadCountries(); comboBoxCountry.DisplayMember = "CountryName"; comboBoxCountry.ValueMember = "Id"; + } @@ -64,11 +66,18 @@ public partial class FormCompany : Form { try { - if (string.IsNullOrEmpty(textBoxName.Text)) + if (string.IsNullOrWhiteSpace(textBoxName.Text)) { - throw new Exception("Имеются незаполненные поля"); + throw new Exception("Имеются незаполненные поля"); + } + if (_companyId.HasValue) + { + _companyRepository.UpdateCompany(CreateCompany(_companyId.Value)); + } + else + { + _companyRepository.CreateCompany(CreateCompany(0)); } - _companyRepository.CreateCompany(Company.CreateCompany(0, textBoxName.Text, (int)comboBoxCountry.SelectedItem!)); Close(); } catch (Exception ex) @@ -76,8 +85,15 @@ public partial class FormCompany : Form MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } + } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - + + + + private Company CreateCompany(int id) + { + return Company.CreateCompany(id, textBoxName.Text, (int)comboBoxCountry.SelectedValue!); + } } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.Designer.cs index 4520584..b8dc5e3 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.Designer.cs @@ -237,7 +237,6 @@ dataGridView1.Location = new Point(3, 23); dataGridView1.MultiSelect = false; dataGridView1.Name = "dataGridView1"; - dataGridView1.ReadOnly = true; dataGridView1.RowHeadersVisible = false; dataGridView1.RowHeadersWidth = 51; dataGridView1.Size = new Size(426, 450); @@ -248,14 +247,12 @@ columnCompanyName.HeaderText = "Название компании"; columnCompanyName.MinimumWidth = 6; columnCompanyName.Name = "columnCompanyName"; - columnCompanyName.ReadOnly = true; // // ColumnAdditionalPrice // ColumnAdditionalPrice.HeaderText = "Добавочная стоимость"; ColumnAdditionalPrice.MinimumWidth = 6; ColumnAdditionalPrice.Name = "ColumnAdditionalPrice"; - ColumnAdditionalPrice.ReadOnly = true; ColumnAdditionalPrice.Resizable = DataGridViewTriState.True; ColumnAdditionalPrice.SortMode = DataGridViewColumnSortMode.NotSortable; // diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs index 3466669..fa11957 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrip.cs @@ -74,19 +74,11 @@ Enum.GetValues(typeof(TravelType))) try { if (string.IsNullOrWhiteSpace(textBoxStartCity.Text) || - string.IsNullOrWhiteSpace(textBoxEndCity.Text) || dataGridView1.RowCount < 1) + string.IsNullOrWhiteSpace(textBoxEndCity.Text) || dataGridView1.RowCount < 1 || dateTimePickerStartDate.Value > dateTimePickerEndDate.Value) { - throw new Exception("Имеются незаполненные поля"); - } - - if (_tripId.HasValue) - { - _tripRepository.CreateTrip(CreateTrip(_tripId.Value)); - } - else - { - _tripRepository.CreateTrip(CreateTrip(0)); + throw new Exception("Имеются незаполненные или некорректно заполненные поля"); } + _tripRepository.CreateTrip(CreateTrip(0)); Close(); } @@ -122,7 +114,7 @@ Enum.GetValues(typeof(TravelType))) { continue; } - list.Add(CompanyTrip.CreateCompanyTrip(0, 0, + list.Add(CompanyTrip.CreateCompanyTrip(0, Convert.ToInt32(row.Cells["ColumnCompanyName"].Value), Convert.ToInt32(row.Cells["ColumnAdditionalPrice"].Value))); } diff --git a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs index e57b191..9323bd9 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Forms/FormTrips.Designer.cs @@ -48,6 +48,7 @@ dataGridView1.MultiSelect = false; dataGridView1.Name = "dataGridView1"; dataGridView1.ReadOnly = true; + dataGridView1.RowHeadersVisible = false; dataGridView1.RowHeadersWidth = 51; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.Size = new Size(645, 450); diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs index 59fcf97..0e5da6e 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CheckRepository.cs @@ -33,8 +33,8 @@ internal class CheckRepository : ICheckRepository { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var queryInsert = @" - INSERT INTO Receipt (Title, PurchaseDate, Price, TripId, ClientId) - VALUES (@Title, @PurchaseDate, @Price, @TripId, @ClientId)"; + INSERT INTO Receipt (PurchaseDate, Price, TripId, ClientId) + VALUES (@PurchaseDate, @Price, @TripId, @ClientId)"; connection.Execute(queryInsert, check); } catch (Exception ex) diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs index 8b42691..7e05dad 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/CompanyRepository.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Unity; namespace ProjectTourismCompany.Repositories.Implementations; @@ -33,35 +34,18 @@ internal class CompanyRepository : ICompanyRepository try { - using var connection = new - NpgsqlConnection(_connectionString.ConnectionString); - connection.Open(); - using var transaction = connection.BeginTransaction(); + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var queryInsert = @" INSERT INTO Company (Name, CountryId) - VALUES (@Name, @CountryId); - SELECT MAX(Id) FROM Company"; - var companyId = - connection.QueryFirst(queryInsert, company, transaction); - var querySubInsert = @" - INSERT INTO CompanyTrip (CompanyId, TripId, AdditionalPrice) - VALUES (@CompanyId, @TripId, @AdditionalPrice)"; - foreach (var elem in company.CompanyTrip) - { - connection.Execute(querySubInsert, new - { - companyId, - elem.TripId, - elem.AdditionalPrice - }, transaction); - } - transaction.Commit(); + VALUES (@Name, @CountryId)"; + connection.Execute(queryInsert, company); } catch (Exception ex) { _logger.LogError(ex, "Ошибка при добавлении объекта"); throw; } + } public void DeleteCompany(int id) diff --git a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs index a5e5a47..b1dd538 100644 --- a/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs +++ b/ProjectTourismCompany/ProjectTourismCompany/Repositories/Implementations/TripRepository.cs @@ -27,20 +27,38 @@ internal class TripRepository : ITripRepository { _logger.LogInformation("Добавление объекта"); _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(trip)); - try { - using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); var queryInsert = @" -INSERT INTO Trip (Title, Price, HumanCapacity, StartDate, EndDate, StartCity, EndCity, CompanyId, TravelType) -VALUES (@Title, @Price, @HumanCapacity, @StartDate, @EndDate, @StartCity, @EndCity, @CompanyId, @TravelType)"; - connection.Execute(queryInsert, trip); + INSERT INTO Trip (Title, Price, HumanCapacity, StartDate, EndDate, StartCity, EndCity, TravelType) + VALUES (@Title, @Price, @HumanCapacity, @StartDate, @EndDate, @StartCity, @EndCity, @TravelType); + SELECT MAX(Id) FROM Trip"; + var companyTripId = + connection.QueryFirst(queryInsert, trip, transaction); + var querySubInsert = @" + INSERT INTO CompanyTrip (CompanyId, AdditionalPrice) + VALUES (@CompanyId, @AdditionalPrice)"; + foreach (var elem in trip.CompanyTrip) + { + connection.Execute(querySubInsert, new + { + companyTripId, + elem.CompanyId, + elem.AdditionalPrice + }, transaction); + } + transaction.Commit(); } catch (Exception ex) { _logger.LogError(ex, "Ошибка при добавлении объекта"); throw; } + } public Trip ReadTripById(int id) -- 2.25.1