Finish DB models methods

This commit is contained in:
ShabOl 2024-04-30 23:48:10 +04:00
parent b10e63f24f
commit 81b5398465
4 changed files with 131 additions and 13 deletions

View File

@ -7,6 +7,8 @@ namespace ComputerShopContracts.BindingModels
public int Id { get; set; }
public int UserId { get; set; }
public int? ShipmentId { get; set; }
public string ProductName { get; set; } = string.Empty;
@ -15,7 +17,5 @@ namespace ComputerShopContracts.BindingModels
public int Warranty { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
public int? ShipmentId { get; set; }
}
}

View File

@ -9,6 +9,11 @@ namespace ComputerShopContracts.ViewModels
public int UserId { get; set; }
public int? ShipmentId { get; set; }
[DisplayName("Поставщик")]
public string? ProviderName { get; set; }
[DisplayName("Название товара")]
public string ProductName { get; set; } = string.Empty;
@ -19,10 +24,5 @@ namespace ComputerShopContracts.ViewModels
public int Warranty { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents { get; set; } = new();
public int? ShipmentId { get; set; }
[DisplayName("Поставщик")]
public string ProviderName { get; set; } = string.Empty;
}
}

View File

@ -1,4 +1,5 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -63,6 +64,64 @@ namespace ComputerShopDatabaseImplement.Models
};
}
// TODO: Update(), ViewModel, UpdateComponents()
public void Update(AssemblyBindingModel Model)
{
if (!string.IsNullOrEmpty(Model.AssemblyName))
{
AssemblyName = Model.AssemblyName;
}
if (!string.IsNullOrEmpty(Model.Category))
{
Category = Model.Category;
}
Cost = Model.Cost;
}
public AssemblyViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,
AssemblyName = AssemblyName,
Cost = Cost,
Category = Category,
AssemblyComponents = AssemblyComponents,
};
public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model)
{
var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList();
if (AssemblyComponents != null && AssemblyComponents.Count > 0)
{
Context.AssemblyComponents
.RemoveRange(AssemblyComponents
.Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId)));
Context.SaveChanges();
foreach (var ComponentToUpdate in AssemblyComponents)
{
ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2;
Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId);
}
Context.SaveChanges();
}
var CurrentAssembly = Context.Assemblies.First(x => x.Id == Id);
foreach (var AssemblyComponent in Model.AssemblyComponents)
{
Context.AssemblyComponents.Add(new AssemblyComponent
{
Assembly = CurrentAssembly,
Component = Context.Components.First(x => x.Id == AssemblyComponent.Key),
Count = AssemblyComponent.Value.Item2
});
Context.SaveChanges();
}
_assemblyComponents = null;
}
}
}

View File

@ -1,4 +1,5 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using ComputerShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -12,6 +13,10 @@ namespace ComputerShopDatabaseImplement.Models
[Required]
public int UserId { get; set; }
public int? ShipmentId { get; set; }
public virtual Shipment? Shipment { get; set; }
[Required]
public string ProductName { get; set; } = string.Empty;
@ -21,10 +26,6 @@ namespace ComputerShopDatabaseImplement.Models
[Required]
public int Warranty { get; set; }
public int? ShipmentId { get; set; }
public virtual Shipment? Shipment { get; set; }
[ForeignKey("ProductId")]
public virtual List<ProductComponent> Components { get; set; } = new();
@ -53,6 +54,7 @@ namespace ComputerShopDatabaseImplement.Models
{
Id = Model.Id,
UserId = Model.UserId,
ShipmentId = Model.ShipmentId,
ProductName = Model.ProductName,
Cost = Model.Cost,
Warranty = Model.Warranty,
@ -61,8 +63,65 @@ namespace ComputerShopDatabaseImplement.Models
Component = Context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
ShipmentId = Model.ShipmentId,
};
}
public void Update(ProductBindingModel Model)
{
if (!string.IsNullOrEmpty(Model.ProductName))
{
ProductName = Model.ProductName;
}
ShipmentId = Model.ShipmentId;
Cost = Model.Cost;
Warranty = Model.Warranty;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,
ShipmentId = ShipmentId,
Cost = Cost,
Warranty = Warranty,
ProductComponents = ProductComponents,
ProviderName = Shipment?.ProviderName,
};
public void UpdateComponents(ComputerShopDatabase Context, ProductBindingModel Model)
{
var ProductComponents = Context.ProductComponents.Where(x => x.ProductId == Model.Id).ToList();
if (ProductComponents != null && ProductComponents.Count > 0)
{
Context.ProductComponents
.RemoveRange(ProductComponents
.Where(x => !Model.ProductComponents.ContainsKey(x.ComponentId)));
Context.SaveChanges();
foreach (var ComponentToUpdate in ProductComponents)
{
ComponentToUpdate.Count = Model.ProductComponents[ComponentToUpdate.ComponentId].Item2;
Model.ProductComponents.Remove(ComponentToUpdate.ComponentId);
}
Context.SaveChanges();
}
var CurrentProduct = Context.Products.First(x => x.Id == Id);
foreach (var ProductComponent in Model.ProductComponents)
{
Context.ProductComponents.Add(new ProductComponent
{
Product = CurrentProduct,
Component = Context.Components.First(x => x.Id == ProductComponent.Key),
Count = ProductComponent.Value.Item2
});
Context.SaveChanges();
}
_productComponents = null;
}
}
}