public static void UpdateDataList(this IHasAdHocDataList entity, UnitOfWork uow,
                                          Organisation org, bool newIsAdHoc, IEnumerable <DataListItem> newAdHocItems,
                                          IEnumerable <DataListItem> deletedAdHocItems, Guid?newDataListId, DataList oldDataList)
        {
            if (oldDataList != null && !newIsAdHoc && newDataListId != null && oldDataList.IsAdHoc)
            {
                uow.DataListsRepository.Delete(oldDataList);
            }

            if (!newIsAdHoc)
            {
                return;
            }

            var dbDataList = (oldDataList?.IsAdHoc ?? false) ? oldDataList : null;

            if (dbDataList == null)
            {
                dbDataList = new DataList()
                {
                    Name = string.Empty, OrganisationId = org.Id
                };
            }

            uow.DataListsRepository.InsertOrUpdate(dbDataList);
            entity.DataList = dbDataList;

            var order = 1;

            foreach (var val in newAdHocItems)
            {
                var dbItem = uow.DataListItemsRepository.Find(val.Id);
                if (dbItem == null)
                {
                    dbItem = new DataListItem();
                }

                dbItem.Text     = val.Text;
                dbItem.Value    = val.Value;
                dbItem.DataList = dbDataList;
                dbItem.Order    = order++;
                uow.DataListItemsRepository.InsertOrUpdate(dbItem);
            }

            foreach (var val in deletedAdHocItems)
            {
                var dbItem = uow.DataListItemsRepository.Find(val.Id);
                if (dbItem == null)
                {
                    return;
                }

                uow.DataListItemsRepository.Delete(dbItem);
            }
        }
Exemple #2
0
        public void MoveDown(DataListItem item)
        {
            var otherItems = this.Items.Where(i => i.Order > item.Order).ToList();

            if (!otherItems.Any())
            {
                return;
            }

            var MinBigger = otherItems.Min(i => i.Order);

            if (MinBigger > 0)
            {
                var bigger = otherItems.Where(i => i.Order == MinBigger).First();
                bigger.Order = item.Order;
                item.Order   = MinBigger;
            }
        }
Exemple #3
0
        public void MoveUp(DataListItem item)
        {
            var otherItems = this.Items.Where(i => i.Order < item.Order).ToList();

            if (!otherItems.Any())
            {
                return;
            }

            var MaxSmaller = otherItems.Max(i => i.Order);

            if (MaxSmaller > 0)
            {
                var smaller = otherItems.Where(i => i.Order == MaxSmaller).Last();
                smaller.Order = item.Order;
                item.Order    = MaxSmaller;
            }
        }