using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.SearchModels; using BeautySaloonContracts.StoragesContracts; using BeautySaloonContracts.ViewModels; namespace BeautySaloonDatabaseImplement.Implements { public class ClientStorage : IClientStorage { public ClientViewModel? Delete(ClientBindingModel model) { using var context = new NewdbContext(); var element = context.Clients .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Clients.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public ClientViewModel? GetElement(ClientSearchModel model) { using var context = new NewdbContext(); if (model.Id.HasValue) return context.Clients .FirstOrDefault(x => x.Id == model.Id)? .GetViewModel; if (!string.IsNullOrEmpty(model.Phone)) return context.Clients .FirstOrDefault(x => x.Phone.Equals(model.Phone))? .GetViewModel; return null; } public List GetFilteredList(ClientSearchModel model) { if (string.IsNullOrEmpty(model.Surname)) { return new(); } using var context = new NewdbContext(); return context.Clients .Where(x => x.Surname.Contains(model.Surname)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new NewdbContext(); return context.Clients .Select(x => x.GetViewModel) .ToList(); } public ClientViewModel? Insert(ClientBindingModel model) { var newClient = Client.Create(model); if (newClient == null) { return null; } using var context = new NewdbContext(); context.Clients.Add(newClient); context.SaveChanges(); return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { using var context = new NewdbContext(); var client = context.Clients .FirstOrDefault(x => x.Id == model.Id); if (client == null) { return null; } client.Update(model); context.SaveChanges(); return client.GetViewModel; } } }