using IceCreamShopContracts.BindingModels; using IceCreamShopContracts.SearchModels; using IceCreamShopContracts.StoragesContracts; using IceCreamShopContracts.ViewModels; using IceCreamShopDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IceCreamShopDatabaseImplement.Implements { public class IceCreamStorage : IIceCreamStorage { public List GetFullList() { using var context = new IceCreamShopDataBase(); return context.IceCreams .Include(x => x.Components) .ThenInclude(x => x.Component) .ToList() .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(IceCreamSearchModel model) { if (string.IsNullOrEmpty(model.IceCreamName)) { return new(); } using var context = new IceCreamShopDataBase(); return context.IceCreams.Include(x => x.Components) .ThenInclude(x => x.Component) .Where(x => x.IceCreamName.Contains(model.IceCreamName)) .ToList() .Select(x => x.GetViewModel) .ToList(); } public IceCreamViewModel? GetElement(IceCreamSearchModel model) { if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) { return null; } using var context = new IceCreamShopDataBase(); return context.IceCreams .Include(x => x.Components) .ThenInclude(x => x.Component) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IceCreamName) && x.IceCreamName == model.IceCreamName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public IceCreamViewModel? Insert(IceCreamBindingModel model) { using var context = new IceCreamShopDataBase(); var newIceCream = IceCream.Create(context, model); if (newIceCream == null) { return null; } context.IceCreams.Add(newIceCream); context.SaveChanges(); return newIceCream.GetViewModel; } public IceCreamViewModel? Update(IceCreamBindingModel model) { using var context = new IceCreamShopDataBase(); using var transaction = context.Database.BeginTransaction(); try { var iceCream = context.IceCreams.FirstOrDefault(rec => rec.Id == model.Id); if (iceCream == null) { return null; } iceCream.Update(model); context.SaveChanges(); iceCream.UpdateComponents(context, model); transaction.Commit(); return iceCream.GetViewModel; } catch { transaction.Rollback(); throw; } } public IceCreamViewModel? Delete(IceCreamBindingModel model) { using var context = new IceCreamShopDataBase(); var element = context.IceCreams .Include(x => x.Components) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.IceCreams.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }