public List <OrderViewModel> Read(OrderBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { return(context.Orders.Where(rec => rec.Id == model.Id || (rec.ClientId == model.ClientId) && (model.Date == null && model.DateTo == null || rec.OrderDate >= model.Date && rec.OrderDate <= model.DateTo)) .Select(rec => new OrderViewModel { Id = rec.Id, ClientId = rec.ClientId, FirstName = rec.Client.FirstName, SecondName = rec.Client.SecondName, Patronymic = rec.Client.Patronymic, TotalSum = rec.TotalSum, OrderDate = rec.OrderDate, LeftSum = rec.TotalSum - context.Payments.Where(recP => recP.OrderId == rec.Id).Select(recP => recP.PaymentAmount).Sum(), Status = rec.Status, Positions = GetOrderPositionsViewModel(rec) }) .ToList()); } }
public void CreateOrUpdate(PaymentBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { Payment element = model.Id.HasValue ? null : new Payment(); if (model.Id.HasValue) { element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Payment(); context.Payments.Add(element); } element.OrderId = model.OrderId; element.ClientId = model.ClientId; element.PaymentAmount = model.PaymentAmount; element.PaymentDate = model.PaymentDate; context.SaveChanges(); } }
protected override List <T> GetList <T>() { using (var context = new FurnitureFactoryDatabase()) { return(context.Set <T>().ToList()); } }
public void CreateOrUpdate(OrderBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Order element = model.Id.HasValue ? null : new Order(); if (model.Id.HasValue) { element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } element.ClientId = model.ClientId; element.OrderDate = model.OrderDate; element.Status = model.Status; element.TotalSum = model.TotalSum; context.SaveChanges(); } else { element.ClientId = model.ClientId; element.OrderDate = model.OrderDate; element.TotalSum = model.TotalSum; element.Status = model.Status; context.Orders.Add(element); context.SaveChanges(); var groupPositions = model.Positions .GroupBy(rec => rec.FurnitureModelId) .Select(rec => new { FurnitureModelId = rec.Key, Count = rec.Sum(r => r.Count) }); foreach (var groupPosition in groupPositions) { context.Positions.Add(new Position { OrderId = element.Id, FurnitureModelId = groupPosition.FurnitureModelId, Count = groupPosition.Count }); context.SaveChanges(); } } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void SaveToDatabase() { var Models = LoadFurnitureModels(); using (var context = new FurnitureFactoryDatabase()) { foreach (var model in Models) { FurnitureModel element = context.FurnitureModels.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { break; } else { element = new FurnitureModel(); context.FurnitureModels.Add(element); } element.ModelName = model.ModelName; element.Price = model.Price; element.TypeName = model.TypeName; element.Dimensions = model.Dimensions; context.SaveChanges(); } } }
public void CreateOrUpdate(ClientBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { Client element = model.Id.HasValue ? null : new Client(); if (model.Id.HasValue) { element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Client(); context.Clients.Add(element); } element.FirstName = model.FirstName; element.SecondName = model.SecondName; element.Patronymic = model.Patronymic; element.PhoneNumber = model.PhoneNumber; element.Email = model.Email; element.Block = model.Block; element.Password = model.Password; context.SaveChanges(); } }
protected override List <PropertyInfo> GetFullList() { using (var context = new FurnitureFactoryDatabase()) { Type type = context.GetType(); return(type.GetProperties().Where(x => x.PropertyType.FullName.StartsWith("Microsoft.EntityFrameworkCore.DbSet")).ToList()); } }
public List <FurnitureTypeViewModel> Read(FurnitureTypeBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { return(context.FurnitureTypes.Where(rec => rec.Id == model.Id) .Select(rec => new FurnitureTypeViewModel { Id = rec.Id, TypeName = rec.TypeName }) .ToList()); } }
public List <PaymentViewModel> Read(PaymentBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { return(context.Payments .Where(rec => model == null || rec.Id == model.Id || rec.OrderId.Equals(model.OrderId)) .Select(rec => new PaymentViewModel { Id = rec.Id, ClientId = rec.ClientId, OrderId = rec.OrderId, PaymentAmount = rec.PaymentAmount, PaymentDate = rec.PaymentDate }) .ToList()); } }
public void Delete(PaymentBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { Payment element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id.Value); if (element != null) { context.Payments.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
public List <PositionViewModel> Read(PositionBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { return(context.Positions.Where(rec => rec.Id == model.Id || model == null || rec.OrderId.Equals(model.OrderId)) .Select(rec => new PositionViewModel { Id = rec.Id, Count = rec.Count, FurnitureModelId = rec.FurnitureModelId, ModelName = rec.FurnitureModel.ModelName, Price = rec.Count * rec.FurnitureModel.Price, OrderId = rec.OrderId, TypeName = rec.FurnitureModel.TypeName }) .ToList()); } }
public List <ClientViewModel> Read(ClientBindingModel model) { using (var context = new FurnitureFactoryDatabase()) { return(context.Clients .Where(rec => model == null || rec.Id == model.Id || rec.Email == model.Email && (model.Password == null || rec.Password == model.Password)) .Select(rec => new ClientViewModel { Id = rec.Id, FirstName = rec.FirstName, SecondName = rec.SecondName, Patronymic = rec.Patronymic, Email = rec.Email, Password = rec.Password, PhoneNumber = rec.PhoneNumber, Block = rec.Block }) .ToList()); } }
public static List <PositionViewModel> GetOrderPositionsViewModel(Order order) { using (var context = new FurnitureFactoryDatabase()) { var Positions = context.Positions .Where(rec => rec.OrderId == order.Id) .Include(rec => rec.FurnitureModel) .Select(rec => new PositionViewModel { Id = rec.Id, OrderId = rec.OrderId, FurnitureModelId = rec.FurnitureModelId, Count = rec.Count }).ToList(); foreach (var position in Positions) { var positionData = context.FurnitureModels.Where(rec => rec.Id == position.FurnitureModelId).FirstOrDefault(); position.TypeName = positionData.TypeName; position.ModelName = positionData.ModelName; position.Price = positionData.Price; } return(Positions); } }