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 GetFullList() { using var context = new LawFirmDBContext(); return context.Customers .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(CustomerSearchModel model) { using var context = new LawFirmDBContext(); 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 LawFirmDBContext(); if (model.Id.HasValue) { return context.Customers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } else if (!string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password)) { return context.Customers .FirstOrDefault(x => x.Login == model.Login)?.GetViewModel; } else if (!string.IsNullOrEmpty(model.Login) && !string.IsNullOrEmpty(model.Password)) { return context.Customers .FirstOrDefault(x => x.Login == model.Login && x.Password == model.Password)?.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; } } }