80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
|
using CarServiceContracts.BindingModels;
|
|||
|
using CarServiceContracts.SearchModels;
|
|||
|
using CarServiceContracts.StorageContracts;
|
|||
|
using CarServiceContracts.ViewModels;
|
|||
|
using CarServiceDatabase.Models;
|
|||
|
|
|||
|
namespace CarServiceDatabase.Implements
|
|||
|
{
|
|||
|
public class CustomerStorage : ICustomerStorage
|
|||
|
{
|
|||
|
public List<CustomerViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new CarServiceDbContext();
|
|||
|
return context.Customers
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
public List<CustomerViewModel> 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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|