готово 2 лаб ворк

This commit is contained in:
cyxaruk 2024-12-05 16:52:52 +04:00
parent 8267c65d83
commit 7c3e544cf8
16 changed files with 288 additions and 73 deletions

View File

@ -11,17 +11,17 @@ public class Bus
{
public int ID { get; private set; }
public string BusName { get; private set; } = string.Empty;
public string Bus_Name { get; private set; } = string.Empty;
public string LicensePlate { get; private set; } = string.Empty;
public string Licence_Plate { get; private set; } = string.Empty;
public static Bus CreateEntity(int id, string busName, string licensePlate)
{
return new Bus
{
ID = id,
BusName = busName,
LicensePlate = licensePlate
Bus_Name = busName,
Licence_Plate = licensePlate
};
}
}

View File

@ -6,23 +6,23 @@ public class BusCheck
{
public int ID { get; private set; }
public int BusID { get; private set; }
public int Bus_ID { get; private set; }
public int Price { get; private set; }
public DateTime DateReceipt { get; private set; }
public DateTime Date { get; private set; }
public BusElementType BusElementType { get; private set; }
public BusElementType Bus_Element_Type { get; private set; }
public static BusCheck CreateOperation(int id, int busID, int price, BusElementType busElementType)
{
return new BusCheck
{
ID = id,
BusID = busID,
Bus_ID = busID,
Price = price,
DateReceipt = DateTime.Now,
BusElementType = busElementType
Date = DateTime.Now,
Bus_Element_Type = busElementType
};
}
}

View File

@ -6,21 +6,21 @@ public class Employee
{
public int ID { get; private set; }
public string FirstName { get; private set; } = string.Empty;
public string First_Name { get; private set; } = string.Empty;
public string LastName { get; private set; } = string.Empty;
public string Last_Name { get; private set; } = string.Empty;
public EmployeePost EmployeePost { get; private set; }
public EmployeePost Post { get; private set; }
public static Employee CreateEntity(int id, string first, string last, EmployeePost employeePost)
{
return new Employee
{
ID = id,
FirstName = first ?? string.Empty,
LastName = last ?? string.Empty,
EmployeePost = employeePost
First_Name = first ?? string.Empty,
Last_Name = last ?? string.Empty,
Post = employeePost
};
}

View File

@ -13,17 +13,17 @@ public class RouteList
{
public int Id { get; private set; }
public string Name { get; private set; } = string.Empty;
public string Route_Name { get; private set; } = string.Empty;
public string Description { get; private set; } = string.Empty;
public string Route_Description { get; private set; } = string.Empty;
public static RouteList CreateEntity(int id, string name, string description)
{
return new RouteList
{
Id = id,
Name = name ?? string.Empty,
Description = description ?? string.Empty
Route_Name = name ?? string.Empty,
Route_Description = description ?? string.Empty
};
}

View File

@ -12,11 +12,11 @@ public class StartingShift
{
public int ID { get; private set; }
public int RouteListID { get; private set; }
public int Route_List_ID { get; private set; }
public int BusID { get; private set; }
public int Bus_ID { get; private set; }
public DateTime StartingShiftDate { get; private set; }
public DateTime Starting_Shift_Date { get; private set; }
public IEnumerable<StartingShift_Employee> StartingShiftEmployees { get; private set; } = [];
@ -25,10 +25,10 @@ public class StartingShift
return new StartingShift
{
ID = id,
RouteListID = routeListID,
BusID = busID,
Route_List_ID = routeListID,
Bus_ID = busID,
StartingShiftEmployees = startingShiftEmployees,
StartingShiftDate = DateTime.Now,
Starting_Shift_Date = DateTime.Now,
};
}

View File

@ -2,19 +2,19 @@
public class StartingShift_Employee
{
public int ID { get; private set; }
public int Starting_Shift_ID { get; private set; }
public int EmployeeID { get; private set; }
public int Employee_ID { get; private set; }
public int ShiftDuration { get; private set; }
public int Shift_Duration { get; private set; }
public static StartingShift_Employee CreateElement(int id, int employeeID, int shiftDuration)
{
return new StartingShift_Employee
{
ID = id,
EmployeeID = employeeID,
ShiftDuration = shiftDuration
Starting_Shift_ID = id,
Employee_ID = employeeID,
Shift_Duration = shiftDuration
};
}
}

View File

@ -24,8 +24,8 @@ public partial class FormBus : Form
throw new InvalidDataException(nameof(bus));
}
textBoxLicensePlate.Text = bus.LicensePlate;
textBoxBusName.Text = bus.BusName;
textBoxLicensePlate.Text = bus.Licence_Plate;
textBoxBusName.Text = bus.Bus_Name;
_busId = value;
}
catch (Exception ex)

View File

@ -25,9 +25,9 @@ namespace ProjectPeopleTransportation.Forms
throw new InvalidDataException(nameof(employee));
}
textBoxFirstName.Text = employee.FirstName;
textBoxLastName.Text = employee.LastName;
comboBoxPost.SelectedItem = employee.EmployeePost;
textBoxFirstName.Text = employee.First_Name;
textBoxLastName.Text = employee.Last_Name;
comboBoxPost.SelectedItem = employee.Post;
_employeeId = value;
}
catch (Exception ex)

View File

@ -23,8 +23,8 @@ namespace ProjectPeopleTransportation.Forms
}
textBoxName.Text = routeList.Name;
textBoxDescription.Text = routeList.Description;
textBoxName.Text = routeList.Route_Name;
textBoxDescription.Text = routeList.Route_Description;
_routeListId = value;
}
catch (Exception ex)

View File

@ -83,6 +83,10 @@
//
// dataGridViewRoute
//
dataGridViewRoute.AllowUserToAddRows = false;
dataGridViewRoute.AllowUserToDeleteRows = false;
dataGridViewRoute.AllowUserToResizeColumns = false;
dataGridViewRoute.AllowUserToResizeRows = false;
dataGridViewRoute.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewRoute.Dock = DockStyle.Fill;
dataGridViewRoute.Location = new Point(0, 0);

View File

@ -16,11 +16,11 @@ namespace ProjectPeopleTransportation.Forms
throw new ArgumentNullException(nameof(startingShiftRepository));
ColumnEmployees.DataSource = employeeRepository.ReadEmployees();
ColumnEmployees.DisplayMember = "FirstName";
ColumnEmployees.DisplayMember = "First_Name";
ColumnEmployees.ValueMember = "Id";
comboBoxBus.DataSource = busRepository.ReadBuses();
comboBoxBus.DisplayMember = "BusName";
comboBoxBus.DisplayMember = "Bus_Name";
comboBoxBus.ValueMember = "Id";
comboBoxRoute.DataSource = routeListRepository.ReadRouteLists();
@ -60,8 +60,8 @@ namespace ProjectPeopleTransportation.Forms
var list = new List<StartingShift_Employee>();
foreach (DataGridViewRow row in dataGridViewEmployees.Rows)
{
if (row.Cells["ColumnFeed"].Value == null ||
row.Cells["ColumnCount"].Value == null)
if (row.Cells["ColumnEmployees"].Value == null ||
row.Cells["ColumnShiftDuration"].Value == null)
{
continue;
}

View File

@ -1,20 +1,78 @@
using ProjectPeopleTransportation.Entities;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectPeopleTransportation.Entities;
using ProjectPeopleTransportation.Entities.Enums;
namespace ProjectPeopleTransportation.Repositories.Implementations;
public class BusCheckRepository : IBusCheckRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<BusCheckRepository> _logger;
public BusCheckRepository(IConnectionString connectionString, ILogger<BusCheckRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateBusCheck(BusCheck busCheck)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(busCheck));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO bus_check (date, price, bus_id, bus_element_type)
VALUES (@Date, @Price, @Bus_id, @Bus_Element_Type)";
connection.Execute(queryInsert, busCheck);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public void DeleteBusCheck(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM bus_check
WHERE id=@id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public IEnumerable<BusCheck> ReadBusCheck(DateTime? dateForm = null, DateTime? dateTo = null, BusElementType BusElementType = BusElementType.None, int? busId = null, int? price = null)
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM bus_check";
var services = connection.Query<BusCheck>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(services));
return services;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}

View File

@ -1,29 +1,127 @@
using ProjectPeopleTransportation.Entities;
using ProjectPeopleTransportation.Entities.Enums;
using Dapper;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectPeopleTransportation.Entities;
using System;
namespace ProjectPeopleTransportation.Repositories.Implementations;
public class BusRepository : IBusRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<BusRepository> _logger;
public BusRepository(IConnectionString connectionString, ILogger<BusRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateBus(Bus bus)
{
}
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(bus));
public void DeleteBus(int id)
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO bus (licence_plate, bus_name)
VALUES (@Licence_Plate, @Bus_Name)";
connection.Execute(queryInsert, bus);
}
public Bus ReadBusByID(int id)
catch (Exception ex)
{
return Bus.CreateEntity(0, string.Empty, string.Empty);
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
public IEnumerable<Bus> ReadBuses()
{
return [];
}
public void UpdateBus(Bus bus)
{
_logger.LogInformation("Редактирование объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(bus));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryUpdate = @"
UPDATE bus
SET
licence_plate=@Licence_Plate,
bus_name=@Bus_Name
WHERE id=@ID";
connection.Execute(queryUpdate, bus);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при редактировании объекта");
throw;
}
}
public void DeleteBus(int id)
{
_logger.LogInformation("Удаление объекта");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM bus
WHERE id=@ID";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при удалении объекта");
throw;
}
}
public Bus ReadBusByID(int busId)
{
_logger.LogInformation("Получение объекта по идентификатору");
_logger.LogDebug("Объект: {id}", busId);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM bus
WHERE id=@busId";
var bus = connection.QueryFirst<Bus>(querySelect, new { busId });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(bus));
return bus;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при поиске объекта");
throw;
}
}
public IEnumerable<Bus> ReadBuses()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM bus";
var buses = connection.Query<Bus>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(buses));
return buses;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}

View File

@ -28,7 +28,7 @@ public class EmployeeRepository : IEmployeeRepository
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO employee (first_name, last_name, post)
VALUES (@FirstName, @LastName, @EmployeePost)";
VALUES (@First_Name, @Last_Name, @Post)";
connection.Execute(queryInsert, employee);
}
catch (Exception ex)
@ -68,7 +68,7 @@ WHERE id=@ID";
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM employee
WHERE [id]=@ID";
WHERE id=@ID";
var employee = connection.QueryFirst<Employee>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(employee));
@ -110,9 +110,9 @@ WHERE [id]=@ID";
var queryUpdate = @"
UPDATE employee
SET
first_name=@FirstName,
last_name=@LastName,
post=@EmployeePost
first_name=@First_Name,
last_name=@Last_Name,
post=@Post
WHERE id=@ID";
connection.Execute(queryUpdate, employee);
}

View File

@ -28,7 +28,7 @@ public class RouteListRepository : IRouteListRepository
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryInsert = @"
INSERT INTO route_list (route_name, route_description)
VALUES (@Name, @Description)";
VALUES (@Route_Name, @Route_Description)";
connection.Execute(queryInsert, routeList);
}
catch (Exception ex)
@ -48,7 +48,7 @@ VALUES (@Name, @Description)";
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
DELETE FROM route_list
WHERE id=@ID";
WHERE id=@id";
connection.Execute(queryDelete, new { id });
}
catch (Exception ex)
@ -68,7 +68,7 @@ WHERE id=@ID";
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM route_list
WHERE [id]=@ID";
WHERE id=@id";
var routeList = connection.QueryFirst<RouteList>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(routeList));
@ -110,9 +110,9 @@ WHERE [id]=@ID";
var queryUpdate = @"
UPDATE route_list
SET
route_name=@Name,
route_description=@Description
WHERE id=@ID";
route_name=@Route_Name,
route_description=@Route_Description
WHERE id=@Id";
connection.Execute(queryUpdate, routeList);
}
catch (Exception ex)

View File

@ -1,15 +1,70 @@
using ProjectPeopleTransportation.Entities;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using ProjectPeopleTransportation.Entities;
using Dapper;
namespace ProjectPeopleTransportation.Repositories.Implementations;
public class StartingShiftRepository : IStartingShiftRepository
{
private readonly IConnectionString _connectionString;
private readonly ILogger<StartingShiftRepository> _logger;
public StartingShiftRepository(IConnectionString connectionString, ILogger<StartingShiftRepository> logger)
{
_connectionString = connectionString;
_logger = logger;
}
public void CreateStartingShift(StartingShift startingShift)
{
_logger.LogInformation("Добавление объекта");
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(startingShift));
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
connection.Open();
using var transaction = connection.BeginTransaction();
var queryInsert = @"
INSERT INTO starting_shift (starting_shift_date, route_list_id, bus_id)
VALUES (@Starting_Shift_Date, @Route_list_id, @Bus_id);
SELECT MAX(Id) FROM starting_shift";
var starting_shift_id = connection.QueryFirst<int>(queryInsert, startingShift, transaction);
var querySubInsert = @"
INSERT INTO starting_shift_employee (starting_shift_id, employee_id, shift_duration)
VALUES (@Starting_shift_id, @Employee_id, @Shift_Duration)";
foreach (var elem in startingShift.StartingShiftEmployees)
{
connection.Execute(querySubInsert, new { starting_shift_id, elem.Employee_ID, elem.Shift_Duration }, transaction);
}
transaction.Commit();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при добавлении объекта");
throw;
}
}
public IEnumerable<StartingShift> ReadStartingShifts(DateTime? dateForm = null, DateTime? dateTo = null, int? employeeId = null, int? busId = null, int? routelistId = null)
{
return [];
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT * FROM starting_shift";
var startingShifts = connection.Query<StartingShift>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(startingShifts));
return startingShifts;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");
throw;
}
}
}