using BeautySalonContracts.BindingModels; using BeautySalonContracts.SearchModels; using BeautySalonContracts.StoragesContracts; using BeautySalonContracts.ViewModels; using BeautySalonDatabaseImplement.Models; namespace BeautySalonDatabaseImplement.Implements { public class ClientStorage : IClientStorage { public ClientViewModel? Delete(ClientBindingModel model) { using var context = new BeautySalonDatabase(); 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) { if (string.IsNullOrEmpty(model.PhoneNumber) && !model.Id.HasValue) { return null; } using var context = new BeautySalonDatabase(); return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PhoneNumber) && x.PhoneNumber == model.PhoneNumber) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(ClientSearchModel model) { if (string.IsNullOrEmpty(model.PhoneNumber)) { return new(); } using var context = new BeautySalonDatabase(); return context.Clients .Where(x => x.PhoneNumber.Equals(model.PhoneNumber)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new BeautySalonDatabase(); 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 BeautySalonDatabase(); context.Clients.Add(newClient); context.SaveChanges(); return newClient.GetViewModel; } public ClientViewModel? Update(ClientBindingModel model) { using var context = new BeautySalonDatabase(); var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); if (client == null) { return null; } client.Update(model); context.SaveChanges(); return client.GetViewModel; } } }