Фаза 2 + исправления
This commit is contained in:
parent
3f6b1ca250
commit
8c000bd30a
@ -16,15 +16,15 @@ public class Flight
|
||||
public string DeparturePoint { get; private set; } = string.Empty;
|
||||
public int TicketPrice { get; private set; }
|
||||
public IEnumerable<EmployeeFlight> EmployeeFlights { get; private set; } = [];
|
||||
public static Flight CreateOperation(int id, int airPlaneID, string destination, string departurePoint, int ticketPrice,
|
||||
public static Flight CreateOperation(int id, int airPlaneID, DateTime dateTimeDep, DateTime dateTimeArr, string destination, string departurePoint, int ticketPrice,
|
||||
IEnumerable<EmployeeFlight> employeeFlights)
|
||||
{
|
||||
return new Flight
|
||||
{
|
||||
Id = id,
|
||||
AirplaneID = airPlaneID,
|
||||
DepartureTime = DateTime.Now,
|
||||
ArrivalTime = DateTime.Now,
|
||||
DepartureTime = dateTimeDep,
|
||||
ArrivalTime = dateTimeArr,
|
||||
Destination = destination ?? string.Empty,
|
||||
DeparturePoint = departurePoint ?? string.Empty,
|
||||
TicketPrice = ticketPrice,
|
||||
|
@ -14,7 +14,7 @@ public class Passenger
|
||||
public string FirstName { get; private set; } = string.Empty;
|
||||
public string LastName { get; private set; } = string.Empty;
|
||||
public DateTime DateBirth { get; private set; }
|
||||
public static Passenger CreateEntity (int id, FoodPreferences foodPreferences, string first, string last)
|
||||
public static Passenger CreateEntity (int id, FoodPreferences foodPreferences, string first, string last, DateTime dateTime)
|
||||
{
|
||||
return new Passenger
|
||||
{
|
||||
@ -22,7 +22,7 @@ public class Passenger
|
||||
FoodPreferences = foodPreferences,
|
||||
FirstName = first ?? string.Empty,
|
||||
LastName = last ?? string.Empty,
|
||||
DateBirth = DateTime.Now
|
||||
DateBirth = dateTime
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ public class Ticket
|
||||
|
||||
public int TicketPrice { get; private set; }
|
||||
public DateTime DateBuy { get; private set; }
|
||||
public static Ticket CreateTicket (int id, int flightID, int passengerID, int ticketPrice)
|
||||
public static Ticket CreateTicket (int id, int flightID, int passengerID, int ticketPrice, DateTime dateTimeBuy)
|
||||
{
|
||||
return new Ticket
|
||||
{
|
||||
@ -22,7 +22,7 @@ public class Ticket
|
||||
FlightID = flightID,
|
||||
PassengerID = passengerID,
|
||||
TicketPrice = ticketPrice,
|
||||
DateBuy = DateTime.Now
|
||||
DateBuy = dateTimeBuy
|
||||
};
|
||||
}
|
||||
|
||||
|
1
ProjectAirline/Forms/FormAirplane.Designer.cs
generated
1
ProjectAirline/Forms/FormAirplane.Designer.cs
generated
@ -83,6 +83,7 @@
|
||||
// numericUpDownCapacity
|
||||
//
|
||||
numericUpDownCapacity.Location = new Point(155, 165);
|
||||
numericUpDownCapacity.Maximum = new decimal(new int[] { 1000, 0, 0, 0 });
|
||||
numericUpDownCapacity.Name = "numericUpDownCapacity";
|
||||
numericUpDownCapacity.Size = new Size(150, 27);
|
||||
numericUpDownCapacity.TabIndex = 5;
|
||||
|
1
ProjectAirline/Forms/FormFlight.Designer.cs
generated
1
ProjectAirline/Forms/FormFlight.Designer.cs
generated
@ -132,6 +132,7 @@
|
||||
// numericUpDownPrice
|
||||
//
|
||||
numericUpDownPrice.Location = new Point(183, 274);
|
||||
numericUpDownPrice.Maximum = new decimal(new int[] { 100000, 0, 0, 0 });
|
||||
numericUpDownPrice.Name = "numericUpDownPrice";
|
||||
numericUpDownPrice.Size = new Size(250, 27);
|
||||
numericUpDownPrice.TabIndex = 9;
|
||||
|
@ -39,6 +39,7 @@ namespace ProjectAirline.Forms
|
||||
try
|
||||
{
|
||||
_flightRepository.CreateFlight(Flight.CreateOperation(0, (int)comboBoxAirplane.SelectedValue,
|
||||
dateTimePickerDeparture.Value, dateTimePickerArrival.Value,
|
||||
textBoxDestination.Text, textBoxDeparture.Text,
|
||||
(int)numericUpDownPrice.Value, CreateListFlightFromDataGrid()));
|
||||
Close();
|
||||
|
@ -104,6 +104,6 @@ public partial class FormPassenger : Form
|
||||
foodPreferences |= (FoodPreferences)elem;
|
||||
}
|
||||
|
||||
return Passenger.CreateEntity(id, foodPreferences, textBoxFirstname.Text, textBoxLastname.Text);
|
||||
return Passenger.CreateEntity(id, foodPreferences, textBoxFirstname.Text, textBoxLastname.Text, dateTimePickerBorn.Value);
|
||||
}
|
||||
}
|
||||
|
22
ProjectAirline/Forms/FormTicket.Designer.cs
generated
22
ProjectAirline/Forms/FormTicket.Designer.cs
generated
@ -36,6 +36,8 @@
|
||||
numericUpDownPrice = new NumericUpDown();
|
||||
buttonAdd = new Button();
|
||||
buttonCancel = new Button();
|
||||
label4 = new Label();
|
||||
dateTimePickerBuy = new DateTimePicker();
|
||||
((System.ComponentModel.ISupportInitialize)numericUpDownPrice).BeginInit();
|
||||
SuspendLayout();
|
||||
//
|
||||
@ -115,11 +117,29 @@
|
||||
buttonCancel.UseVisualStyleBackColor = true;
|
||||
buttonCancel.Click += ButtonCancel_Click;
|
||||
//
|
||||
// label4
|
||||
//
|
||||
label4.AutoSize = true;
|
||||
label4.Location = new Point(34, 267);
|
||||
label4.Name = "label4";
|
||||
label4.Size = new Size(102, 20);
|
||||
label4.TabIndex = 8;
|
||||
label4.Text = "Дата покупки";
|
||||
//
|
||||
// dateTimePickerBuy
|
||||
//
|
||||
dateTimePickerBuy.Location = new Point(163, 267);
|
||||
dateTimePickerBuy.Name = "dateTimePickerBuy";
|
||||
dateTimePickerBuy.Size = new Size(226, 27);
|
||||
dateTimePickerBuy.TabIndex = 9;
|
||||
//
|
||||
// FormTicket
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(440, 450);
|
||||
Controls.Add(dateTimePickerBuy);
|
||||
Controls.Add(label4);
|
||||
Controls.Add(buttonCancel);
|
||||
Controls.Add(buttonAdd);
|
||||
Controls.Add(numericUpDownPrice);
|
||||
@ -145,5 +165,7 @@
|
||||
private NumericUpDown numericUpDownPrice;
|
||||
private Button buttonAdd;
|
||||
private Button buttonCancel;
|
||||
private Label label4;
|
||||
private DateTimePicker dateTimePickerBuy;
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using ProjectAirline.Repositories;
|
||||
using ProjectAirline.Entities;
|
||||
using ProjectAirline.Repositories.Implementations;
|
||||
|
||||
namespace ProjectAirline.Forms
|
||||
{
|
||||
@ -16,30 +17,7 @@ namespace ProjectAirline.Forms
|
||||
{
|
||||
private readonly ITicketRepository _ticketRepository;
|
||||
private int? _ticketId;
|
||||
public int Id
|
||||
{
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
var appointment =
|
||||
_ticketRepository.ReadTicketById(value);
|
||||
if (appointment == null)
|
||||
{
|
||||
throw new
|
||||
InvalidDataException(nameof(appointment));
|
||||
}
|
||||
comboBoxFlight.SelectedIndex = appointment.FlightID;
|
||||
comboBoxPassenger.SelectedIndex = appointment.PassengerID;
|
||||
_ticketId = value;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FormTicket(ITicketRepository ticketRepository, IPassengerRepository passengerRepository, IFlightRepository flightRepository)
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -64,14 +42,10 @@ namespace ProjectAirline.Forms
|
||||
{
|
||||
throw new Exception("Имеются незаполненны поля");
|
||||
}
|
||||
if (_ticketId.HasValue)
|
||||
{
|
||||
_ticketRepository.UpdateTicket(CreateTicket(_ticketId.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
_ticketRepository.CreateTicket(CreateTicket(0));
|
||||
}
|
||||
|
||||
_ticketRepository.CreateTicket(Ticket.CreateTicket(0,
|
||||
(int)comboBoxFlight.SelectedValue!, (int)comboBoxPassenger.SelectedValue!,
|
||||
Convert.ToInt32(numericUpDownPrice.Value), dateTimePickerBuy.Value));
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -85,7 +59,5 @@ namespace ProjectAirline.Forms
|
||||
{
|
||||
Close();
|
||||
}
|
||||
private Ticket CreateTicket(int id) => Ticket.CreateTicket(id, comboBoxFlight.SelectedIndex,
|
||||
comboBoxPassenger.SelectedIndex, (int)numericUpDownPrice.Value);
|
||||
}
|
||||
}
|
||||
|
14
ProjectAirline/Forms/FormTickets.Designer.cs
generated
14
ProjectAirline/Forms/FormTickets.Designer.cs
generated
@ -29,7 +29,6 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
panel1 = new Panel();
|
||||
buttonUpdate = new Button();
|
||||
buttonAdd = new Button();
|
||||
dataGridView = new DataGridView();
|
||||
panel1.SuspendLayout();
|
||||
@ -38,7 +37,6 @@
|
||||
//
|
||||
// panel1
|
||||
//
|
||||
panel1.Controls.Add(buttonUpdate);
|
||||
panel1.Controls.Add(buttonAdd);
|
||||
panel1.Dock = DockStyle.Right;
|
||||
panel1.Location = new Point(658, 0);
|
||||
@ -46,17 +44,6 @@
|
||||
panel1.Size = new Size(142, 450);
|
||||
panel1.TabIndex = 0;
|
||||
//
|
||||
// buttonUpdate
|
||||
//
|
||||
buttonUpdate.BackgroundImage = Properties.Resources.edit;
|
||||
buttonUpdate.BackgroundImageLayout = ImageLayout.Stretch;
|
||||
buttonUpdate.Location = new Point(24, 148);
|
||||
buttonUpdate.Name = "buttonUpdate";
|
||||
buttonUpdate.Size = new Size(91, 76);
|
||||
buttonUpdate.TabIndex = 1;
|
||||
buttonUpdate.UseVisualStyleBackColor = true;
|
||||
buttonUpdate.Click += ButtonUpdate_Click;
|
||||
//
|
||||
// buttonAdd
|
||||
//
|
||||
buttonAdd.BackgroundImage = Properties.Resources.plus;
|
||||
@ -106,7 +93,6 @@
|
||||
#endregion
|
||||
|
||||
private Panel panel1;
|
||||
private Button buttonUpdate;
|
||||
private Button buttonAdd;
|
||||
private DataGridView dataGridView;
|
||||
}
|
||||
|
@ -48,25 +48,7 @@ namespace ProjectAirline.Forms
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonUpdate_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
var form = _container.Resolve<FormTicket>();
|
||||
form.Id = findId;
|
||||
form.ShowDialog();
|
||||
LoadList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, "Ошибка при изменении",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadList() => dataGridView.DataSource = _ticketRepository.ReadTickets();
|
||||
private bool TryGetIdentifierFromSelectedRow(out int id)
|
||||
{
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectAirline.Repositories.Implementations;
|
||||
using ProjectAirline.Repositories;
|
||||
using Unity;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using Unity.Microsoft.Logging;
|
||||
using Unity.Lifetime;
|
||||
namespace ProjectAirline
|
||||
{
|
||||
internal static class Program
|
||||
@ -20,14 +25,31 @@ namespace ProjectAirline
|
||||
private static IUnityContainer CreateContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.RegisterType<IFlightRepository, FlightRepository>();
|
||||
container.RegisterType<IPassengerRepository, PassengerRepository>();
|
||||
container.RegisterType<IAirplaneRepository, AirplaneRepository>();
|
||||
container.RegisterType<IEmployeeFlightRepository, EmployeeFlightRepository>();
|
||||
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
|
||||
container.RegisterType<ITicketRepository, TicketRepository>();
|
||||
container.AddExtension(new LoggingExtension(CreateLoggerFactory()));
|
||||
|
||||
container.RegisterType<IConnectionString, ConnectionString>(new SingletonLifetimeManager());
|
||||
|
||||
container.RegisterType<IFlightRepository, FlightRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IPassengerRepository, PassengerRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IAirplaneRepository, AirplaneRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IEmployeeFlightRepository, EmployeeFlightRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<IEmployeeRepository, EmployeeRepository>(new TransientLifetimeManager());
|
||||
container.RegisterType<ITicketRepository, TicketRepository>(new TransientLifetimeManager());
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,19 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.35" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Npgsql" Version="8.0.5" />
|
||||
<PackageReference Include="Serilog" Version="4.0.2" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
|
||||
<PackageReference Include="Unity" Version="5.11.10" />
|
||||
<PackageReference Include="Unity.Microsoft.Logging" Version="5.11.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@ -27,4 +39,10 @@
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
12
ProjectAirline/Repositories/IConnectionString.cs
Normal file
12
ProjectAirline/Repositories/IConnectionString.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectAirline.Repositories;
|
||||
|
||||
public interface IConnectionString
|
||||
{
|
||||
public string ConnectionString { get; }
|
||||
}
|
@ -10,7 +10,6 @@ namespace ProjectAirline.Repositories;
|
||||
public interface IFlightRepository
|
||||
{
|
||||
IEnumerable<Flight> ReadFlights(DateTime? dateForm = null, DateTime? dateTo = null, int? flightId = null, int? airplaneId = null);
|
||||
Flight ReadFlightById(int flightId);
|
||||
void CreateFlight(Flight flight);
|
||||
void DeleteFlight(int id);
|
||||
}
|
||||
|
@ -10,7 +10,5 @@ namespace ProjectAirline.Repositories;
|
||||
public interface ITicketRepository
|
||||
{
|
||||
IEnumerable<Ticket> ReadTickets();
|
||||
Ticket ReadTicketById(int id);
|
||||
void CreateTicket(Ticket ticket);
|
||||
void UpdateTicket(Ticket ticket);
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectAirline.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAirline.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,26 +14,125 @@ namespace ProjectAirline.Repositories.Implementations;
|
||||
|
||||
public class AirplaneRepository : IAirplaneRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
|
||||
private readonly ILogger<AirplaneRepository> _logger;
|
||||
|
||||
public AirplaneRepository(IConnectionString connectionString, ILogger<AirplaneRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public void CreateAirplane(Airplane airplane)
|
||||
{
|
||||
}
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(airplane));
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"INSERT INTO Airplane (Country, Model, Capacity)
|
||||
VALUES (@Country, @Model, @Capacity)";
|
||||
connection.Execute(queryInsert, airplane);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public void UpdateAirplane(Airplane airplane)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(airplane));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Airplane
|
||||
SET
|
||||
Country=@Country,
|
||||
Model=@Model,
|
||||
Capacity=@Capacity
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, airplane);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeleteAirplane(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Airplane
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Airplane ReadAirplaneById(int id)
|
||||
{
|
||||
return Airplane.CreateEntity(0, string.Empty, string.Empty, 0);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Airplane
|
||||
WHERE Id=@id";
|
||||
var airplane = connection.QueryFirst<Airplane>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(airplane));
|
||||
return airplane;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IEnumerable<Airplane> ReadAirplanes()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Airplane";
|
||||
var airplanes = connection.Query<Airplane>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(airplanes));
|
||||
return airplanes;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void UpdateAirplane(Airplane airplane)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectAirline.Repositories.Implementations;
|
||||
|
||||
internal class ConnectionString : IConnectionString
|
||||
{
|
||||
string IConnectionString.ConnectionString => "Server=localhost;Port=5432;Database=airlines;Username=postgres;Password=postgres;";
|
||||
}
|
@ -1,7 +1,12 @@
|
||||
using ProjectAirline.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAirline.Entities;
|
||||
using ProjectAirline.Entities.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -10,25 +15,125 @@ namespace ProjectAirline.Repositories.Implementations;
|
||||
|
||||
public class EmployeeRepository : IEmployeeRepository
|
||||
{
|
||||
public void CreateEmployee(Employee employee)
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<EmployeeRepository> _logger;
|
||||
|
||||
public EmployeeRepository(IConnectionString connectionString, ILogger<EmployeeRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateEmployee(Employee employee)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(employee));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Employee (FirstName, LastName, ContactInformation, EmployeePost)
|
||||
VALUES (@FirstName, @LastName, @ContactInformation, @EmployeePost)";
|
||||
connection.Execute(queryInsert, employee);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void UpdateEmployee(Employee employee)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(employee));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Employee
|
||||
SET
|
||||
FirstName=@FirstName,
|
||||
LastName=@LastName,
|
||||
ContactInformation=@ContactInformation,
|
||||
EmployeePost=@EmployeePost
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, employee);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public void DeleteEmployee(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Employee
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Employee ReadEmployeeById(int id)
|
||||
{
|
||||
return Employee.CreateEntity(0, string.Empty, string.Empty, string.Empty, EmployeePost.None);
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Employee
|
||||
WHERE Id=@id";
|
||||
var employee = connection.QueryFirst<Employee>(querySelect,
|
||||
new { id });
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(employee));
|
||||
return employee;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Employee> ReadEmployees()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Employee";
|
||||
var employees = connection.Query<Employee>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(employees));
|
||||
return employees;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateEmployee(Employee employee)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectAirline.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAirline.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,21 +14,94 @@ namespace ProjectAirline.Repositories.Implementations;
|
||||
|
||||
public class FlightRepository : IFlightRepository
|
||||
{
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<FlightRepository> _logger;
|
||||
public FlightRepository(IConnectionString connectionString,
|
||||
ILogger<FlightRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreateFlight(Flight flight)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(flight));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
connection.Open();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
var queryInsert = @"
|
||||
INSERT INTO Flight (AirplaneID, DepartureTime, ArrivalTime, Destination, DeparturePoint, TicketPrice)
|
||||
VALUES (@AirplaneID, @DepartureTime, @ArrivalTime, @Destination, @DeparturePoint, @TicketPrice);
|
||||
SELECT MAX(Id) FROM Flight";
|
||||
var flightId =
|
||||
connection.QueryFirst<int>(queryInsert, flight, transaction);
|
||||
var querySubInsert = @"
|
||||
INSERT INTO EmployeeFlights (EmployeeId, FlightId, HoursWorked)
|
||||
VALUES (@EmployeeId,@FlightId, @HoursWorked)";
|
||||
foreach (var elem in flight.EmployeeFlights)
|
||||
{
|
||||
connection.Execute(querySubInsert, new
|
||||
{
|
||||
elem.EmployeeId,
|
||||
flightId,
|
||||
elem.HoursWorked
|
||||
}, transaction);
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteFlight(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Flight
|
||||
WHERE ID = @id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Flight ReadFlightById(int flightId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public IEnumerable<Flight> ReadFlights(DateTime? dateForm = null, DateTime? dateTo = null, int? flightId = null, int? airplaneId = null)
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Flight";
|
||||
var flights =
|
||||
connection.Query<Flight>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(flights));
|
||||
return flights;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectAirline.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAirline.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,25 +14,126 @@ namespace ProjectAirline.Repositories.Implementations;
|
||||
|
||||
public class PassengerRepository : IPassengerRepository
|
||||
{
|
||||
public void CreatePassenger(Passenger passenger)
|
||||
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<PassengerRepository> _logger;
|
||||
public PassengerRepository(IConnectionString connectionString,
|
||||
ILogger<PassengerRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void CreatePassenger(Passenger passenger)
|
||||
{
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(passenger));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Passenger (FoodPreferences, FirstName, LastName, DateBirth)
|
||||
VALUES (@FoodPreferences, @FirstName, @LastName, @DateBirth)";
|
||||
connection.Execute(queryInsert, passenger);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void UpdatePassenger(Passenger passenger)
|
||||
{
|
||||
_logger.LogInformation("Редактирование объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(passenger));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryUpdate = @"
|
||||
UPDATE Passenger
|
||||
SET
|
||||
FoodPreferences=@FoodPreferences,
|
||||
FirstName=@FirstName,
|
||||
LastName=@LastName,
|
||||
DateBirth=@DateBirth
|
||||
WHERE Id=@Id";
|
||||
connection.Execute(queryUpdate, passenger);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при редактировании объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void DeletePassenger(int id)
|
||||
{
|
||||
_logger.LogInformation("Удаление объекта");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryDelete = @"
|
||||
DELETE FROM Passenger
|
||||
WHERE Id=@id";
|
||||
connection.Execute(queryDelete, new { id });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при удалении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public Passenger ReadPassengerById(int id)
|
||||
{
|
||||
return null;
|
||||
_logger.LogInformation("Получение объекта по идентификатору");
|
||||
_logger.LogDebug("Объект: {id}", id);
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"
|
||||
SELECT * FROM Passenger
|
||||
WHERE Id=@id";
|
||||
var passenger = connection.QueryFirst<Passenger>(querySelect, new
|
||||
{
|
||||
id
|
||||
});
|
||||
_logger.LogDebug("Найденный объект: {json}",
|
||||
JsonConvert.SerializeObject(passenger));
|
||||
return passenger;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при поиске объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Passenger> ReadPassengers()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Passenger";
|
||||
var passengers = connection.Query<Passenger>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(passengers));
|
||||
return passengers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdatePassenger(Passenger passenger)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
using ProjectAirline.Entities;
|
||||
using Dapper;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using ProjectAirline.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -9,21 +14,55 @@ namespace ProjectAirline.Repositories.Implementations;
|
||||
|
||||
public class TicketRepository : ITicketRepository
|
||||
{
|
||||
public void CreateTicket(Ticket ticket)
|
||||
private readonly IConnectionString _connectionString;
|
||||
private readonly ILogger<TicketRepository> _logger;
|
||||
public TicketRepository(IConnectionString connectionString,
|
||||
ILogger<TicketRepository> logger)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Ticket ReadTicketById(int id)
|
||||
public void CreateTicket(Ticket ticket)
|
||||
{
|
||||
return null;
|
||||
_logger.LogInformation("Добавление объекта");
|
||||
_logger.LogDebug("Объект: {json}",
|
||||
JsonConvert.SerializeObject(ticket));
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var queryInsert = @"
|
||||
INSERT INTO Ticket (FlightID, PassengerID, TicketPrice, DateBuy)
|
||||
VALUES (@FlightID, @PassengerID, @TicketPrice, @DateBuy)";
|
||||
connection.Execute(queryInsert, ticket);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<Ticket> ReadTickets()
|
||||
{
|
||||
return [];
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new
|
||||
NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Ticket";
|
||||
var tickets =
|
||||
connection.Query<Ticket>(querySelect);
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(tickets));
|
||||
return tickets;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Ошибка при чтении объектов");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateTicket(Ticket ticket)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
15
ProjectAirline/appsettings.json
Normal file
15
ProjectAirline/appsettings.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Debug",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/airline_log.txt",
|
||||
"rollingInterval": "Day"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user