Skip to content

Commit 8c04a5c

Browse files
committed
Optimized GetByOrganization on project controller
1 parent f1e31c7 commit 8c04a5c

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

Source/Api/Controllers/ProjectController.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,13 @@ public async Task<IHttpActionResult> GetAsync(int page = 1, int limit = 10, stri
7676
[Route("~/" + API_PREFIX + "/organizations/{organization:objectid}/projects")]
7777
[ResponseType(typeof(List<ViewProject>))]
7878
public async Task<IHttpActionResult> GetByOrganizationAsync(string organization, int page = 1, int limit = 10, string mode = null) {
79-
if (!String.IsNullOrEmpty(organization) && !CanAccessOrganization(organization))
79+
if (String.IsNullOrEmpty(organization) || !CanAccessOrganization(organization))
8080
return NotFound();
8181

82-
var organizationIds = new List<string>();
83-
if (!String.IsNullOrEmpty(organization) && CanAccessOrganization(organization))
84-
organizationIds.Add(organization);
85-
else
86-
organizationIds.AddRange(GetAssociatedOrganizationIds());
87-
8882
page = GetPage(page);
8983
limit = GetLimit(limit);
9084
var options = new PagingOptions { Page = page, Limit = limit };
91-
var projects = await _repository.GetByOrganizationIdsAsync(organizationIds, options);
85+
var projects = await _repository.GetByOrganizationIdAsync(organization, options, true);
9286
var viewProjects = (await MapCollectionAsync<ViewProject>(projects.Documents, true)).ToList();
9387

9488
if (!String.IsNullOrEmpty(mode) && String.Equals(mode, "stats", StringComparison.OrdinalIgnoreCase))

Source/Core/Repositories/Base/RepositoryOwnedByOrganization.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public RepositoryOwnedByOrganization(IIndexType<T> indexType, IValidator<T> vali
2020

2121
public virtual Task<FindResults<T>> GetByOrganizationIdAsync(string organizationId, PagingOptions paging = null, bool useCache = false, TimeSpan? expiresIn = null) {
2222
if (String.IsNullOrEmpty(organizationId))
23-
return Task.FromResult<FindResults<T>>(new FindResults<T>());
23+
throw new ArgumentNullException(nameof(organizationId));
2424

2525
string cacheKey = String.Concat("paged:Organization:", organizationId);
2626
return FindAsync(new ExceptionlessQuery()
@@ -32,7 +32,7 @@ public virtual Task<FindResults<T>> GetByOrganizationIdAsync(string organization
3232

3333
public Task<long> RemoveAllByOrganizationIdAsync(string organizationId) {
3434
if (String.IsNullOrEmpty(organizationId))
35-
return Task.FromResult<long>(0);
35+
throw new ArgumentNullException(nameof(organizationId));
3636

3737
return RemoveAllAsync(new ExceptionlessQuery().WithOrganizationId(organizationId));
3838
}

Source/Core/Repositories/ProjectRepository.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ public Task<FindResults<Project>> GetByOrganizationIdsAsync(ICollection<string>
3535
throw new ArgumentNullException(nameof(organizationIds));
3636

3737
if (organizationIds.Count == 0)
38-
return Task.FromResult<FindResults<Project>>(new FindResults<Project>());
38+
return Task.FromResult(new FindResults<Project>());
3939

40-
string cacheKey = organizationIds.Count == 1 ? String.Concat("paged:Organization:", organizationIds.Single()) : null;
41-
return FindAsync(new ExceptionlessQuery()
42-
.WithOrganizationIds(organizationIds)
43-
.WithPaging(paging)
44-
.WithCacheKey(cacheKey));
40+
if (organizationIds.Count == 1)
41+
return GetByOrganizationIdAsync(organizationIds.First(), paging, true);
42+
43+
return FindAsync(new ExceptionlessQuery().WithOrganizationIds(organizationIds).WithPaging(paging));
4544
}
4645

4746
public Task<FindResults<Project>> GetByNextSummaryNotificationOffsetAsync(byte hourToSendNotificationsAfterUtcMidnight, int limit = 50) {

0 commit comments

Comments
 (0)