77 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabase.Models;
namespace LawFirmDatabase.Implements
{
public class CustomerStorage : ICustomerStorage
{
public List<CustomerViewModel> GetFullList()
{
using var context = new LawFirmDBContext();
return context.Customers.Select(x => x.GetViewModel).ToList();
}
public List<CustomerViewModel> GetFilteredList(CustomerSearchModel model)
{
using var context = new LawFirmDBContext();
return context.Customers.Select(x => x.GetViewModel).Where(x => x.Id == model.Id).ToList();
}
public CustomerViewModel? GetElement(CustomerSearchModel model)
{
using var context = new LawFirmDBContext();
if (model.Id.HasValue)
{
return context.Customers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
else if (!string.IsNullOrEmpty(model.Login))
{
return context.Customers.FirstOrDefault(x => x.Login == model.Login)?.GetViewModel;
}
return null;
}
public CustomerViewModel? Insert(CustomerBindingModel model)
{
using var context = new LawFirmDBContext();
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 LawFirmDBContext();
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 LawFirmDBContext();
var customer = context.Customers.FirstOrDefault(x => x.Id == model.Id);
if (customer == null)
{
return null;
}
context.Customers.Remove(customer);
context.SaveChanges();
return customer.GetViewModel;
}
}
}