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 { public 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.Supply.Component.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; } } }