diff --git a/src/InternshipSystem.Api/Controllers/DocumentsController.cs b/src/InternshipSystem.Api/Controllers/DocumentsController.cs index f680c0f..88ca5dd 100644 --- a/src/InternshipSystem.Api/Controllers/DocumentsController.cs +++ b/src/InternshipSystem.Api/Controllers/DocumentsController.cs @@ -96,21 +96,24 @@ namespace InternshipSystem.Api.Controllers await _context.Entry(edition) .Collection(e => e.Internships) .Query() - .Include(i => i.Documentation) + .Include(i => i.Documentation) .FirstAsync(i => i.Student.Id == user.PersonNumber, cancellationToken); - var document = internship.Documentation.First(d => d.Id == documentId); + var document = await _context.Entry(internship) + .Collection(i => i.Documentation) + .Query() + .Include(d => d.Scan) + .FirstAsync(d => d.Id == documentId, cancellationToken); + + document.Scan ??= new DocumentScan(); + + document.Scan.Size = memoryStream.Length; + document.Scan.Filename = documentScan.FileName; + document.Scan.Mime = _fileValidator.GetFileMime(memoryStream.ToArray()); + document.Scan.File = memoryStream.ToArray(); - document.Scan = new DocumentScan - { - File = memoryStream.ToArray(), - Size = memoryStream.Length, - Filename = documentScan.FileName, - Mime = _fileValidator.GetFileMime(memoryStream.ToArray()) - }; - document.State = DocumentState.Submitted; - + await _context.SaveChangesAsync(cancellationToken); return Ok();