Optimized db queries
This commit is contained in:
@@ -12,5 +12,11 @@ public class TimelineController(ITimelineRepository repository) : ControllerBase
|
||||
var timeline = await repository.GetTimeline(type, ct);
|
||||
return Ok(timeline);
|
||||
}
|
||||
|
||||
[HttpGet("featured")]
|
||||
public async Task<IActionResult> GetFeaturedTimeline(CancellationToken ct) {
|
||||
var timeline = await repository.GetFeaturedTimeline(ct);
|
||||
return Ok(timeline);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,5 +11,10 @@ internal sealed class TimelineRepository(DatabaseContext context) : ITimelineRep
|
||||
.Where(entry => entry.Type == type)
|
||||
.ToArrayAsync(ct);
|
||||
}
|
||||
|
||||
|
||||
public async Task<IEnumerable<TimelineEntry>> GetFeaturedTimeline(CancellationToken ct) {
|
||||
return await context.Timeline
|
||||
.Where(entry => entry.Featured)
|
||||
.ToArrayAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,6 @@ public interface ITimelineRepository {
|
||||
|
||||
Task<IEnumerable<TimelineEntry>> GetTimeline(TimelineEntryType type, CancellationToken ct);
|
||||
|
||||
Task<IEnumerable<TimelineEntry>> GetFeaturedTimeline(CancellationToken ct);
|
||||
|
||||
}
|
||||
@@ -104,12 +104,7 @@
|
||||
var technologies = await TechnologyRepository.GetTechnologies(TokenSource.Token);
|
||||
_technologies = technologies.Where(t => t.Featured);
|
||||
|
||||
var carrierTimeline = await TimelineRepository.GetTimeline(TimelineEntryType.Carrier, TokenSource.Token);
|
||||
var experienceTimeline = await TimelineRepository.GetTimeline(TimelineEntryType.Experience, TokenSource.Token);
|
||||
_timeline = experienceTimeline
|
||||
.Aggregate(carrierTimeline, (current, entry) => current.Append(entry))
|
||||
.Where(t => t.Featured)
|
||||
.OrderBy(t => t.Date);
|
||||
_timeline = await TimelineRepository.GetFeaturedTimeline(TokenSource.Token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,4 +12,13 @@ internal sealed class TimelineRepository(IHttpClientFactory factory) : ITimeline
|
||||
var data = await result.Content.ReadFromJsonAsync<IEnumerable<TimelineEntry>>(ct);
|
||||
return data ?? [];
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TimelineEntry>> GetFeaturedTimeline(CancellationToken ct) {
|
||||
var client = factory.CreateClient("api");
|
||||
var result = await client.GetAsync("api/timeline/featured", ct);
|
||||
if (!result.IsSuccessStatusCode) return [];
|
||||
|
||||
var data = await result.Content.ReadFromJsonAsync<IEnumerable<TimelineEntry>>(ct);
|
||||
return data ?? [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user