public virtual async Task <NavBarItemModel> PrepareNavBarItemModelAsync(TblNavBarItems item) { NavBarItemModel result; if (item == null) { result = new NavBarItemModel(); } else { result = item.Adapt <NavBarItemModel>(); await item.LoadAllLocalizedStringsToModelAsync(result); } return(result); }
public virtual async Task UpdateAsync(TblNavBarItems record) { var allItems = (await GetAsEnumerableAsync()).ToList(); var oldRecord = allItems.FirstOrDefault(p => p.Id == record.Id); allItems.RemoveWhere(p => p.Id == record.Id); allItems.Add(record); if (DetectLoop(allItems, record, null)) { throw new Exception("Self referencing loop detected"); } _dbContext.NavBarItems.AddOrUpdate(record); await _dbContext.SaveChangesAsync(); QueryCacheManager.ExpireTag(CacheTags.NavbarItem); _eventPublisher.EntityUpdated(record, oldRecord); }
protected virtual bool DetectLoop(List <TblNavBarItems> allItems, TblNavBarItems item, HashSet <TblNavBarItems> visited) { visited = visited ?? new HashSet <TblNavBarItems>(); var parentItem = allItems.FirstOrDefault(p => p.Id == item.ParentItemId); if (parentItem != null) { if (!visited.Contains(parentItem)) { visited.Add(parentItem); return(DetectLoop(allItems, parentItem, visited)); } else { return(true); } } return(false); }
public virtual async Task <int> AddAsync(TblNavBarItems record) { if (record.Index == -1) { var maxIdx = _dbContext.NavBarItems.Max(p => (int?)p.Index); record.Index = (maxIdx ?? 0) + 1; } else { await _dbContext.NavBarItems.Where(p => p.Index >= record.Index).UpdateAsync(p => new TblNavBarItems() { Index = p.Index + 1 }); } _dbContext.NavBarItems.Add(record); await _dbContext.SaveChangesAsync(); QueryCacheManager.ExpireTag(CacheTags.NavbarItem); _eventPublisher.EntityInserted(record); return(record.Id); }