public async Task<TagRelatedEntitiesResponse> GetRealmsWithTags(ClaimsPrincipal claim, TagRelatedEntitiesRequest request) { var response = new TagRelatedEntitiesResponse(); var user = await _userService.GetCurrentUser(claim); var userId = user == null ? string.Empty : user.Id; // Realms valid to user var realms = await _realmDataService.GetRealms(userId); // Realms for these tags var tagOptions = await _tagDataService .GetTagsForScopeQuery(TagScope.Realm) .Include(x => x.Realms) .OrderBy(x => x.Name) .ToListAsync(); var tags = tagOptions .Where(x => request.TagCodes.Contains(x.Code)) .ToList(); var tagRealmIds = tags.SelectMany(x => x.Realms.Select(r => r.Id)).Distinct(); var realmIds = tagRealmIds .Where(x => tags.All(t => t.Realms.Any(r => r.Id == x))) .ToList(); response.Data.Tags = _mapper.Map<List<TagDropdownModel>>(tags); response.Data.TagOptions = _mapper.Map<List<TagDropdownModel>>(tagOptions); response.Data.Items = _mapper.Map<List<TagRelatedItem>>( realms .Where(r => realmIds.Contains(r.Id)) .OrderBy(r => r.Name) ); return response; }
public async Task<TagRelatedEntitiesResponse> GetFragmentsWithTags(ClaimsPrincipal claim, TagRelatedEntitiesRequest request) { var response = new TagRelatedEntitiesResponse(); var user = await _userService.GetCurrentUser(claim); var userId = user == null ? string.Empty : user.Id; // Fragments for a valid to realm var realm = await _tagDataService.GetAsync<Realm>(x => x.Code == request.RealmCode); if (realm == null || (realm.IsAuthenticationRestricted && user == null) || (realm.IsPrivate && realm.ApplicationUserId != userId)) { response.ErrorMessages.Add("You are not permitted to access this realm."); return response; } // Fragments for these tags var tagOptions = await _tagDataService .GetTagsForScopeQuery(TagScope.Fragment) .Include(x => x.Fragments).ThenInclude(x => x.Realm) .Where(x => x.Fragments.Any(f => f.RealmId == realm.Id)) .OrderBy(x => x.Name) .ToListAsync(); var tags = tagOptions .Where(x => request.TagCodes.Contains(x.Code)) .ToList(); var tagFragments = tags.SelectMany(x => x.Fragments).Distinct(); var fragments = tagFragments .Where(x => tags.All(t => t.Fragments.Any(f => f.Id == x.Id))) .ToList(); response.Data.RealmName = realm.Name; response.Data.Tags = _mapper.Map<List<TagDropdownModel>>(tags); response.Data.TagOptions = _mapper.Map<List<TagDropdownModel>>(tagOptions); response.Data.Items = _mapper.Map<List<TagRelatedItem>>(fragments); return response; }