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] =?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)