lab2 done

This commit is contained in:
pnevmoslon1 2024-12-09 06:05:57 +04:00
parent 1a50d8d5c4
commit adc93488c9
8 changed files with 52 additions and 30 deletions

View File

@ -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
};
}

View File

@ -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
};

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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);

View File

@ -50,14 +50,13 @@ public partial class FormPlacingAthlete : Form
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private List<AthletePlacingAthlete>
CreateListAthletePlacingAthleteFromDataGrid()
private List<AthletePlacingAthlete> CreateListAthletePlacingAthleteFromDataGrid()
{
var list = new List<AthletePlacingAthlete>();
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;
}

View File

@ -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("Удаление объекта");

View File

@ -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<int>(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)