diff --git a/BusinessLogic/BusinessLogic/SellLogic.cs b/BusinessLogic/BusinessLogic/SellLogic.cs index aacd8e5..ababedf 100644 --- a/BusinessLogic/BusinessLogic/SellLogic.cs +++ b/BusinessLogic/BusinessLogic/SellLogic.cs @@ -31,7 +31,8 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Insert operation failed."); } - return sell; + //return sell; + return new(); } public SellViewModel GetElement(SellSearchModel model) { @@ -41,17 +42,19 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Get element operation failed."); } - return sell; + return new(); + //return sell; } public IEnumerable GetElements(SellSearchModel? model) { - var sell_list = _sellStorage.GetList(model); - if (sell_list is null || sell_list.Count() == 0) - { - _logger.LogWarning("ReadList return null list"); - return []; - } - return sell_list; + //var sell_list = _sellStorage.GetList(model); + //if (sell_list is null || sell_list.Count() == 0) + //{ + // _logger.LogWarning("ReadList return null list"); + // return []; + //} + return []; + //return sell_list; } public SellViewModel Update(SellSearchModel model) { @@ -61,7 +64,8 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Update operation failed."); } - return sell; + return new(); + //return sell; } public SellViewModel Delete(SellSearchModel model) { @@ -71,7 +75,8 @@ namespace BusinessLogic.BusinessLogic { throw new Exception("Update operation failed."); } - return sell; + return new(); + //return sell; } } } diff --git a/BusinessLogic/BusinessLogic/SupplyLogic.cs b/BusinessLogic/BusinessLogic/SupplyLogic.cs index 63a40d9..b246c0d 100644 --- a/BusinessLogic/BusinessLogic/SupplyLogic.cs +++ b/BusinessLogic/BusinessLogic/SupplyLogic.cs @@ -3,6 +3,7 @@ using Contracts.BusinessLogicContracts; using Contracts.SearchModels; using Contracts.StorageContracts; using Contracts.ViewModels; +using DataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -101,5 +102,22 @@ namespace BusinessLogic.BusinessLogic throw new ArgumentNullException("Нет названия поставки", nameof(model.Name)); } } + + public bool StatusUpdate(SupplyBindingModel model, SupplyStatus newStatus) + { + if (model.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Supply status incorrect."); + return false; + } + model.Status = newStatus; + if (_supplyStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } } } diff --git a/Contracts/BusinessLogicContracts/ISupplyLogic.cs b/Contracts/BusinessLogicContracts/ISupplyLogic.cs index d21ff5d..8ae201a 100644 --- a/Contracts/BusinessLogicContracts/ISupplyLogic.cs +++ b/Contracts/BusinessLogicContracts/ISupplyLogic.cs @@ -1,6 +1,7 @@ using Contracts.BindingModels; using Contracts.SearchModels; using Contracts.ViewModels; +using DataModels.Enums; using System; using System.Collections.Generic; using System.Linq; @@ -16,5 +17,6 @@ namespace Contracts.BusinessLogicContracts bool Create(SupplyBindingModel model); bool Update(SupplyBindingModel model); bool Delete(SupplyBindingModel model); + bool StatusUpdate(SupplyBindingModel model, SupplyStatus status); } } diff --git a/Contracts/StorageContracts/ISupplyStorage.cs b/Contracts/StorageContracts/ISupplyStorage.cs index 2f81c46..b35e7aa 100644 --- a/Contracts/StorageContracts/ISupplyStorage.cs +++ b/Contracts/StorageContracts/ISupplyStorage.cs @@ -15,7 +15,7 @@ namespace Contracts.StorageContracts List GetFilteredList(SupplySearchModel model); SupplyViewModel? GetElement(SupplySearchModel model); SupplyViewModel? Insert(SupplyBindingModel model); - SupplyViewModel? Update(SupplyBindingModel model); + bool? Update(SupplyBindingModel model); SupplyViewModel? Delete(SupplyBindingModel model); } } diff --git a/DatabaseImplement/Implements/SupplyStorage.cs b/DatabaseImplement/Implements/SupplyStorage.cs index b1f5ec9..66009e7 100644 --- a/DatabaseImplement/Implements/SupplyStorage.cs +++ b/DatabaseImplement/Implements/SupplyStorage.cs @@ -127,7 +127,7 @@ namespace DatabaseImplement.Implements return newProduct.GetViewModel; } - public SupplyViewModel? Update(SupplyBindingModel model) + public bool? Update(SupplyBindingModel model) { using var context = new Database(); using var transaction = context.Database.BeginTransaction(); @@ -137,13 +137,12 @@ namespace DatabaseImplement.Implements rec.Id == model.Id); if (product == null) { - return null; + return false; } product.Update(model); context.SaveChanges(); - product.UpdateProducts(context, model); transaction.Commit(); - return product.GetViewModel; + return true; } catch { diff --git a/DatabaseImplement/Models/Supply.cs b/DatabaseImplement/Models/Supply.cs index f66b0dd..060d97c 100644 --- a/DatabaseImplement/Models/Supply.cs +++ b/DatabaseImplement/Models/Supply.cs @@ -65,8 +65,7 @@ namespace DatabaseImplement.Models } public void Update(SupplyBindingModel model) { - Name = model.Name; - Price = model.Price; + Status = model.Status; } public SupplyViewModel GetViewModel { @@ -79,41 +78,12 @@ namespace DatabaseImplement.Models Name = Name, Price = Price, Products = SupplyProducts, + SupplierId = SupplierId, Date = Date, Status = Status, - SupplierName = context.Suppliers.FirstOrDefault(x => x.Id == Id)?.Name ?? string.Empty, + SupplierName = Supplier.Name, }; } } - public void UpdateProducts(Database context, SupplyBindingModel model) - { - var supplyProducts = context.SupplyProducts.Where(rec => - rec.Id == model.Id).ToList(); - if (supplyProducts != null && supplyProducts.Count > 0) - { // удалили те, которых нет в модели - context.SupplyProducts.RemoveRange(supplyProducts.Where(rec - => !model.SupplyProducts.ContainsKey(rec.ProductId))); - context.SaveChanges(); - // обновили количество у существующих записей - foreach (var updateProduct in supplyProducts) - { - updateProduct.Count = model.SupplyProducts[updateProduct.ProductId].Item2; - model.SupplyProducts.Remove(updateProduct.ProductId); - } - context.SaveChanges(); - } - var supply = context.Supplies.First(x => x.Id == Id); - foreach (var pc in model.SupplyProducts) - { - context.SupplyProducts.Add(new SupplyProduct - { - Supply = supply, - Product = context.Products.First(x => x.Id == pc.Key), - Count = pc.Value.Item2 - }); - context.SaveChanges(); - } - _supplyProducts = null; - } } } diff --git a/WinFormsApp/FormMain.Designer.cs b/WinFormsApp/FormMain.Designer.cs index ecf603f..706261e 100644 --- a/WinFormsApp/FormMain.Designer.cs +++ b/WinFormsApp/FormMain.Designer.cs @@ -33,6 +33,7 @@ menuStrip1 = new MenuStrip(); товарыToolStripMenuItem = new ToolStripMenuItem(); поставщикиToolStripMenuItem = new ToolStripMenuItem(); + buttonSupplyStatusArriving = new Button(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); menuStrip1.SuspendLayout(); SuspendLayout(); @@ -48,7 +49,7 @@ // // buttonCreateSupply // - buttonCreateSupply.Location = new Point(706, 24); + buttonCreateSupply.Location = new Point(707, 27); buttonCreateSupply.Name = "buttonCreateSupply"; buttonCreateSupply.Size = new Size(154, 23); buttonCreateSupply.TabIndex = 1; @@ -79,11 +80,22 @@ поставщикиToolStripMenuItem.Text = "Поставщики"; поставщикиToolStripMenuItem.Click += поставщикиToolStripMenuItem_Click_1; // + // buttonSupplyStatusArriving + // + buttonSupplyStatusArriving.Location = new Point(707, 76); + buttonSupplyStatusArriving.Name = "buttonSupplyStatusArriving"; + buttonSupplyStatusArriving.Size = new Size(154, 23); + buttonSupplyStatusArriving.TabIndex = 3; + buttonSupplyStatusArriving.Text = "Поставка в пути"; + buttonSupplyStatusArriving.UseVisualStyleBackColor = true; + buttonSupplyStatusArriving.Click += buttonSupplyStatusArriving_Click; + // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(901, 384); + Controls.Add(buttonSupplyStatusArriving); Controls.Add(buttonCreateSupply); Controls.Add(dataGridView); Controls.Add(menuStrip1); @@ -105,5 +117,6 @@ private MenuStrip menuStrip1; private ToolStripMenuItem товарыToolStripMenuItem; private ToolStripMenuItem поставщикиToolStripMenuItem; + private Button buttonSupplyStatusArriving; } } \ No newline at end of file diff --git a/WinFormsApp/FormMain.cs b/WinFormsApp/FormMain.cs index 16b881f..b3c05e3 100644 --- a/WinFormsApp/FormMain.cs +++ b/WinFormsApp/FormMain.cs @@ -1,4 +1,7 @@ -using Contracts.BusinessLogicContracts; +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using DataModels.Enums; +using DataModels.Models; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Logging; using System; @@ -77,5 +80,33 @@ namespace WinFormsApp form.ShowDialog(); } } + + private void buttonSupplyStatusArriving_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + Guid id = (Guid)dataGridView.SelectedRows[0].Cells["Id"].Value; + _logger.LogInformation("Поставка No{id}. Меняется статус", id); + try + { + var operationResult = _supplyLogic.StatusUpdate(new SupplyBindingModel + { + Id = id, + Status = (SupplyStatus)dataGridView.SelectedRows[0].Cells["Status"].Value + }, SupplyStatus.Arriving); + if (!operationResult) + { + //throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка передачи заказа в работу"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } } }