diff --git a/ComputerShopProvider/ComputerShopContracts/BindingModels/SupplyBindingModel.cs b/ComputerShopProvider/ComputerShopContracts/BindingModels/SupplyBindingModel.cs index df56fde..2e8f55f 100644 --- a/ComputerShopProvider/ComputerShopContracts/BindingModels/SupplyBindingModel.cs +++ b/ComputerShopProvider/ComputerShopContracts/BindingModels/SupplyBindingModel.cs @@ -17,8 +17,10 @@ namespace ComputerShopContracts.BindingModels public DateTime? DateImplement { get; set; } - public int OrderId { get; set; } - - public int ReceivingId { get; set; } + public Dictionary SupplyOrders + { + get; + set; + } = new(); } } diff --git a/ComputerShopProvider/ComputerShopContracts/ViewModels/SupplyViewModel.cs b/ComputerShopProvider/ComputerShopContracts/ViewModels/SupplyViewModel.cs index f2ce861..e781dde 100644 --- a/ComputerShopProvider/ComputerShopContracts/ViewModels/SupplyViewModel.cs +++ b/ComputerShopProvider/ComputerShopContracts/ViewModels/SupplyViewModel.cs @@ -20,15 +20,13 @@ namespace ComputerShopContracts.ViewModels [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } - [DisplayName("Номер заказа")] - public int OrderId { get; set; } - - [DisplayName("Номер получения")] - public int ReceivingId { get; set; } - [DisplayName("Номер")] public int Id { get; set; } - + public Dictionary SupplyOrders + { + get; + set; + } = new(); diff --git a/ComputerShopProvider/ComputerShopDataModels/Models/ISupplyModel.cs b/ComputerShopProvider/ComputerShopDataModels/Models/ISupplyModel.cs index 2cfa9e0..043bc5d 100644 --- a/ComputerShopProvider/ComputerShopDataModels/Models/ISupplyModel.cs +++ b/ComputerShopProvider/ComputerShopDataModels/Models/ISupplyModel.cs @@ -7,7 +7,6 @@ namespace ComputerShopDataModels.Models SupplyStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } - int OrderId { get; } - int ReceivingId { get; } + Dictionary SupplyOrders { get; } } } diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/EquipmentReceivingStorage.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/EquipmentReceivingStorage.cs new file mode 100644 index 0000000..0099af8 --- /dev/null +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/EquipmentReceivingStorage.cs @@ -0,0 +1,87 @@ +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 EquipmentReceivingStorage : IEquipmentReceivingStorage + { + public EquipmentReceivingViewModel? Delete(EquipmentReceivingBindingModel model) + { + using var context = new ComputerShopDatabase(); + var element = context.EquipmentReceivings.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.EquipmentReceivings.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public EquipmentReceivingViewModel? GetElement(EquipmentReceivingSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.EquipmentReceivings + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public List GetFilteredList(EquipmentReceivingSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputerShopDatabase(); + return context.EquipmentReceivings + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.EquipmentReceivings.Select(x => x.GetViewModel).ToList(); + } + + public EquipmentReceivingViewModel? Insert(EquipmentReceivingBindingModel model) + { + var newOrder = EquipmentReceiving.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ComputerShopDatabase(); + context.EquipmentReceivings.Add(newOrder); + context.SaveChanges(); + return context.EquipmentReceivings.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + } + + public EquipmentReceivingViewModel? Update(EquipmentReceivingBindingModel model) + { + using var context = new ComputerShopDatabase(); + var order = context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + } +} diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/OrderStorage.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/OrderStorage.cs index 2374d45..af905a8 100644 --- a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/OrderStorage.cs @@ -15,17 +15,42 @@ namespace ComputerShopDatabaseImplement.Implements { public OrderViewModel? Delete(OrderBindingModel model) { - throw new NotImplementedException(); + using var context = new ComputerShopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == + model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } public OrderViewModel? GetElement(OrderSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Orders + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputerShopDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/SupplyStorage.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/SupplyStorage.cs new file mode 100644 index 0000000..72825cd --- /dev/null +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/SupplyStorage.cs @@ -0,0 +1,113 @@ +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 SupplyStorage : ISupplyStorage + { + public SupplyViewModel? Delete(SupplyBindingModel model) + { + using var context = new ComputerShopDatabase(); + var element = context.Supplies + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Supplies.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public SupplyViewModel? GetElement(SupplySearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Supplies + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(SupplySearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputerShopDatabase(); + return context.Supplies + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .Where(x => x.Id == model.Id) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.Supplies + .Include(x => x.Orders) + .ThenInclude(x => x.Order) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public SupplyViewModel? Insert(SupplyBindingModel model) + { + using var context = new ComputerShopDatabase(); + var newProduct = Supply.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Supplies.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public SupplyViewModel? Update(SupplyBindingModel model) + { + using var context = new ComputerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Supplies.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateOrders(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Models/EquipmentReceiving.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Models/EquipmentReceiving.cs new file mode 100644 index 0000000..ed62267 --- /dev/null +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Models/EquipmentReceiving.cs @@ -0,0 +1,53 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Models +{ + internal class EquipmentReceiving : IEquipmentReceivingModel + { + + public DateTime? DateImplement { get; private set; } + + public int Id { get; set; } + public EquipmentReceivingStatus Status { get; private set; } = EquipmentReceivingStatus.Неизвестен; + + public static EquipmentReceiving? Create(EquipmentReceivingBindingModel? model) + { + if (model == null) + { + return null; + } + return new EquipmentReceiving + { + Status = model.Status, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(EquipmentReceivingBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public EquipmentReceivingViewModel GetViewModel => new() + { + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + } +} diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Supply.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Supply.cs new file mode 100644 index 0000000..a15d047 --- /dev/null +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Models/Supply.cs @@ -0,0 +1,114 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Models +{ + internal class Supply : ISupplyModel + { + public int Id { get; set; } + [Required] + public SupplyStatus Status { get; private set; } = SupplyStatus.Неизвестен; + [Required] + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int OrderId { get; set; } + + public int ReceivingId { get; set; } + private Dictionary? _supplyOrders = + null; + [NotMapped] + public Dictionary SupplyOrders + { + get + { + if (_supplyOrders == null) + { + _supplyOrders = Orders + .ToDictionary(recPC => recPC.OrderId, recPC => + (recPC.Order as IOrderModel, recPC.Count)); + } + return _supplyOrders; + } + } + [ForeignKey("SupplyId")] + public virtual List Orders { get; set; } = new(); + [ForeignKey("SupplyId")] + public virtual List Receivings { get; set; } = new(); + + public static Supply Create(ComputerShopDatabase context, SupplyBindingModel model) + { + return new Supply + { + Id = model.Id, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Orders = model.SupplyOrders.Select(x => new + SupplyOrder + { + Order = context.Orders.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(SupplyBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public SupplyViewModel GetViewModel => new() + { + Id = Id, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + SupplyOrders = SupplyOrders + }; + public void UpdateOrders(ComputerShopDatabase context, SupplyBindingModel model) + { + var SupplyOrders = context.SupplyOrders.Where(rec => + rec.Id == model.Id).ToList(); + if (SupplyOrders != null && SupplyOrders.Count > 0) + { // удалили те, которых нет в модели + context.SupplyOrders.RemoveRange(SupplyOrders.Where(rec + => !model.SupplyOrders.ContainsKey(rec.OrderId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateOrder in SupplyOrders) + { + updateOrder.Count = model.SupplyOrders[updateOrder.OrderId].Item2; + model.SupplyOrders.Remove(updateOrder.OrderId); + } + context.SaveChanges(); + } + var supply = context.Supplies.First(x => x.Id == Id); + foreach (var pc in model.SupplyOrders) + { + context.SupplyOrders.Add(new SupplyOrder + { + Supply = supply, + Order = context.Orders.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _supplyOrders = null; + } + } +} diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Models/SupplyOrder.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Models/SupplyOrder.cs index 86c8133..73fe7f4 100644 --- a/ComputerShopProvider/ComputerShopDatabaseImplement/Models/SupplyOrder.cs +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Models/SupplyOrder.cs @@ -11,10 +11,10 @@ namespace ComputerShopDatabaseImplement.Models { public int Id { get; set; } [Required] - public int SupplyId { get; set; } - [Required] public int OrderId { get; set; } [Required] + public int SupplyId { get; set; } + [Required] public int Count { get; set; } public virtual Order Order { get; set; } = new(); public virtual Supply Supply { get; set; } = new();