Database implement
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using ComputerShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
internal class Assembly : IAssemblyModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string AssemblyName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
private Dictionary<int, (IComponentModel, int)>? _assemblyComponents =
|
||||
null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> AssemblyComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_assemblyComponents == null)
|
||||
{
|
||||
_assemblyComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||||
(recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _assemblyComponents;
|
||||
}
|
||||
}
|
||||
[ForeignKey("AssemblyId")]
|
||||
public virtual List<AssemblyComponent> Components { get; set; } = new();
|
||||
[ForeignKey("AssemblyId")]
|
||||
public virtual List<Purchase> Purchases { get; set; } = new();
|
||||
public static Assembly Create(ComputerShopDatabase context, AssemblyBindingModel model)
|
||||
{
|
||||
return new Assembly()
|
||||
{
|
||||
Id = model.Id,
|
||||
AssemblyName = model.AssemblyName,
|
||||
Price = model.Price,
|
||||
Components = model.AssemblyComponents.Select(x => new
|
||||
AssemblyComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(AssemblyBindingModel model)
|
||||
{
|
||||
AssemblyName = model.AssemblyName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public AssemblyViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
AssemblyName = AssemblyName,
|
||||
Price = Price,
|
||||
AssemblyComponents = AssemblyComponents
|
||||
};
|
||||
public void UpdateComponents(ComputerShopDatabase context, AssemblyBindingModel model)
|
||||
{
|
||||
var assemblyComponents = context.AssemblyComponents.Where(rec =>
|
||||
rec.Id == model.Id).ToList();
|
||||
if (assemblyComponents != null && assemblyComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.AssemblyComponents.RemoveRange(assemblyComponents.Where(rec
|
||||
=> !model.AssemblyComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in assemblyComponents)
|
||||
{
|
||||
updateComponent.Count = model.AssemblyComponents[updateComponent.ComponentId].Item2;
|
||||
model.AssemblyComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var assembly = context.Assemblies.First(x => x.Id == Id);
|
||||
foreach (var pc in model.AssemblyComponents)
|
||||
{
|
||||
context.AssemblyComponents.Add(new AssemblyComponent
|
||||
{
|
||||
Assembly = assembly,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_assemblyComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
internal class AssemblyComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int AssemblyId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Assembly Assembly { get; set; } = new();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
internal class Component : IComponentModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string ComponentName { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public double Cost { get; set; }
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<AssemblyComponent> AssemblyComponents { get; set; } =
|
||||
new();
|
||||
public static Component? Create(ComponentBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Component()
|
||||
{
|
||||
Id = model.Id,
|
||||
ComponentName = model.ComponentName,
|
||||
Cost = model.Cost
|
||||
};
|
||||
}
|
||||
public static Component Create(ComponentViewModel model)
|
||||
{
|
||||
return new Component
|
||||
{
|
||||
Id = model.Id,
|
||||
ComponentName = model.ComponentName,
|
||||
Cost = model.Cost
|
||||
};
|
||||
}
|
||||
public void Update(ComponentBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ComponentName = model.ComponentName;
|
||||
Cost = model.Cost;
|
||||
}
|
||||
public ComponentViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ComponentName = ComponentName,
|
||||
Cost = Cost
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using ComputerShopContracts.BindingModels;
|
||||
using ComputerShopContracts.ViewModels;
|
||||
using ComputerShopDataModels.Enums;
|
||||
using ComputerShopDataModels.Models;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ComputerShopDatabaseImplement.Models
|
||||
{
|
||||
internal class Purchase : IPurchaseModel
|
||||
{
|
||||
public string ComponentName { get; private set; } = String.Empty;
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public int ComponentId { get; private set; }
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
[Required]
|
||||
public PurchaseStatus Status { get; private set; } = PurchaseStatus.Неизвестен;
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
public virtual Component Component { get; set; }
|
||||
|
||||
public static Purchase? Create(PurchaseBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Purchase
|
||||
{
|
||||
ComponentId = model.ComponentId,
|
||||
ComponentName = model.ComponentName,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
Id = model.Id,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(PurchaseBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Status = model.Status;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public PurchaseViewModel GetViewModel => new()
|
||||
{
|
||||
ComponentId = ComponentId,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
Id = Id,
|
||||
Status = Status,
|
||||
ComponentName = ComponentName
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user