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