using SewingDressesContracts.BindingModels; using SewingDressesContracts.SearchModels; using SewingDressesContracts.StoragesContracts; using SewingDressesContracts.ViewModels; using SewingDressesDatabaseImplement.Models; namespace SewingDressesDatabaseImplement.Implements { public class ClientStorage : IClientStorage { public ClientViewModel? Insert(ClientBindingModel model) { using var context = new SewingDressesDatabase(); var newClient = Client.Create(model); if (newClient == null) { return null; } context.Clients.Add(newClient); context.SaveChanges(); return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { using var context = new SewingDressesDatabase(); var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); if (client == null) { return null; }; client.Update(model); context.SaveChanges(); return client.GetViewModel; } public ClientViewModel? Delete(ClientBindingModel model) { using var context = new SewingDressesDatabase(); var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); if (client == null) { return null; } context.Clients.Remove(client); context.SaveChanges(); return client.GetViewModel; } public ClientViewModel? GetElement(ClientSearchModel model) { if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) { return null; } using var context = new SewingDressesDatabase(); return context.Clients.FirstOrDefault(x => (!(string.IsNullOrEmpty(model.Email)) && model.Email == x.Email) || (model.Id.HasValue && model.Id == x.Id))?.GetViewModel; } public List GetFilteredList(ClientSearchModel model) { if (string.IsNullOrEmpty(model.Email)) { return new(); } using var context = new SewingDressesDatabase(); return context.Clients.Where(x => x.Email.Equals(model.Email)).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new SewingDressesDatabase(); return context.Clients.Select(x => x.GetViewModel).ToList(); } } }