using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement; using ConfectioneryDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; namespace ConfectioneryDatabaseImplement.Implements { public class PastryStorage : IPastryStorage { public PastryViewModel? Delete(PastryBindingModel model) { using var context = new ConfectioneryDatabase(); var element = context.Pastries.FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Pastries.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public PastryViewModel? GetElement(PastrySearchModel model) { if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue) { return null; } using var context = new ConfectioneryDatabase(); return context.Pastries .Include(x => x.Components) .ThenInclude(x => x.Component) .FirstOrDefault (x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || (model.Id.HasValue && x.Id == model.Id) )?.GetViewModel; } public List GetFilteredList(PastrySearchModel model) { if (string.IsNullOrEmpty(model.PastryName)) { return new(); } using var context = new ConfectioneryDatabase(); return context.Pastries .Include(x => x.Components) .ThenInclude(x => x.Component) .Select(x => x.GetViewModel) .Where(x => x.PastryName.Contains(model.PastryName)) .ToList(); } public List GetFullList() { using var context = new ConfectioneryDatabase(); return context.Pastries .Include(x => x.Components) .ThenInclude(x => x.Component) .Select(x => x.GetViewModel) .ToList(); } public PastryViewModel? Insert(PastryBindingModel model) { using var context = new ConfectioneryDatabase(); var newPastry = Pastry.Create(context, model); if (newPastry == null) { return null; } context.Pastries.Add(newPastry); context.SaveChanges(); return newPastry.GetViewModel; } public PastryViewModel? Update(PastryBindingModel model) { using var context = new ConfectioneryDatabase(); using var transaction = context.Database.BeginTransaction(); try { var pastry = context.Pastries.FirstOrDefault(x => x.Id == model.Id); if (pastry == null) { return null; } pastry.Update(model); context.SaveChanges(); pastry.UpdateComponents(context, model); transaction.Commit(); return pastry.GetViewModel; } catch { transaction.Rollback(); throw; } } } }