11using ClassTranscribeDatabase ;
22using ClassTranscribeDatabase . Models ;
3+ using ClassTranscribeServer . Utils ;
34using Microsoft . AspNetCore . Authorization ;
45using Microsoft . AspNetCore . Http ;
56using Microsoft . AspNetCore . Mvc ;
@@ -23,14 +24,16 @@ public class CaptionsController : BaseController
2324 private readonly WakeDownloader _wakeDownloader ;
2425 private readonly CaptionQueries _captionQueries ;
2526 private readonly SubParser parser = new SubParser ( ) ;
27+ private readonly UserUtils _userUtils ;
2628
2729 public CaptionsController ( WakeDownloader wakeDownloader ,
2830 CTDbContext context ,
29- CaptionQueries captionQueries ,
31+ CaptionQueries captionQueries , UserUtils userUtils ,
3032 ILogger < CaptionsController > logger ) : base ( context , logger )
3133 {
3234 _captionQueries = captionQueries ;
3335 _wakeDownloader = wakeDownloader ;
36+ _userUtils = userUtils ;
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 )
@@ -86,32 +92,83 @@ public async Task<ActionResult<Caption>> GetCaption(string transcriptionId, int
8692
8793 // POST: api/Captions
8894 [ HttpPost ]
95+ [ Authorize ]
8996 public async Task < ActionResult < Caption > > PostCaption ( Caption modifiedCaption )
9097 {
98+ // This endpoint should handle deletion as well, which is represented by posting a caption
99+ // with the empty string as text.
100+
101+ // This endpoint should be accessible only for people who are logged in
102+ var user = await _userUtils . GetUser ( User ) ;
103+ if ( user == null )
104+ {
105+ return Unauthorized ( ) ;
106+ }
107+
91108 if ( modifiedCaption == null || modifiedCaption . Id == null )
92109 {
93110 return BadRequest ( "modifiedCaption.Id not present" ) ;
94111 }
112+
95113 Caption oldCaption = await _context . Captions . FindAsync ( modifiedCaption . Id ) ;
96114 if ( oldCaption == null )
97115 {
98116 return NotFound ( ) ;
99117 }
100118 Caption newCaption = new Caption
101119 {
102- Begin = oldCaption . Begin ,
103- End = oldCaption . End ,
120+ Begin = modifiedCaption . Begin ,
121+ End = modifiedCaption . End ,
104122 Index = oldCaption . Index ,
105123 CaptionType = oldCaption . CaptionType ,
106124 Text = modifiedCaption . Text ,
107- TranscriptionId = oldCaption . TranscriptionId
125+ TranscriptionId = oldCaption . TranscriptionId ,
126+ LastUpdatedBy = user . Id ,
127+ CreatedBy = oldCaption . CreatedBy
108128 } ;
109129 _context . Captions . Add ( newCaption ) ;
110130 await _context . SaveChangesAsync ( ) ;
111- // nope _wakeDownloader.UpdateVTTFile(oldCaption.TranscriptionId);
112131 return newCaption ;
113132 }
114133
134+ // POST: api/Captions/Add
135+ [ HttpPost ( "Add" ) ]
136+ [ Authorize ]
137+ public async Task < ActionResult < Caption > > AddCaption ( Caption newCaption )
138+ {
139+ // This endpoint should be accessible only for people who are logged in
140+ var user = await _userUtils . GetUser ( User ) ;
141+ if ( user == null )
142+ {
143+ return Unauthorized ( ) ;
144+ }
145+
146+ if ( newCaption == null )
147+ {
148+ return BadRequest ( "newCaption not present" ) ;
149+ }
150+
151+ var allCaptions = await _context . Captions . Where ( c => c . TranscriptionId == newCaption . TranscriptionId ) . ToListAsync ( ) ;
152+
153+ // Every new caption must have a unique index to avoid conflicts with existing indices.
154+ var newIndex = allCaptions . Max ( c => c . Index ) + 1 ;
155+
156+ Caption addedCaption = new Caption
157+ {
158+ Begin = newCaption . Begin ,
159+ End = newCaption . End ,
160+ Index = newIndex ,
161+ CaptionType = newCaption . CaptionType ,
162+ Text = newCaption . Text ,
163+ TranscriptionId = newCaption . TranscriptionId ,
164+ LastUpdatedBy = user . Id ,
165+ CreatedBy = user . Id
166+ } ;
167+ _context . Captions . Add ( addedCaption ) ;
168+ await _context . SaveChangesAsync ( ) ;
169+ return addedCaption ;
170+ }
171+
115172 // POST: api/Captions/UpVote
116173 [ HttpPost ( "UpVote" ) ]
117174 public async Task < ActionResult < Caption > > UpVote ( string id )
0 commit comments