ПИбд-23. Гуторов И.А. Лабораторная работа 3 #3

Closed
vasmaae wants to merge 5 commits from LabWork_3 into LabWork_2
4 changed files with 57 additions and 6 deletions
Showing only changes of commit d644ef51a2 - Show all commits

View File

@ -36,4 +36,19 @@ public class Contract
Services = services
};
}
public static Contract CreateEntity(TempServiceContract tempServiceContract, IEnumerable<Service> 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
};
}
}

View File

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

View File

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

View File

@ -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<Entities.Contract> ReadContracts(DateTime? dateFrom = null, DateTime? dateTo = null, int? customerId = null, int? executorId = null)
public IEnumerable<Contract> 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<Entities.Contract>(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<TempServiceContract>(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)
{