using BeautySalonContracts.BindingModels; using BeautySalonContracts.SearchModels; using BeautySalonContracts.StoragesContracts; using BeautySalonContracts.ViewModels; using BeautySalonDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; namespace BeautySalonDatabaseImplement.Implements { public class VisitStorage : IVisitStorage { public VisitViewModel? Delete(VisitBindingModel model) { using var context = new BeautySalonDatabase(); var element = context.Visits.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Visits.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public VisitViewModel? GetElement(VisitSearchModel model) { if (!model.Id.HasValue) { return null; } using var context = new BeautySalonDatabase(); return context.Visits.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(VisitSearchModel model) { if (!model.Id.HasValue) { return new(); } using var context = new BeautySalonDatabase(); return context.Visits.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new BeautySalonDatabase(); return context.Visits.Select(x => x.GetViewModel).ToList(); } public VisitViewModel? Insert(VisitBindingModel model) { var newVisit = Visit.Create(model); if (newVisit == null) { return null; } using var context = new BeautySalonDatabase(); context.Visits.Add(newVisit); context.SaveChanges(); return newVisit.GetViewModel; } public VisitViewModel? Update(VisitBindingModel model) { using var context = new BeautySalonDatabase(); var order = context.Visits.FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; } order.Update(model); context.SaveChanges(); return order.GetViewModel; } } }