diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index 8a8efc6..cb49d55 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -1,7 +1,7 @@ using AutomobilePlantBusinessLogic.BusinessLogics; using AutomobilePlantContracts.BusinessLogicsContracts; using AutomobilePlantContracts.StoragesContracts; -using AutomomilePlantFileImplement.Implements; +using AutomobilePlantDataBaseImplements.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/CarStorage.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/CarStorage.cs new file mode 100644 index 0000000..bc56a38 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/CarStorage.cs @@ -0,0 +1,111 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataBaseImplements.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDataBaseImplements.Implements +{ + public class CarStorage : ICarStorage + { + public List GetFullList() + { + using var context = new AutoPlantDataBase(); + return context.Cars + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName)) + { + return new(); + } + using var context = new AutoPlantDataBase(); + return context.Cars + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.CarName.Contains(model.CarName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public CarViewModel? GetElement(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue) + { + return null; + } + using var context = new AutoPlantDataBase(); + return context.Cars + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CarName) && x.CarName == model.CarName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public CarViewModel? Insert(CarBindingModel model) + { + using var context = new AutoPlantDataBase(); + var newCar = Car.Create(context, model); + if (newCar == null) + { + return null; + } + context.Cars.Add(newCar); + context.SaveChanges(); + return newCar.GetViewModel; + } + + public CarViewModel? Update(CarBindingModel model) + { + using var context = new AutoPlantDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var car = context.Cars.FirstOrDefault(rec => rec.Id == model.Id); + if (car == null) + { + return null; + } + car.Update(model); + context.SaveChanges(); + car.UpdateComponents(context, model); + transaction.Commit(); + return car.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public CarViewModel? Delete(CarBindingModel model) + { + using var context = new AutoPlantDataBase(); + var element = context.Cars + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Cars.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/ComponentStorage.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/ComponentStorage.cs new file mode 100644 index 0000000..fe818fc --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataBaseImplements.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDataBaseImplements.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new AutoPlantDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AutoPlantDataBase(); + 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 AutoPlantDataBase(); + 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 AutoPlantDataBase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AutoPlantDataBase(); + 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 AutoPlantDataBase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/OrderStorage.cs b/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/OrderStorage.cs new file mode 100644 index 0000000..0c29eba --- /dev/null +++ b/AutomobilePlant/AutomobilePlantDataBaseImplements/Implements/OrderStorage.cs @@ -0,0 +1,102 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomobilePlantDataBaseImplements.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantDataBaseImplements.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new AutoPlantDataBase(); + return context.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (model.Id.HasValue) + { + return new(); + } + using var context = new AutoPlantDataBase(); + return context.Orders + .Where(x => x.Id == model.Id) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new AutoPlantDataBase(); + return context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new AutoPlantDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new AutoPlantDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + transaction.Commit(); + return order.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new AutoPlantDataBase(); + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +}