diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs b/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs index 66ec15b..c56b0a1 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs @@ -36,4 +36,19 @@ public class Contract Services = services }; } + + public static Contract CreateEntity(TempServiceContract tempServiceContract, IEnumerable services) + { + return new Contract + { + Id = tempServiceContract.Id, + CustomerID = tempServiceContract.CustomerID, + ExecutorID = tempServiceContract.ExecutorID, + Category = tempServiceContract.Category, + ConclusionDate = tempServiceContract.ConclusionDate, + Deadline = tempServiceContract.Deadline, + PaymentAmount = tempServiceContract.PaymentAmount, + Services = services + }; + } } diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Entities/TempServiceContract.cs b/PIbd-23_Gutorov_I.A._IT-Company/Entities/TempServiceContract.cs new file mode 100644 index 0000000..9509d60 --- /dev/null +++ b/PIbd-23_Gutorov_I.A._IT-Company/Entities/TempServiceContract.cs @@ -0,0 +1,24 @@ +using PIbd_23_Gutorov_I.A._IT_Company.Entities.Enums; + +namespace PIbd_23_Gutorov_I.A._IT_Company.Entities; + +public class TempServiceContract +{ + public int Id { get; private set; } + + public int CustomerID { get; private set; } + + public int ExecutorID { get; private set; } + + public ContractCategory Category { get; private set; } + + public DateTime ConclusionDate { get; private set; } + + public DateTime Deadline { get; private set; } + + public int PaymentAmount { get; private set; } + + public int ServiceId { get; private set; } + + public string Description { get; private set; } = string.Empty; +} diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Forms/FormContract.cs b/PIbd-23_Gutorov_I.A._IT-Company/Forms/FormContract.cs index 4275593..2cf5d43 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Forms/FormContract.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Forms/FormContract.cs @@ -75,9 +75,12 @@ namespace PIbd_23_Gutorov_I.A._IT_Company.Forms foreach (DataGridViewRow row in dataGridViewServices.Rows) if (row.Cells["ColumnServiceDescription"].Value != null) - list.Add(Service.CreateEntity(0, Convert.ToString(row.Cells["ColumnServiceDescription"].Value)!)); + list.Add(Service.CreateEntity(Convert.ToInt32(row.Cells["ColumnServiceDescription"].Value), + Convert.ToString(row.Cells["ColumnServiceDescription"])!)); - return list; + return list.GroupBy(x => x.Id) + .Select(x => x.First()) + .ToList(); } } } diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs index c6a8c40..c0123dc 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; +using PIbd_23_Gutorov_I.A._IT_Company.Entities; namespace PIbd_23_Gutorov_I.A._IT_Company.Repositories.Implementations; @@ -76,17 +77,25 @@ public class ContractRepository : IContractRepository } } - public IEnumerable ReadContracts(DateTime? dateFrom = null, DateTime? dateTo = null, int? customerId = null, int? executorId = null) + public IEnumerable ReadContracts(DateTime? dateFrom = null, DateTime? dateTo = null, int? customerId = null, int? executorId = null) { _logger.LogInformation("Получение всех объектов"); try { using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT * FROM Contracts"; - var contracts = connection.Query(querySelect); + var querySelect = @" + SELECT c.*, sc.ServiceId, s.Description + FROM Contracts c + INNER JOIN ServiceContract sc ON sc.ContractId = c.Id + INNER JOIN Services s ON sc.ServiceId = s.Id + "; + var contracts = connection.Query(querySelect); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts)); - return contracts; + return contracts + .GroupBy(x => x.Id, y => y, (key, value) => + Contract.CreateEntity(value.First(), value.Select(z => Service.CreateEntity(z.ServiceId, z.Description)))) + .ToList(); } catch (Exception ex) {