Пример #1
0
        public ResturantEditViewModel Add(ResturantEditViewModel Resturant)
        {
            Resturant _Resturant = ResturantRepo.Add(Resturant.ToModel());

            UnitOfWork.commit();
            if (Resturant.ResturantImages != null && Resturant.ResturantImages.Count() > 0)
            {
                foreach (var image in Resturant.ResturantImages)
                {
                    image.ResturantID = _Resturant.ID;
                    ImagRepo.Add(image.ToModel());
                }
            }

            if (Resturant.Items != null && Resturant.Items.Count() > 0)
            {
                foreach (var item in Resturant.Items)
                {
                    item.ResturantID = _Resturant.ID;
                    Item Inserted = ItemRepo.Add(item.ToModel());
                    UnitOfWork.commit();
                    if (item.ItemDetail != null && item.ItemDetail.Count() > 0)
                    {
                        foreach (var inner in item.ItemDetail)
                        {
                            inner.ItemID = Inserted.ID;
                            ItemSizeRepo.Add(inner.ToModel());
                        }
                    }
                }
            }
            UnitOfWork.commit();
            return(_Resturant.ToEditViewModel());
        }
Пример #2
0
        public ResturantEditViewModel Update(ResturantEditViewModel Resturant)
        {
            Resturant _Resturant = ResturantRepo.Update(Resturant.ToModel());

            if (Resturant.ResturantImages != null && Resturant.ResturantImages.Count() > 0)
            {
                var toAdd = Resturant.ResturantImages.Where(i => i.ID == 0);
                foreach (var image in toAdd)
                {
                    ImagRepo.Add(image.ToModel());
                }

                var toUpdate = Resturant.ResturantImages.Where(i => i.ID > 0);
                foreach (var image in toUpdate)
                {
                    ImagRepo.Update(image.ToModel());
                }

                int[] ids      = Resturant.ResturantImages.Select(x => x.ID).ToArray();
                var   toDelete = ImagRepo.GetFilter(i => !ids.Contains(i.ID) && i.ResturantID == Resturant.ID);
                foreach (var image in toDelete)
                {
                    ImagRepo.Remove(image);
                }
            }
            if (Resturant.Items != null && Resturant.Items.Count() > 0)
            {
                var toAdd = Resturant.Items.Where(i => i.ID == 0);
                foreach (var item in toAdd)
                {
                    var inserted = ItemRepo.Add(item.ToModel());
                    UnitOfWork.commit();
                    if (item.ItemDetail != null && item.ItemDetail.Count() > 0)
                    {
                        foreach (var innerItem in item.ItemDetail)
                        {
                            innerItem.ItemID = inserted.ID;
                            ItemSizeRepo.Add(innerItem.ToModel());
                        }
                    }
                }


                var toUpdate = Resturant.Items.Where(i => i.ID > 0);
                foreach (var itemToUpdate in toUpdate)
                {
                    ItemRepo.Update(itemToUpdate.ToModel());

                    var itemDetailsToAdd
                        = itemToUpdate.ItemDetail.Where(i => i.ID == 0);
                    foreach (var detail in itemDetailsToAdd)
                    {
                        ItemSizeRepo.Add(detail.ToModel());
                    }


                    var itemsToBeUpdated
                        = itemToUpdate.ItemDetail.Where(i => i.ID > 0);

                    foreach (var detail in itemsToBeUpdated)
                    {
                        ItemSizeRepo.Update(detail.ToModel());
                    }

                    int[] ids = itemToUpdate.ItemDetail.Select
                                    (x => x.ID).ToArray();
                    var itemsToDeleted
                        = ItemSizeRepo
                          .GetFilter(i => !ids.Contains(i.ID) && i.ItemID == itemToUpdate.ID);

                    foreach (var detail in itemsToDeleted)
                    {
                        ItemSizeRepo.Remove(detail);
                    }
                }


                var idsOfItems = Resturant.Items.Select(X => X.ID)
                                 .ToArray();

                var DeletedItems
                    = ItemRepo.GetFilter
                          (i => !idsOfItems.Contains(i.ID) && i.RestrantID == Resturant.ID);

                foreach (var item in DeletedItems)
                {
                    ItemRepo.Remove(item);
                }
            }
            UnitOfWork.commit();
            return(_Resturant.ToEditViewModel());
        }