-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCaptionQueries.cs
52 lines (48 loc) · 1.93 KB
/
CaptionQueries.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using ClassTranscribeDatabase.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ClassTranscribeDatabase
{
/// <summary>
/// Some commonly used queries to get the captions for a given videoId.
/// </summary>
public class CaptionQueries
{
private readonly CTDbContext _context;
public CaptionQueries(CTDbContext context)
{
_context = context;
}
/// <summary>
/// Get the captions for a given videoId
/// </summary>
/// <param name="language">Language of the captions to fetch.</param>
public async Task<List<Caption>> GetCaptionsAsync(string videoId, string language = "en-US")
{
try
{
var transcriptionId = _context.Transcriptions.Where(t => t.Language == language && t.VideoId == videoId
&& t.TranscriptionType == TranscriptionType.Caption).First().Id;
return await GetCaptionsAsync(transcriptionId);
}
catch (System.InvalidOperationException)
{
// If Transcriptions do not exist then First() will throw InvalidOperationException
return new List<Caption>();
}
}
/// <summary>
/// Get the captions for a given transcriptionId
/// </summary>
public async Task<List<Caption>> GetCaptionsAsync(string transcriptionId)
{
// This has to be split in two because of https://docs.microsoft.com/en-us/ef/core/querying/client-eval
var allCaptions = await _context.Captions.Where(c => c.TranscriptionId == transcriptionId).ToListAsync();
var captions = allCaptions.GroupBy(c => c.Index).Select(g => g.OrderByDescending(c => c.CreatedAt).First())
.OrderBy(c => c.Index).ToList();
return captions;
}
}
}