правки 2

This commit is contained in:
vettaql 2025-01-30 19:03:30 +04:00
parent 15410aec67
commit 5040951949

View File

@ -1,12 +1,10 @@
using Atelier.Repositories;
using Dapper;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Office2010.Excel;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Npgsql;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
internal class ModelRepository : IModelRepository
{
@ -18,6 +16,7 @@ internal class ModelRepository : IModelRepository
_connectionString = connectionString;
_logger = logger;
}
public void CreateModel(Model model)
{
_logger.LogInformation("Добавление объекта");
@ -111,10 +110,22 @@ internal class ModelRepository : IModelRepository
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var queryDelete = @"
connection.Open();
using var transaction = connection.BeginTransaction();
var queryDeleteFabricModel = @"
DELETE FROM FabricModel
WHERE ModelId = @Id";
connection.Execute(queryDeleteFabricModel, new { Id = id }, transaction);
var queryDeleteModel = @"
DELETE FROM Models
WHERE Id = @id";
connection.Execute(queryDelete, new { id });
WHERE Id = @Id";
connection.Execute(queryDeleteModel, new { Id = id }, transaction);
transaction.Commit();
}
catch (Exception ex)
{
@ -145,7 +156,6 @@ internal class ModelRepository : IModelRepository
}
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(model));
return model;
}
catch (Exception ex)
@ -155,35 +165,21 @@ internal class ModelRepository : IModelRepository
}
}
public IEnumerable<Model> ReadModels()
{
_logger.LogInformation("Получение всех объектов");
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @$"SELECT
m.*,
fm.FabricId,
fm.modelId,
fm.Count,
f.FabricType as FabricName
FROM Models m
INNER JOIN FabricModel fm on fm.ModelId = m.Id
LEFT JOIN Fabrics f ON f.Id = fm.FabricId";
var querySelect = "SELECT * FROM Models";
var models = connection.Query<Model>(querySelect).ToList();
foreach (var model in models)
{
var querySelectSub = @$"SELECT
fm.FabricId,
fm.modelId,
fm.Count,
f.FabricType as FabricName
FROM FabricModel fm
LEFT JOIN Fabrics f ON f.Id = fm.FabricId
WHERE ModelId = {model.Id}";
var querySelectSub = @$"SELECT fm.FabricId, fm.Count, f.FabricType as FabricName
FROM FabricModel fm
JOIN Fabrics f ON f.Id = fm.FabricId
WHERE ModelId = {model.Id}";
var fabricModel = connection.Query<FabricModel>(querySelectSub).ToList();
model.CreateList(fabricModel);
_logger.LogDebug("Model ID: {ModelId}, FabricModels: {FabricModels}", model.Id, JsonConvert.SerializeObject(fabricModel));