diff --git a/CarService/CarServiceDatabase/Implements/CustomerStorage.cs b/CarService/CarServiceDatabase/Implements/CustomerStorage.cs new file mode 100644 index 0000000..47f5f3f --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/CustomerStorage.cs @@ -0,0 +1,79 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; + +namespace CarServiceDatabase.Implements +{ + public class CustomerStorage : ICustomerStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.Customers + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CustomerSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.Customers + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public CustomerViewModel? GetElement(CustomerSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue)//ищем по Id + { + return context.Customers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + //другие варианты поиска не реализуются (заглушка для роли клиента) + return null; + } + public CustomerViewModel? Insert(CustomerBindingModel model) + { + using var context = new CarServiceDbContext(); + var newCustomer = Customer.Create(model); + if (newCustomer != null) + { + context.Customers.Add(newCustomer); + context.SaveChanges(); + return newCustomer.GetViewModel; + } + return null; + } + public CustomerViewModel? Update(CustomerBindingModel model) + { + using var context = new CarServiceDbContext(); + var customer = context.Customers + .FirstOrDefault(x => x.Id == model.Id); + if (customer == null) + { + return null; + } + customer.Update(model); + context.SaveChanges(); + return customer.GetViewModel; + } + public CustomerViewModel? Delete(CustomerBindingModel model) + { + using var context = new CarServiceDbContext(); + var customer = context.Customers + .FirstOrDefault(x => x.Id == model.Id); + if (customer == null) + { + return null; + } + context.Customers.Remove(customer); + context.SaveChanges(); + return customer.GetViewModel; + } + } +} diff --git a/CarService/CarServiceDatabase/Implements/VehicleStorage.cs b/CarService/CarServiceDatabase/Implements/VehicleStorage.cs new file mode 100644 index 0000000..052ae49 --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/VehicleStorage.cs @@ -0,0 +1,78 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; + +namespace CarServiceDatabase.Implements +{ + public class VehicleStorage : IVehicleStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.Vehicles + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(VehicleSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.Vehicles + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public VehicleViewModel? GetElement(VehicleSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue)//ищем по Id + { + return context.Vehicles.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public VehicleViewModel? Insert(VehicleBindingModel model) + { + using var context = new CarServiceDbContext(); + var newVehicle = Vehicle.Create(model); + if (newVehicle != null) + { + context.Vehicles.Add(newVehicle); + context.SaveChanges(); + return newVehicle.GetViewModel; + } + return null; + } + public VehicleViewModel? Update(VehicleBindingModel model) + { + using var context = new CarServiceDbContext(); + var vehicle = context.Vehicles + .FirstOrDefault(x => x.Id == model.Id); + if (vehicle == null) + { + return null; + } + vehicle.Update(model); + context.SaveChanges(); + return vehicle.GetViewModel; + } + public VehicleViewModel? Delete(VehicleBindingModel model) + { + using var context = new CarServiceDbContext(); + var vehicle = context.Vehicles + .FirstOrDefault(x => x.Id == model.Id); + if (vehicle == null) + { + return null; + } + context.Vehicles.Remove(vehicle); + context.SaveChanges(); + return vehicle.GetViewModel; + } + } +} diff --git a/CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs b/CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs new file mode 100644 index 0000000..d066068 --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs @@ -0,0 +1,78 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarServiceDatabase.Implements +{ + public class WorkInRequestStorage : IWorkInRequestStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.WorksInRequest + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(WorkInRequestSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.WorksInRequest + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public WorkInRequestViewModel? GetElement(WorkInRequestSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue) + { + return context.WorksInRequest + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public WorkInRequestViewModel? Insert(WorkInRequestBindingModel model) + { + using var context = new CarServiceDbContext(); + var newWorkInRequest = WorkInRequest.Create(model); + if (newWorkInRequest != null) + { + context.WorksInRequest.Add(newWorkInRequest); + context.SaveChanges(); + return newWorkInRequest.GetViewModel; + } + return null; + } + public WorkInRequestViewModel? Update(WorkInRequestBindingModel model) + { + using var context = new CarServiceDbContext(); + var workInRequest = context.WorksInRequest.FirstOrDefault(x => x.Id == model.Id); + if (workInRequest == null) + { + return null; + } + workInRequest.Update(model); + context.SaveChanges(); + return workInRequest.GetViewModel; + } + public WorkInRequestViewModel? Delete(WorkInRequestBindingModel model) + { + using var context = new CarServiceDbContext(); + var workInRequest = context.WorksInRequest.FirstOrDefault(x => x.Id == model.Id); + if (workInRequest == null) + { + return null; + } + context.WorksInRequest.Remove(workInRequest); + context.SaveChanges(); + return workInRequest.GetViewModel; + } + } +} diff --git a/CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs b/CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs new file mode 100644 index 0000000..953b47a --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs @@ -0,0 +1,77 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; + +namespace CarServiceDatabase.Implements +{ + public class WorkPaymentStorage : IWorkPaymentStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.WorkPayments + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(WorkPaymentSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.WorkPayments + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public WorkPaymentViewModel? GetElement(WorkPaymentSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue) + { + return context.WorkPayments + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public WorkPaymentViewModel? Insert(WorkPaymentBindingModel model) + { + using var context = new CarServiceDbContext(); + var newWorkPayment = WorkPayment.Create(model); + if (newWorkPayment != null) + { + context.WorkPayments.Add(newWorkPayment); + context.SaveChanges(); + return newWorkPayment.GetViewModel; + } + return null; + } + public WorkPaymentViewModel? Update(WorkPaymentBindingModel model) + { + using var context = new CarServiceDbContext(); + var workPayment = context.WorkPayments.FirstOrDefault(x => x.Id == model.Id); + if (workPayment == null) + { + return null; + } + workPayment.Update(model); + context.SaveChanges(); + return workPayment.GetViewModel; + } + public WorkPaymentViewModel? Delete(WorkPaymentBindingModel model) + { + using var context = new CarServiceDbContext(); + var workPayment = context.WorkPayments.FirstOrDefault(x => x.Id == model.Id); + if (workPayment == null) + { + return null; + } + context.WorkPayments.Remove(workPayment); + context.SaveChanges(); + return workPayment.GetViewModel; + } + } +}