using Microsoft.EntityFrameworkCore; using PlumbingRepairContracts.BindingModels; using PlumbingRepairContracts.SearchModels; using PlumbingRepairContracts.StoragesContracts; using PlumbingRepairContracts.ViewModels; using PlumbingRepairDataBaseImplement.Models; using PlumbingRepairDataModels.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PlumbingRepairDataBaseImplement.Implements { public class StoreStorage : IStoreStorage { public StoreViewModel? Delete(StoreBindingModel model) { using var context = new PlumbingRepairDataBase(); var element = context.Stores .Include(x => x.Works) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Stores.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public StoreViewModel? GetElement(StoreSearchModel model) { if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue) { return null; } using var context = new PlumbingRepairDataBase(); return context.Stores .Include(x => x.Works) .ThenInclude(x => x.Work) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.StoreName) && x.StoreName == model.StoreName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public List GetFilteredList(StoreSearchModel model) { if (string.IsNullOrEmpty(model.StoreName)) { return new(); } using var context = new PlumbingRepairDataBase(); return context.Stores .Include(x => x.Works) .ThenInclude(x => x.Work) .Where(x => x.StoreName.Contains(model.StoreName)) .ToList() .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new PlumbingRepairDataBase(); return context.Stores .Include(x => x.Works) .ThenInclude(x => x.Work) .ToList() .Select(x => x.GetViewModel) .ToList(); } public StoreViewModel? Insert(StoreBindingModel model) { using var context = new PlumbingRepairDataBase(); var newStore = Store.Create(context, model); if (newStore == null) { return null; } context.Stores.Add(newStore); context.SaveChanges(); return newStore.GetViewModel; } public bool SellWork(IWorkModel model, int quantity) { using var context = new PlumbingRepairDataBase(); using var transaction = context.Database.BeginTransaction(); try { foreach (var store in context.Stores .Include(x => x.Works) .ThenInclude(x => x.Work) .ToList() .Where(x => x.StoreWorks.ContainsKey(model.Id))) { int countInCurrentStore = store.StoreWorks[model.Id].Item2; if (countInCurrentStore <= quantity) { var elem = context.StoreWorks .Where(x => x.WorkId == model.Id) .FirstOrDefault(x => x.StoreId == store.Id); context.StoreWorks.Remove(elem); store.StoreWorks.Remove(model.Id); quantity -= countInCurrentStore; } else { store.StoreWorks[model.Id] = (store.StoreWorks[model.Id].Item1, countInCurrentStore - quantity); quantity = 0; store.UpdateWorks(context, new() { Id = store.Id, StoreWorks = store.StoreWorks, }); } if (quantity == 0) { context.SaveChanges(); transaction.Commit(); return true; } } transaction.Rollback(); return false; } catch { transaction.Rollback(); throw; } } public StoreViewModel? Update(StoreBindingModel model) { using var context = new PlumbingRepairDataBase(); using var transaction = context.Database.BeginTransaction(); try { var store = context.Stores.FirstOrDefault(rec => rec.Id == model.Id); if (store == null) { return null; } store.Update(model); context.SaveChanges(); store.UpdateWorks(context, model); transaction.Commit(); return store.GetViewModel; } catch { transaction.Rollback(); throw; } } } }