@@ -24,13 +24,16 @@ public class CaptionsController : BaseController
2424 private readonly CaptionQueries _captionQueries ;
2525 private readonly SubParser parser = new SubParser ( ) ;
2626
27+ private ILogger < CaptionsController > _logger ;
28+
2729 public CaptionsController ( WakeDownloader wakeDownloader ,
2830 CTDbContext context ,
2931 CaptionQueries captionQueries ,
3032 ILogger < CaptionsController > logger ) : base ( context , logger )
3133 {
3234 _captionQueries = captionQueries ;
3335 _wakeDownloader = wakeDownloader ;
36+ _logger = logger ;
3437 }
3538
3639 // GET: api/Captions/ByTranscription/5
@@ -72,6 +75,9 @@ public async Task<ActionResult<string>> GetTranscriptionFile(string Transcriptio
7275 [ HttpGet ]
7376 public async Task < ActionResult < Caption > > GetCaption ( string transcriptionId , int index )
7477 {
78+ // Note here that captions are effectively "stacked" on top of all other captions with the
79+ // same transcription Id and Index.
80+ // Then, getting a caption only returns the top caption of the stack with the newest creation date.
7581 var captions = await _context . Captions . Where ( c => c . TranscriptionId == transcriptionId && c . Index == index )
7682 . OrderByDescending ( c => c . CreatedAt ) . ToListAsync ( ) ;
7783 if ( captions == null || captions . Count == 0 )
@@ -88,6 +94,9 @@ public async Task<ActionResult<Caption>> GetCaption(string transcriptionId, int
8894 [ HttpPost ]
8995 public async Task < ActionResult < Caption > > PostCaption ( Caption modifiedCaption )
9096 {
97+ // This endpoint should handle deletion as well, which is represented by posting a caption
98+ // with the empty string as text.
99+ _logger . LogInformation ( "DEBUG Id: {Id}, Text: {Text}, Begin: {Begin}, End: {End}" , modifiedCaption . Id , modifiedCaption . Text , modifiedCaption . Begin , modifiedCaption . End ) ;
91100 if ( modifiedCaption == null || modifiedCaption . Id == null )
92101 {
93102 return BadRequest ( "modifiedCaption.Id not present" ) ;
@@ -108,7 +117,31 @@ public async Task<ActionResult<Caption>> PostCaption(Caption modifiedCaption)
108117 } ;
109118 _context . Captions . Add ( newCaption ) ;
110119 await _context . SaveChangesAsync ( ) ;
111- // nope _wakeDownloader.UpdateVTTFile(oldCaption.TranscriptionId);
120+ return newCaption ;
121+ }
122+
123+ // POST: api/Captions/Add
124+ [ HttpPost ( "Add" ) ]
125+ public async Task < ActionResult < Caption > > AddCaption ( Caption newCaption )
126+ {
127+ if ( newCaption == null ) {
128+ return BadRequest ( "newCaption not present" ) ;
129+ }
130+ var allCaptions = await _context . Captions . Where ( c => c . TranscriptionId == newCaption . TranscriptionId ) . ToListAsync ( ) ;
131+ // Every new caption must have a unique index to avoid conflicts with existing indices.
132+ var newIndex = allCaptions . Max ( c => c . Index ) + 1 ;
133+
134+ Caption addedCaption = new Caption
135+ {
136+ Begin = newCaption . Begin ,
137+ End = newCaption . End ,
138+ Index = newIndex ,
139+ CaptionType = newCaption . CaptionType ,
140+ Text = newCaption . Text ,
141+ TranscriptionId = newCaption . TranscriptionId
142+ } ;
143+ _context . Captions . Add ( addedCaption ) ;
144+ await _context . SaveChangesAsync ( ) ;
112145 return newCaption ;
113146 }
114147
0 commit comments