Database implement

This commit is contained in:
the 2023-04-05 19:08:51 +04:00
parent 40fe6af205
commit e3a360bdec
9 changed files with 576 additions and 0 deletions

View File

@ -0,0 +1,27 @@
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement
{
internal class ComputerShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-IRUVF5S\SQLEXPRESS;Initial Catalog=ComputerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Assembly> Assemblies { set; get; }
public virtual DbSet<AssemblyComponent> AssemblyComponents { set; get; }
public virtual DbSet<Purchase> Purchases { set; get; }
}
}

View File

@ -17,4 +17,9 @@
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComputerShopContracts\ComputerShopContracts.csproj" />
<ProjectReference Include="..\ComputerShopDataModels\ComputerShopDataModels.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,111 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
internal class AssemblyStorage : IAssemblyStorage
{
public List<AssemblyViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<AssemblyViewModel> GetFilteredList(AssemblySearchModel model)
{
if (string.IsNullOrEmpty(model.AssemblyName))
{
return new();
}
using var context = new ComputerShopDatabase();
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.AssemblyName.Contains(model.AssemblyName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public AssemblyViewModel? GetElement(AssemblySearchModel model)
{
if (string.IsNullOrEmpty(model.AssemblyName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.AssemblyName) &&
x.AssemblyName == model.AssemblyName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public AssemblyViewModel? Insert(AssemblyBindingModel model)
{
using var context = new ComputerShopDatabase();
var newProduct = Assembly.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Assemblies.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public AssemblyViewModel? Update(AssemblyBindingModel model)
{
using var context = new ComputerShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Assemblies.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateComponents(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public AssemblyViewModel? Delete(AssemblyBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.Assemblies
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Assemblies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,89 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
internal class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new ComputerShopDatabase();
return context.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.Components
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName ==
model.ComponentName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new ComputerShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new ComputerShopDatabase();
var component = context.Components.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,85 @@
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
internal class PurchaseStorage : IPurchaseStorage
{
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.Purchases.Include(x => x.Component).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new ComputerShopDatabase();
return context.Purchases
.Where(x => x.Id == model.Id)
.Include(x => x.Component)
.Select(x => x.GetViewModel)
.ToList();
}
public List<PurchaseViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.Purchases.Include(x => x.Component).Select(x => x.GetViewModel).ToList();
}
public PurchaseViewModel? Insert(PurchaseBindingModel model)
{
var newOrder = Purchase.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new ComputerShopDatabase();
context.Purchases.Add(newOrder);
context.SaveChanges();
return context.Purchases.Include(x => x.Component).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
}
public PurchaseViewModel? Update(PurchaseBindingModel model)
{
using var context = new ComputerShopDatabase();
var order = context.Purchases.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Purchases.Include(x => x.Component).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public PurchaseViewModel? Delete(PurchaseBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.Purchases.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Purchases.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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();
}
}

View File

@ -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
};
}
}

View File

@ -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
};
}
}