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