diff --git a/Atelier/Atelier/Repositories/Implementations/ModelRepository.cs b/Atelier/Atelier/Repositories/Implementations/ModelRepository.cs index 2c49476..f45a166 100644 --- a/Atelier/Atelier/Repositories/Implementations/ModelRepository.cs +++ b/Atelier/Atelier/Repositories/Implementations/ModelRepository.cs @@ -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 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(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(querySelectSub).ToList(); model.CreateList(fabricModel); _logger.LogDebug("Model ID: {ModelId}, FabricModels: {FabricModels}", model.Id, JsonConvert.SerializeObject(fabricModel));