public async Task <IActionResult> NewAttempt(int courseId, [FromForm] CourseTaskSubmitAttemptCommand command) { if (!_userResolver.UserBelongsToCourse(courseId)) { return(Unauthorized()); } await _commandBus.ExecuteAsync(command); return(Ok()); }
public async Task HandleAsync(CourseTaskSubmitAttemptCommand command) { var newCourseTaskAttempt = new CourseTaskAttempt() { CourseTaskId = command.CourseTaskId, Description = command.Description, UserId = command.SubmittedById, SubmittedAt = DateTime.Now }; await _context.CourseTaskAttempt.AddAsync(newCourseTaskAttempt); await _context.SaveChangesAsync(); if (command.Files != null) { List <tvz2api_cqrs.Models.File> newFiles = new List <tvz2api_cqrs.Models.File>(); command.Files.ForEach(file => { using (var ms = new MemoryStream()) { file.CopyTo(ms); var fileBytes = ms.ToArray(); var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); fileName = fileName.Substring(0, fileName.LastIndexOf(".")) + fileName.Substring(fileName.LastIndexOf(".")).ToLower(); newFiles.Add(new tvz2api_cqrs.Models.File { Name = Path.GetFileName(fileName), ContentType = file.ContentType, Data = fileBytes, Size = fileBytes.Length }); } }); await _context.File.AddRangeAsync(newFiles); await _context.SaveChangesAsync(); newFiles.ForEach(file => { _context.TaskAttemptAttachment.Add(new TaskAttemptAttachment() { CourseTaskAttemptId = newCourseTaskAttempt.Id, FileId = file.Id }); }); await _context.SaveChangesAsync(); } }