diff --git a/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs b/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs index c715d0f..58d04d8 100644 --- a/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs +++ b/ProjectHotel/ProjectHotel/Entities/AthletePlacingAthlete.cs @@ -10,6 +10,7 @@ public class AthletePlacingAthlete { public int Id { get; set; } public int AthleteId { get; private set; } + public int PlacingAthleteId{ get; private set; } public int LengthOfStay { get; private set; } public static AthletePlacingAthlete CreateElement(int id, int athleteId, int lengthOfStay) { @@ -17,7 +18,7 @@ public class AthletePlacingAthlete { Id = id, AthleteId = athleteId, - LengthOfStay = lengthOfStay + LengthOfStay = lengthOfStay }; } diff --git a/ProjectHotel/ProjectHotel/Entities/Hotel.cs b/ProjectHotel/ProjectHotel/Entities/Hotel.cs index 5425230..573c8ea 100644 --- a/ProjectHotel/ProjectHotel/Entities/Hotel.cs +++ b/ProjectHotel/ProjectHotel/Entities/Hotel.cs @@ -10,16 +10,16 @@ public class Hotel { public int Id { get; private set; } public string Name { get; private set; } = string.Empty; - public string Adress { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; - public static Hotel CreateEntity(int id, string name, string adress) + public static Hotel CreateEntity(int id, string name, string address) { return new Hotel { Id = id, Name = name, - Adress = adress + Address = address }; diff --git a/ProjectHotel/ProjectHotel/Forms/FormHotel.Designer.cs b/ProjectHotel/ProjectHotel/Forms/FormHotel.Designer.cs index 8fbb75a..a9203b7 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormHotel.Designer.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormHotel.Designer.cs @@ -111,9 +111,9 @@ private Label label1; private Label label2; private TextBox textBoxHotelName; - private TextBox textBoxHotelAdress; + private TextBox textBoxHotelAddress; private Button buttonSave; private Button buttonCancel; - private TextBox textBoxHotelAddress; + } } \ No newline at end of file diff --git a/ProjectHotel/ProjectHotel/Forms/FormHotel.cs b/ProjectHotel/ProjectHotel/Forms/FormHotel.cs index eb664df..c47406e 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormHotel.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormHotel.cs @@ -31,8 +31,8 @@ public partial class FormHotel : Form InvalidDataException(nameof(hotel)); } textBoxHotelName.Text = hotel.Name; - textBoxHotelAdress.Text = - hotel.Adress; + textBoxHotelAddress.Text = + hotel.Address; _hotelId = value; } @@ -77,8 +77,8 @@ public partial class FormHotel : Form } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - private Hotel CreateHotel(int id) => Hotel.CreateEntity(id, textBoxHotelName.Text, textBoxHotelAdress.Text); + private Hotel CreateHotel(int id) => Hotel.CreateEntity(id, textBoxHotelName.Text, textBoxHotelAddress.Text); - + } diff --git a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.Designer.cs b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.Designer.cs index 8be2f83..c4de78b 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.Designer.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.Designer.cs @@ -36,8 +36,8 @@ comboBoxRoom = new ComboBox(); groupBox1 = new GroupBox(); dataGridView = new DataGridView(); - ColumnAthlete = new DataGridViewComboBoxColumn(); ColumnLengthOfStay = new DataGridViewTextBoxColumn(); + ColumnAthlete = new DataGridViewComboBoxColumn(); groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -119,18 +119,18 @@ dataGridView.Size = new Size(485, 307); dataGridView.TabIndex = 0; // + // ColumnLengthOfStay + // + ColumnLengthOfStay.HeaderText = "Длительность проживания"; + ColumnLengthOfStay.Name = "ColumnLengthOfStay"; + // // ColumnAthlete // ColumnAthlete.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; ColumnAthlete.HeaderText = "Спортсмен"; ColumnAthlete.Name = "ColumnAthlete"; // - // ColumnLengthOfStay - // - ColumnLengthOfStay.HeaderText = "Длительность проживания"; - ColumnLengthOfStay.Name = "ColumnLengthOfStay"; - // - // FormAthletePlacingAthlete + // FormPlacingAthlete // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; @@ -141,7 +141,7 @@ Controls.Add(buttonSave); Controls.Add(label2); Controls.Add(label1); - Name = "FormAthletePlacingAthlete"; + Name = "FormPlacingAthlete"; StartPosition = FormStartPosition.CenterParent; Text = "Размещение спортсмена"; groupBox1.ResumeLayout(false); diff --git a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs index 8c7ef6a..c27d32b 100644 --- a/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs +++ b/ProjectHotel/ProjectHotel/Forms/FormPlacingAthlete.cs @@ -50,14 +50,13 @@ public partial class FormPlacingAthlete : Form } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); - private List - CreateListAthletePlacingAthleteFromDataGrid() + private List CreateListAthletePlacingAthleteFromDataGrid() { var list = new List(); foreach (DataGridViewRow row in dataGridView.Rows) { - if (row.Cells["ColumnFeed"].Value == null || - row.Cells["ColumnCount"].Value == null) + if (row.Cells["ColumnAthlete"].Value == null || + row.Cells["ColumnLengthOfStay"].Value == null) { continue; } diff --git a/ProjectHotel/ProjectHotel/Repositories/Implementations/HotelRepository.cs b/ProjectHotel/ProjectHotel/Repositories/Implementations/HotelRepository.cs index 4ab5205..49408bf 100644 --- a/ProjectHotel/ProjectHotel/Repositories/Implementations/HotelRepository.cs +++ b/ProjectHotel/ProjectHotel/Repositories/Implementations/HotelRepository.cs @@ -84,8 +84,26 @@ internal class HotelRepository : IHotelRepository } public void UpdateHotel(Hotel hotel) { + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(hotel)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" + UPDATE Hotels + SET Name = @Name, Address = @Address + WHERE Id = @Id; + "; + connection.Execute(queryUpdate, hotel); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } + public void DeleteHotel(int id) { _logger.LogInformation("Удаление объекта"); diff --git a/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs b/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs index ca03a95..b660e12 100644 --- a/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs +++ b/ProjectHotel/ProjectHotel/Repositories/Implementations/PlacingAthleteRepository.cs @@ -6,6 +6,7 @@ using ProjectHotel.Entities; using System; using System.Collections.Generic; using System.Data.SqlClient; +using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -32,20 +33,23 @@ internal class PlacingAthleteRepository : IPlacingAthleteRepository using var connection = new NpgsqlConnection(_connectionString.ConnectionString); connection.Open(); using var transaction = connection.BeginTransaction(); - var queryInsert = @"INSERT INTO PlacingAthlete (RoomId, PlacingDate) VALUES (@RoomId, @PlacingDate) RETURNING Id"; + var queryInsert = @" + INSERT INTO PlacingAthlete (RoomId, PlacingDate) + VALUES (@RoomId, @PlacingDate); + SELECT MAX(Id) FROM PlacingAthlete; + "; var placingAthleteId = connection.QueryFirst(queryInsert, placingAthlete, transaction); - var querySubInsert = @"INSERT INTO AthletePlacingAthlete (PlacingAthleteId, AthleteId, LengthOfStay) VALUES (@PlacingAthleteId, @AthleteId, @LengthOfStay)"; + var querySubInsert = @" + INSERT INTO AthletePlacingAthlete (AthleteId, PlacingAthleteId, LengthOfStay) + VALUES (@AthleteId, @PlacingAthleteId, @LengthOfStay); + "; foreach (var elem in placingAthlete.AthletePlacingAthletes) { - connection.Execute(querySubInsert, new - { - PlacingAthleteId = placingAthleteId, - elem.AthleteId, - elem.LengthOfStay, - }, transaction); + connection.Execute(querySubInsert, new { elem.AthleteId, placingAthleteId , elem.LengthOfStay }, transaction); } + transaction.Commit(); } catch (Exception ex)