public async Task <IActionResult> Get(CancellationToken cancellationToken) { var readType = ReadType.VisibleOnly; // Check if there is an authenticated user who is an administrator // Administrators will receive the full category list with all the properties // Non administrators (both anonymous users and authenticated users) will receive // only visible categories with only the public properties being returned var isAdministrator = IsAdministrator(); if (isAdministrator) { readType = ReadType.All; } var categories = await _query.GetCategories(readType, cancellationToken).ConfigureAwait(false); if (isAdministrator) { return(new OkObjectResult(categories)); } var publicCategories = from x in categories select new PublicCategory(x); return(new OkObjectResult(publicCategories)); }
public async Task <PublicProfile> GetPublicProfile(Guid id, CancellationToken cancellationToken) { Ensure.Guid.IsNotEmpty(id, nameof(id)); var profileTask = FindProfile(id, cancellationToken); var categoriesTask = _query.GetCategories(ReadType.VisibleOnly, cancellationToken); // We will get both the visible categories and profile here at the same time // If the profile is not found, hidden or banned then this is wasted effort getting the category // These two scenarios are unlikely however so better overall to not get these two synchronously // Also, the categories are likely to be cached such that this will be a quick call even if it is redundant await Task.WhenAll(profileTask, categoriesTask).ConfigureAwait(false); var profile = profileTask.Result; if (profile == null) { return(null); } if (profile.Status == ProfileStatus.Hidden) { return(null); } if (profile.BannedAt != null) { return(null); } // Use a copy constructor before removing unapproved categories to ensure that changes don't corrupt the reference type stored in the cache var publicProfile = new PublicProfile(profile); var categories = categoriesTask.Result; // We want to split the categories into their groups // This will make it more efficient to enumerate through the categories by their groups against the profile var categoryGroups = SplitCategoryGroups(categories); RemoveUnapprovedCategories(publicProfile, categoryGroups); return(publicProfile); }
public async Task <IEnumerable <ProfileResult> > GetProfileResults( IEnumerable <ProfileFilter> filters, CancellationToken cancellationToken) { var visibleCategories = (await _query.GetCategories(ReadType.VisibleOnly, cancellationToken).ConfigureAwait(false)) .FastToList(); var matchingProfiles = await FilterResults(filters, visibleCategories, cancellationToken).ConfigureAwait(false); // Use a copy constructor before removing unapproved categories to ensure that changes don't corrupt the reference type stored in the cache var copiedProfiles = from x in matchingProfiles select new ProfileResult(x); var cleanedProfiles = RemoveUnapprovedGenders(copiedProfiles, visibleCategories); return(cleanedProfiles); }
// GET api/<controller> public async Task <IHttpActionResult> Get() { var categories = await _categoryQuery.GetCategories(); return(Ok(categories)); }
public async Task <IHttpActionResult> GetAsync() { var categories = await Task.FromResult(_categoryQuery.GetCategories()); return(Ok(categories)); }